Mastering Python If-Else Blocks and Loops: The Key to More Efficient Python Code

StarTek
4 min readDec 8, 2022

--

Welcome, fellow Python enthusiasts! Are you ready to learn about some of the most powerful and useful features in the language? In this article, we’re going to be talking about conditional blocks and loops, two of the cornerstones of any programmer’s toolkit.

But don’t worry, we won’t be getting too deep into the weeds. This is part of our Python for Beginners series, so we’ll be keeping things friendly and approachable. Plus, we’ll be sprinkling in a few jokes to keep things light and fun. So let’s dive in!

Key points covered in this article:

  • Conditional blocks
  • For and while loops
  • Examples of each in action

Note: This article is part of my Python for Beginners series. Be sure to check out the rest of the series here.

Conditional blocks

Have you ever found yourself in a situation where you wanted your code to do one thing if a certain condition was true, and another thing if that condition was false? That’s where conditional blocks come in!

In Python, we use the keywords if, elif, and else to create conditional blocks. Here's an example:

if condition:
# code to run if condition is true
elif condition:
# code to run if condition is true
else:
# code to run if neither condition is true

The if keyword is used to specify the main condition that we're checking for. If that condition is true, the code indented underneath it will run. The elif keyword (short for "else if") is used to specify additional conditions that we want to check for. If the main if condition is false and one of the elif conditions is true, the code indented underneath that elif will run. Finally, the else keyword is used to specify code that should run if none of the conditions are true.

Here’s an example of a conditional block in action:

age = 25

if age < 21:
print("Sorry, you're not old enough to drink.")
elif age >= 21 and age < 65:
print("You're old enough to drink, but not old enough to retire.")
else:
print("Congratulations, you're old enough to retire!")

In this example, we’re checking the value of the age variable to determine which message to print. If age is less than 21, the code indented under the if condition will run and print the first message. If age is greater than or equal to 21 but less than 65, the code indented under the elif condition will run and print the second message. Otherwise, if age is 65 or older, the code indented under the else condition will run and print the third message.

Loops

Now let’s talk about loops. If you’ve ever had to do something repeatedly in your code, you know how tedious and error-prone it can be to write out the same code over and over again. That’s where loops come in!

In Python, we have two types of loops: for loops and while loops. A for loop is used to iterate over a sequence of values, such as a list or a string. A while loop is used to repeat a block of code until a certain condition is met.

Here’s an example of a for loop in action:

for i in range(10):
print(i)

In this example, we’re using a for loop to iterate over the numbers 0 through 9 (inclusive). The range() function returns a sequence of numbers, and the for loop will iterate over each of those numbers in turn, assigning the current number to the i variable. So on the first iteration of the loop, i will be 0, on the second iteration i will be 1, and so on.

In each iteration of the loop, the code indented underneath the for keyword will run. In this case, we're just printing the value of i, so the loop will print the numbers 0 through 9.

Here’s an example of a while loop:

x = 10
while x > 0:
print(x)
x -= 1

In this example, we’re using a while loop to print the numbers 10 through 1 (inclusive). The while keyword is followed by a condition that we're checking for. In this case, the condition is x > 0, which means that the loop will continue to run as long as x is greater than 0.

In each iteration of the loop, the code indented underneath the while keyword will run. In this case, we're printing the value of x and then subtracting 1 from it. This means that x will get smaller each time the loop runs, eventually reaching 0. When x reaches 0, the condition x > 0 will be false, so the loop will stop running.

Conclusion

And that’s it! You now know the basics of conditional blocks and loops in Python. With these powerful tools in your arsenal, you’ll be able to write more efficient and flexible code. Thanks for reading, and be sure to check out the rest of our Python for Beginners series for more tips and tricks. Happy coding!

I hope you’ve enjoyed reading this article and have learned something new. If you have, please consider subscribing to stay up-to-date with my latest content. Simply subscribing helps me out a lot and will allow me continue creating valuable information for you.

Thank you for your support!

--

--

StarTek
StarTek

Written by StarTek

I hope you've learned something new!

No responses yet