Python If ... Else
- Previous Page Python Dictionaries
- Next Page Python While Loop
Python Conditions and If Statements
Python supports common logical conditions from mathematics:
- Equal to:
a == b
- Not equal to:
a != b
- Less than:
a < b
- less than or equal to:
a <= b
- greater than:
a > b
- greater than or equal to:
a >= b
These conditions can be used in many ways, the most common being "if statements" and loops.
if statements use if
keywords to write.
Example
If statement:
a = 66 b = 200 if b > a: print("b is greater than a")
In this example, we used two variables,a
and b
is part of the if statement and is used to test if b is greater than a. Since a is 66 and b is 200, we know that 200 is greater than 66, so we print "b is greater than a" to the screen.
Indentation
Python relies on indentation to define the scope of code. Other programming languages typically use braces to achieve this purpose.
Example
Unindented If statement (will cause an error):
a = 66 b = 200 if b > a: print("b is greater than a") # Will cause an error
Elif
elif
keyword is Python's way of expressing "If the previous condition is incorrect, try this condition".
Example
a = 66 b = 66 if b > a: print("b is greater than a") elif a == b: print("a and b are equal")
In this example,a
equals b
, so the first condition is not met, but elif
condition is true, so we print "a and b are equal" to the screen.
Else
The else keyword captures any content not captured by the previous conditions.
Example
a = 200 b = 66 if b > a: print("b is greater than a") elif a == b: print("a and b are equal") else: print("a is greater than b")
In this example,a
is greater than b
, so the first condition is not met,elif
condition is also not met, so we move to else
condition and print "a is greater than b" to the screen.
You can also use no elif
of else
:
Example
a = 200 b = 66 if b > a: print("b is greater than a") else: print("b is not greater than a")
Abbreviated If
If you only have one statement to execute, you can put it on the same line as the if statement.
Example
A single-line if statement:
a = 200 b = 66 if a > b: print("a is greater than b")
Abbreviated If ... Else
If you only have two statements to execute, one for if and one for else, you can put them all on the same line:
Example
A single-line if-else statement:
a = 200 b = 66 print("A") if a > b else print("B")
You can also use multiple else statements on the same line:
Example
A single-line if-else statement has three conditions:
a = 200 b = 66 print("A") if a > b else print("=") if a == b else print("B")
And
and
Keywords are logical operators used to combine condition statements:
Example
Test if a is greater than b and if c is greater than a:
a = 200 b = 66 c = 500 if a > b and c > a: print("Both conditions are True")
Or
or
Keywords are also logical operators used to combine condition statements:
Example
Test if a is greater than b or if a is greater than c:
a = 200 b = 66 c = 500 if a > b or a > c: print("At least one of the conditions is True")
Nested If
You can include if statements within if statements, which is called nested if statements.
Example
x = 52 if x > 10: print("Above ten,") if x > 20: print("and also above 20!") else: print("but not above 20.")
Pass Statement
If statements cannot be empty, but if you write an if statement without content for some reason, please use the pass statement to avoid errors.
Example
a = 66 b = 200 if b > a: pass
- Previous Page Python Dictionaries
- Next Page Python While Loop