- Logical Conditions
- Writing ”if” statements
- Writing If variable is equal to
- Writing If variable is not equal to
- Writing If variable is greater than
- Writing If variable is less than
- Writing variable is less than or equal to
- Writing variable is greater than or equal to
- Writing variable is not equal to
- Handling Multiple conditions using if/elif statements
- Adding a default condition to you if statements
Executing code based on a condition being true of false is common in programming. Python has a few techniques for handling conditions in your program.
1) Logical Conditions
Lets first learn the logical conditions in Python:
Conditon | Syntax |
Equals | a == b |
Not Equals | a != b |
Less than | a < b |
Less then or equal to | a <= b |
Greater than | a > b |
Greater than or equal to | a >= b |
Using the “not” keyword means the condition is. false | not a == b (a is not equal to b) not a > b (a is not greater than b) Etc… |
2) Writing ”if” statements
”If” statements test if a condition is true or false. If the condition is met then the code is executed.
3) Writing If variable is equal to
a = 4
# equal to
if a == 4:
print(a)

4) Writing If variable is not equal to
b = 5
# not equal to
if b != 4:
print(b)

5) Writing If variable is greater than
c = 7
# greater than
if c > 5:
print(c)

6) Writing If variable is less than

7) Writing variable is less than or equal to

8) Writing variable is greater than or equal to
f = 6
# is greater than or equal to
if f >= 5:
print(f)

9) Writing variable is not equal to

10) Handling Multiple conditions using if/elif statements
color = "blue"
#handle multiple conditions with if/elif
if color == "red":
print("color is red")
elif color == "green":
print("color is green")
elif color == "blue":
print("color is blue")

11) Adding a default condition to you if statements
color = "yellow"
#handle multiple conditions with if/elif/else
if color == "red":
print("color is red")
elif color == "green":
print("color is green")
elif color == "blue":
print("color is blue")
else:
print("color is not red, blue or green")

- Python Lesson 1: Hello World in Python
- Python Lesson 2: Reserved Keywords
- Python Lesson 3: Creating Variables
- Python Lesson 4: Performing math calculations in Python using operators
- Python Lesson 5: Working with Strings
- Python Lesson 6: Useful String Methods
- Python Lesson 7: Getting User Keyboard input
- Python Lesson 8: Importing Modules into your Python code
- Python Lesson 9: Decision making in Python
- Python Lesson 10: Creating loops in Python
- Python Lesson 11: Lists in Python
- Python Lesson 12: Tuples in Python
- Python Lesson 13: Dictionaries in Python
- Python Lesson 14: Sets in Python
- Python Lesson 15: Dealing with dates and time
- Python Lesson 16: Creating and using functions
- Python Lesson 17: Creating and using classes in Python
- Python Lesson 18: List Comprehensions
- Python Lesson 19: Lambda Functions
- Python Lesson 20: Error Handling