Up until now, I have covered a lot of the basics of Python. I started out with the fundamentals of PowerShell and numbers and operators. Then I explained the difference between static and dynamic typing. I followed that up with the user input function and comparison operators and finally, I tackled conditional logic. Today it’s time to finish the basics: Using for and while loops in Python.[eckosc_contrast_block]
The for loop
There are two types of loops in Python, the for loop and the while loop. I’ll start with the former.
The idea behind the for loop is that there is a collection of data which we can iterate over a set number of times. For example, you might have a list of numbers which you want to loop through and gather some data from. Or you might want to loop through a String, though this is less common. The basic syntax looks like this:
# Prints out the numbers 0,1,2,3,4 for x in range(5): print(x)
For loops can iterate over a sequence of numbers using the “range” and “xrange” functions. The difference between range and xrange is that the range function returns a new list with numbers of that specified range, whereas xrange returns an iterator, which is more efficient. In the above example, we loop through 5 numbers. Since Python the range function in Python is zero based, range(5) starts at 0 and counts 5 numbers, ending at 4. The “x” is a variable only available in this loop for iteration. Here, it prints the numbers in the given range to the console.
You can expand your for loops by adding conditional logic to them. There’s also the break and continue statements. Break is used to exit a for loop or a while loop, whereas continue is used to skip the current block, and return to the “for” or “while” statement. In the previous example, we printed a range of numbers in the normal order.
Now, look at the following example where I loop through 10 numbers and use an if statement to print only the odd numbers:
for x in range(10): # Check if x is even if x % 2 == 0: continue print(x)
One last thing to add: you can also add an else statement in the loop![/eckosc_contrast_block]
The while loop
While loops are similar to for loops. The difference however, is that a while loop will continue looping until a Boolean condition is met. This means you must use conditional logic in a while loop. Look at this example:
# Prints out 0,1,2,3,4 count = 0 while count < 5: print(count) count += 1 # This is the same as count = count + 1
You can read the first line of code as actual English. While the variable “count“, which is set to 0, is lower than 5, print the number it is currently equal to and then, add 1 to it.
Practice makes perfect, so as always, I recommend using for and while loops in your own applications to get to master them. If you want more info and examples to expand your knowledge, check out this part of the Python documentation. Since this was the final part in a series where I explain multiple facets of the Python basics, be sure to let me know if you enjoyed it!
Let me know if this was helpful or if you would like to see more of these types or articles in the future. As always, if you have questions or concerns, feel free to comment below. Lastly, make sure to share the article if you liked it!