Python conditions are can be used in multiple ways in Python. Two of the most common ways are using if statements and loops. In this Python tutorial, we will cover if statements and the logical conditions from mathematics.
Python Logical Conditions
- Equal: a == b
- Not Equal: a != b
- Greater than: a > b
- Greater than or equal to: a >= b
- Less than: a < b
- Less than or equal to: a <= b
In the below Python if statements, a and b are the created variables that will be used to compare the logical conditions. This will test if a and b are equal, if a and b are not equal,if a is greater than b, if a is greater than or equal to b, if a is less than b, and if a is less than or equal to b.
Input:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | a = 25 b = 10 equal = a == b not_equal = a != b greater_than = a > b greater_than_or_equal_to = a >= b less_than = a < b less_than_or_equal_to = a <= b print("\nPython Logical Condition Examples") print(f" First variable is: {a} ") print(f" Second variable: {b} ") print(f" a is equal to b: {equal} ") print(f" a is not equal to b: {not_equal} ") print(f" a is greater than b: {greater_than} ") print(f" a is greater than or equal to b: {greater_than_or_equal_to} ") print(f" a is less than b: {less_than} ") print(f" a is less than or equal to b: {less_than_or_equal_to} ") |
Output:
1 2 3 4 5 6 7 8 9 | Python Logical Condition Examples First variable is: 25 Second variable: 10 a is equal to b: False a is not equal to b: True a is greater than b: False a is less than b: True a is greater than or equal to b: False a is less than or equal to b: True |
Python Indentation
Python code needs four space of indentation, followed by a colon at the end of the statement. This is how one can tell Python is creating a new block of code, and then followed by four spaces of indentation. If the Python code is not indented properly, a syntax error will occur because after the colon, an indentation should occur in the block of code.
Python If Statement
Input:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | a = 25 b = 10 print("\nPython If Statement Examples") print(f" First variable is: {a}") print(f" Second variable: {b} \n") if a == b: print(" a is qual to b") if a != b: print(" a is not equal to b") if a > b: print(" a is greater than b") if a >= b: print(" a is greater than or equal to b") if a < b: print(" a is less than b") if a <= b: print(" a is less than or equal to b") |
Output:
1 2 3 4 5 6 7 | Python If Statement Examples First variable is: 25 Second variable: 10 a is not equal to b a is greater than b a is greater than or equal to b |
Python Keyword AND Logical Operator
The keyword AND logical operator is used to combine multiple conditional statements and all conditions must be met to be true.
Input:
1 2 3 4 5 6 7 8 9 | print("\nPython AND Logical Operator Statement Examples") print(f" First variable is: {a}") print(f" Second variable: {b} \n") if a != b and a > b and a >= b : print(" All AND logical operator conditions are met.") print(" \ta is not equal to b") print(" \ta is greater than b") print(" \ta is greater than or equal to b") |
Output:
1 2 3 4 5 6 7 8 | Python AND Logical Operator Statement Examples First variable is: 25 Second variable: 10 All AND logical operator conditions are met. a is not equal to b a is greater than b a is greater than or equal to b |
Python Keyword OR Logical Operator
The keyword OR logical operator is used to combine multiple conditional statements, but only only one condition must be met to be true.
Input:
1 2 3 4 5 6 | print("\nPython OR Logical Operator Statement Examples") print(f" First variable is: {a}") print(f" Second variable: {b} \n") if a == b or a != b or a > b or a >= b or a <= b : print(" One OR more of the conditions were met.") |
Output:
1 2 3 4 5 | Python OR Logical Operator Statement Examples First variable is: 25 Second variable: 10 One OR more of the conditions were met. |
Python If…Elif…Else Statement
From the above if statement, an if…elif…else statement can be executed. But the statement will only print out the first condition that is true, whereas the above printed out all conditions that are true because there were 6 separate if statements.
Input:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | a = 25 b = 10 print("\nPython If...Elif...Else Statement Examples") print(f" First variable is: {a}") print(f" Second variable: {b} \n") if a == b: print(" a is qual to b") elif a != b: print(" a is not equal to b") elif a > b: print(" a is greater than b") elif a >= b: print(" a is greater than or equal to b") elif a < b: print(" a is less than b") else: print(" a is less than or equal to b") |
Output:
1 2 3 4 5 | Python If...Elif...Else Statement Examples First variable is: 25 Second variable: 10 a is not equal to b |