Python if statement
Overview
Common data logic judgment conditions supported by Python
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
if statement
a = 66
b = 200
if b > a :
print("b is greater than a")
Output:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/01.py
b is greater than a
Process finished with exit code 0
if...elif statement
a = 200
b = 200
if b > a :
print("b is greater than a")
elif b == a:
print("b is equal a")
Output:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/01.py
b is equal a
Process finished with exit code 0
if...elif...else statement
a = 200
b = 99
if b > a :
print("b is greater than a")
elif b == a:
print("b is equal a")
else:
print("b is less than a")
Output:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/01.py
b is less than a
Process finished with exit code 0
Python relies on indentation and uses spaces to define ranges in code. Other programming languages usually use curly braces for this purpose
Single-line if statement
If there is only one statement to be executed, you can put it on the same line as the if statement.
a = 200
b = 66
if a > b: print("a is greater than b")
Output:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/01.py
a is greater than b
Process finished with exit code 0
Single-line if...else statement
If there are only two statements to be executed, one for if and the other for else, you can put them all on the same line
a = 200
b = 66
print("A") if a > b else print("B")
Output:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/01.py
A
Process finished with exit code 0
One-line if...else statement with three conditions
a = 200
b = 66
print("A") if a > b else print("=") if a == b else print("B")
Output:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/01.py
A
Process finished with exit code 0
If statement combining conditions
and if statement
a = 200
b = 66
c = 500
if a > b and c > a:
print("Both conditions are True")
else:
print("conditions is not True")
Output:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/01.py
Both conditions are True
Process finished with exit code 0
or if statement
a = 200
b = 66
c = 500
if a > b or a > c:
print("At least one of the conditions is True")
Output:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/01.py
At least one of the conditions is True
Process finished with exit code 0
Nested if statement
x = 52
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
Output:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/01.py
Above ten,
and also above 20!
Process finished with exit code 0
pass statement
The if statement cannot be empty, but if you write an if statement with no content for some reason, please use the pass statement to avoid errors.
a = 66
b = 200
if b > a:
pass