Loops and conditions in Python
In this tutorial we'll learn about condition and loop statements in Python programming language. Firstly, we'll look at comparison and logical operators.
Comparisons in Python
As we already know, one equal sign = is used for assignment in Python. To compare two numbers (or strings) for equality Python uses ==. To check inequality you can use != (not equal). Also Python has other common comparisons: > (greater than), < (less than), >= (greater than or equal), <= (less than or equal). Comparison operators are evaluated to Boolean value.
Logical operations in Python
Logical operations are used to construct complex logical expressions. There are 3 logical operations: AND, OR, NOT. Examples:
NOT changes truth of statement to the contrary. OR is true when one of parts of the statement is true. AND is true when both parts are true.
In Python logical operations are these keywords: not, and, or:
Python conditions
Conditional operator if allows to execute one branch of code from several, depending on condition. Simplest case of if:
In this case option "code1" will be executed only when condition returns True, so firstly "code1" will be run and then interpreter will continue execution of the program. If condition equals False, then "continuation" will be executed. This variation of if has one branch. It's used when we need to compare two numbers:
More general variation of if looks like this:
Pay attention how semicolons are placed in the if operator - after each condition.
Block elif (from else if) allows to point additional condition of the if. If condition1 is False, Python checks condition2. If condition2 is False, next condition is checked... There can be as many elif blocks as you want.
Block else is executed when all previous conditions are false. It's important to understand that only one of the branches of if will be executed. If one of the conditions is true, Python will execute it's branch, in other case will be executed branch for else. Blocks elif and else are not obligatory, you can omit them if you need to.
If two conditions are true, the branch that is placed upper will be executed.
We can nest conditional operators:
Notice that in this case block else belongs to if with condition2. In contrary to many others programming languages, intends in Python have a value. Looking at intends we see to which if block else belongs.
Python loops: while, for
In Python we can use two type of loops: while and for. while loop is more general and for loop is more specific.
Loops are needed to execute block of the code many times.
while loop
while loop is executed while it's condition is true:
If condition is True, while will execute loop body till the condition will change it's value to False. Example of infinite loop:
Here the loop condition will always be True. In each iteration variable i will be increased by 1. In this example i will increase "infinitely". Let's change condition: loop works while i is less than 100:
In this case while loop will be executed 100 times. In last iteration i will become 100 and in next check condition will become False.
Using while you need to write condition of the loop right.
for loop in Python programming language
for loop allows to iterate all elements in ordered collections. Very often for is used with lists, tuples, dictionaries and strings. In general for loop looks like this:
for loop sequentially takes each element of collection and assign it's value to temporary variable. This variable is used in the loop body. Let's look at the example:
for loop will be executed three times - for each element of a. We will see on the screen: