Create your own game
eng   рус

Loops and conditions in Python

Previous tutorial: Python syntax and build-in types
Next tutorial: Collections 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.

5 == 5 # True 5 != 5 # False 5 > 0 # True 5 < 6 # True 5 >= 5 # True 5 <= 4 # False

Logical operations in Python

Logical operations are used to construct complex logical expressions. There are 3 logical operations: AND, OR, NOT. Examples:

The sun rises from the east # True The sun does NOT rise from the east # False Grass is green AND sky is blue # True - Both parts are true Grass is purple AND sky is blue # False - Only one part is true Grass is purple OR sky is blue # True - Second part is true

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:

1 # True not 1 # False 0 # False not 0 # True (1 == 1) and (1 < 0) # False 0 or 1 # True

Python conditions

Conditional operator if allows to execute one branch of code from several, depending on condition. Simplest case of if:

if condition: # "code1" (here several strings could be) # "continuation of the program"

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:

a = 3 b = 4 if a == b: # here we place the code, # which will be executed when a and b are equal

More general variation of if looks like this:

if condition1: # branch1 elif condition2: # branch2 elif condition3: # branch3 else: # branch4

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:

if condition1: if condition2: # branch1 else: # branch2

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:

while condition: # loop body # code after the loop

If condition is True, while will execute loop body till the condition will change it's value to False. Example of infinite loop:

i = 0 while True: i = i + 1

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:

i = 0 while i < 100: i = i + 1 print( i ) # printing i after the loop

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 element in collection: # loop body

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:

a = [1,2,3] for x in a: print(x);

for loop will be executed three times - for each element of a. We will see on the screen:

No comments yet