Python Conditional Statements: Notes
Introduction
- Decision-making is crucial in programming, just as it is in life.
- Conditional statements automate decision-making in Python.
- Python evaluates conditions as True or False:
- If True, the specified block of code executes.
- If False, another block of code is executed.
Types of Conditional Statements in Python
- If Statement
- If-Else Statement
- If…Elif…Else Ladder
- Nested If Statement
- Short Hand If Statement
- Short Hand If-Else Statement
1. If Statement
- The simplest decision-making statement.
- Executes a block of code if the condition is True.
Syntax:
if condition:
statement
Example:
num = 5
if num > 0:
print(num, "is a positive number.")
Output:5 is a positive number.
2. If-Else Statement
- Used when both True and False parts are specified.
- Executes one block of code if the condition is True and another if False.
Syntax:
if condition:
# Code for true condition
else:
# Code for false condition
Example:
num = 5
if num >= 0:
print("Positive or Zero")
else:
print("Negative number")
Output:Positive or Zero
3. If…Elif…Else Ladder
- Evaluates multiple conditions in a sequence.
- Executes the first condition that is True.
- If none are True, the else block executes.
Syntax:
if condition:
# Code for condition 1
elif condition:
# Code for condition 2
else:
# Code for no conditions true
Example:
num = 7
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Output:Positive number
4. Nested If Statement
- Allows multiple layers of decision-making.
- An
if
statement inside anotherif
statement.
Syntax:
if condition1:
if condition2:
# Code for both conditions true
else:
# Code if condition1 is true, condition2 is false
else:
# Code if condition1 is false
Example:
num = 8
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
Output:Positive number
5. Short Hand If Statement
- Used when there is only one statement in the
if
block. - Everything is written in a single line.
Syntax:
if condition: statement
Example:
i = 15
if i > 11: print("i is greater than 11")
Output:i is greater than 11
6. Short Hand If-Else Statement
- Combines
if
andelse
in a single line. - Executes one statement for True and another for False.
Syntax:
statement_if_true if condition else statement_if_false
Example 1:
a = 3
b = 5
print("A") if a > b else print("B")
Output:B
Example 2 (with 3 conditions):
a = 3
b = 5
print("A is greater") if a > b else print("=") if a == b else print("B is greater")
Output:B is greater
Key Takeaways
- If Statement: Executes code if a condition is true.
- If-Else Statement: Executes different code for true or false conditions.
- If…Elif…Else Ladder: Evaluates multiple conditions in sequence.
- Nested If Statement: Allows decision-making within another condition.
- Short Hand If Statement: Simplifies single-line true condition execution.
- Short Hand If-Else Statement: Combines
if
andelse
in a single line.
Conclusion
- Mastering conditional statements is fundamental for learning Python.
- They form the basis of decision-making in programs.
- Begin with these basics to build robust and dynamic Python applications.