Python supports the usual logical conditions from mathematics (see Operators)
These conditions can be used in several ways, most commonly in “if statements” and loops.
An “if statement” is written by using the if keyword.
Example
In this example we use two variables, a and b, which are used as part of the if statement to test whether b is greater than a. As a is 33, and b is 200, we know that 200 is greater than 33, and so we print to screen that “b is greater than a”.
a = 33b = 200if b > a: print("b is greater than a") ```
Elif
The elif keyword is Python’s way of saying “if the previous conditions were not true, then try this condition”.
Example
In this example a is equal to b, so the first condition is not true, but the elif condition is true, so we print to screen that “a and b are equal”.
a = 33b = 33if b > a: print("b is greater than a")elif a == b: print("a and b are equal")
Else
The else keyword catches anything which isn’t caught by the preceding conditions.
Example
In this example a is greater than b, so the first condition is not true, also the elif condition is not true, so we go to the else condition and print to screen that “a is greater than b”.
You can also have an else without the elif.
a = 200b = 33if b > a: print("b is greater than a")elif a == b: print("a and b are equal")else: print("a is greater than b")
Short-Hand If
If you have only one statement to execute, you can put it on the same line as the if statement.
Example
if a > b: print("a is greater than b")
Short-Hand If … Else
If you have only one statement to execute, one for if, and one for else, you can put it all on the same line:
Example
a = 2b = 330print("A") if a > b else print("B")
And, Or, and Not
The and keyword is a logical operator, and is used to combine conditional statements:
Example
Test if a is greater than b, AND if c is greater than a:
a = 200b = 33c = 500if a > b and c > a: print("Both conditions are True")
The or keyword is a logical operator, and is used to combine conditional statements:
Example
Test if a is greater than b, OR if a is greater than c:
a = 200b = 33c = 500if a > b or a > c: print("At least one of the conditions is True")
The not keyword is a logical operator, and is used to reverse the result of the conditional statement:
Example
Test if a is NOT greater than b:
a = 33b = 200if not a > b: print("a is NOT greater than b")
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
Examples
Print each fruit in a fruit list:
fruits = ["apple", "banana", "cherry"] for x in fruits: print(x)
Loop through the letters in the word “banana”:
for x in "banana": print(x)
Exit the loop when x is “banana”:
fruits = ["apple", "banana", "cherry"] for x in fruits: print(x) if x == "banana": break
Exit the loop when x is “banana”, but this time the break comes before the print:
fruits = ["apple", "banana", "cherry"] for x in fruits: if x == "banana": break print(x)
More for loops
title: "W3Schools.com"image: "https://www.w3schools.com/images/w3schools_logo_436_2.png"description: "W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more."url: "https://www.w3schools.com/python/python_for_loops.asp"favicon: ""aspectRatio: "52.293577981651374"