Python If...Else Statements and Conditions
Conditional statements in Python allow us to make decisions in our code. They are one of the most important programming concepts that help us execute certain blocks of code only when specific conditions are true. In this beginner-friendly guide, we will cover everything about Python If...Else, conditions, indentation, Elif, shorthand conditions, nested if, the pass statement, and logical operators (and, or, not).
Characteristics of Python Conditions
- Python uses indentation (spaces) instead of curly braces to define code blocks.
- Conditions are based on Boolean logic (True or False).
- Supports multiple conditional checks using if, elif, else.
- Logical operators (and, or, not) can combine multiple conditions.
- Shorthand notation is available for writing simple one-line conditions.
- Nested conditions allow decision-making within another decision.
Uses of If...Else in Python
Conditional statements are widely used in real-world programming:
- Decision making in applications (e.g., login systems, form validations).
- Controlling the program flow based on user inputs.
- Implementing business logic (e.g., discounts, taxes, eligibility checks).
- Error handling and input validation.
- Game development (checking win/lose conditions).
Python Conditions and If Statement
The if statement checks whether a condition is true. If it is true, the block of code under it executes.
Syntax:
if condition:
# block of code
Example:
x = 10
if x > 5:
print("x is greater than 5")
Output:
x is greater than 5
Python Indentation
Unlike many other programming languages, Python uses indentation to define blocks of code. This is a key feature of Python.
Example (Correct Indentation):
if True:
print("Indented correctly")
Example (Incorrect Indentation):
if True:
print("This will cause an error")
Note: Incorrect indentation will raise an IndentationError
.
Python Elif Statement
Elif stands for "else if". It lets you check multiple conditions.
Syntax:
if condition1:
# block of code
elif condition2:
# block of code
else:
# block of code
Example:
marks = 85
if marks >= 90:
print("Grade: A")
elif marks >= 75:
print("Grade: B")
else:
print("Grade: C")
Output:
Grade: B
Python Else Statement
The else statement runs a block of code if none of the conditions are true.
age = 15
if age >= 18:
print("You are eligible to vote")
else:
print("You are not eligible to vote")
Output:
You are not eligible to vote
Python Short Hand If
If you have only one statement to execute, you can write it in a single line.
x = 10
if x > 5: print("x is greater than 5")
Python Short Hand If...Else
Python allows writing if...else in a single line using a ternary operator style.
x = 10
print("Positive") if x > 0 else print("Non-positive")
Output:
Positive
Python Nested If
You can use an if statement inside another if statement. This is called nested if.
num = 25
if num > 10:
print("Greater than 10")
if num > 20:
print("Also greater than 20")
Output:
Greater than 10
Also greater than 20
Python Pass Statement
The pass statement is used as a placeholder. It allows us to have empty code blocks without causing errors.
if True:
pass # No action is taken here
Logical Operators in Python Conditions
Logical operators help us combine multiple conditions.
- and → Returns True if both conditions are true.
- or → Returns True if at least one condition is true.
- not → Reverses the condition.
Examples:
x = 10
y = 20
# and operator
if x > 5 and y > 15:
print("Both conditions are True")
# or operator
if x > 15 or y > 15:
print("At least one condition is True")
# not operator
if not(x > 15):
print("x is NOT greater than 15")
Output:
Both conditions are True
At least one condition is True
x is NOT greater than 15
FAQs on Python If...Else
- Q: Can I use multiple elif statements?
Yes, you can use multipleelif
conditions in a single block. - Q: What happens if no condition matches?
If noif
orelif
matches, theelse
block executes. - Q: Can if statements be empty?
No, but you can usepass
as a placeholder. - Q: Is indentation mandatory in Python?
Yes, Python requires proper indentation to define code blocks.
Exercises for Practice
- Write a program to check if a number is even or odd.
- Write a program to find the largest of three numbers using if...elif...else.
- Check if a student has passed or failed based on marks (pass if marks ≥ 40).
- Write a program to check if a person is eligible for a driving license (age ≥ 18).
- Create a program to check if a number is positive, negative, or zero.
- Use nested if to check whether a number is positive and divisible by 5.
Conclusion
In this guide, we covered Python conditions, if...else statements, indentation, elif, shorthand if, nested if, pass statement, and logical operators (and, or, not). Mastering these will help you control the flow of your programs effectively. Start with simple conditions and gradually try out the exercises above to strengthen your skills.
0 Comments