In Python there are two types of loops, for and while, and a lot of statements that allow to change their behaviour, such as pass, break and continue.
You can use while loops to repeat a piece of code as long as the given condition is satisfied.
code inside the loop
Remember that, unlike C and Java, Python doesn’t group blocks of code by brackets, but based on the amount of space before each line.
→This grouping method is called indentation. If you want to know more, you can read this page from the official Python manual.
Furthermore in Python you can use the else statement with loops, too.
n = 80
n = n/2
print("n = 5")
When the condition in a loop becomes false, the program executes the code inside the else statement.
If there’s no else statement, the program will directly execute the first line of code after the loop.
For now, using the else statement seems a bit useless: it appears to do the same thing a simple line of code after the loop would do.
You will understand its function as you read about the break statement in the sections below.
→If you’re interested in the differences of the while loop between C and Python, have a look at this
The for loop‘s syntax in Python is different from the one in other languages and it’s more similar to the one of for each in Java.
In Python for loops are used to iterate in a range of values:
for value in list:
At each cycle the variable value will get the value of the next element in the list.
The variable list can be a string, an array or a range of numbers (
range(min, max)): in short, anything that can contain multiple elements.
- Words in a list:
12345list = ["Cat", "Dog, "Bird"]for element in list:print(element)
- Letters in a word:
1234for letter in word:print(letter)
- Numbers from 2 to 10:
1234for n in range(2, 10):print(n)
Also in this case you can use the else statement.
→If you’re interested in the differences of the for loop between C and Python, have a look at this
The Break Statement
You can use the break statement to interrupt a loop and force the program to exit it.
This interruption doesn’t call the else statement, because technically the loop hasn’t ended because its condition was false.
n = 32
odd = True
n = n/2;
odd = False
print("Loop terminated because n is odd")
print("Loop terminated because n<10")
The program above divides n by 2 at each cycle until n<10 or n becomes odd.
In the first case, the loop interrupts due to its condition, not because n was odd, therefore the else statement is executed.
In the second case, the break statement stops the loop, so the code in else is not executed.
The Continue Statement
Just like in C, in Python the continue statement allows the program to skip a cycle without interrupting the loop.
string = "Remove all the space characters form this sentence"
for c in string:
if c==' ':
This program skips all the spaces in string and prints all the others characters.
The Pass Statement
The pass statement does literally nothing.
I’m not saying it’s useless; actually it’s very useful.
In fact, since in Python statements can’t be empty, you can use pass to write loops that do nothing until their condition becomes false.
The pass statement is also used to write empty functions and honestly I use it much more than break and continue.
The Logical Operators
If you want to combine more conditions, you will need these:
|and||Returns true if both conditions are true||A and B||&&|
|or||Returns true if at least one condition is true||A or B|||||
|not||Returns true if the condition is false and viceversa||not A||!|
The Comparison Operators
Last but not least, the essential operators, the ones without which you can’t write any condition:
|>||Greater that – True if left operand is greater than the right||x > y|
|<||Less that – True if left operand is less than the right||x < y|
|==||Equal to – True if both operands are equal||x == y|
|!=||Not equal to – True if operands are not equal||x != y|
|>=||Greater than or equal to – True if left operand is greater than or equal to the right||x >= y|
|<=||Less than or equal to – True if left operand is less than or equal to the right||x <= y|
This has been a short and simple guide to loops in Python.
Now you know:
- What they are
- How to use them
- All the statements that can change their behaviour
- How to write conditions
- And finally, how to combine those conditions
If you have any questions, feel free to ask them in a comment.
If you liked this tutorial, please share it and subscribe to the site’s newsletter.
Anyway, the post is finished, have a good day and we will see next week!
From Zephyro it’s all, Bye!