Conditional Statements in Python
In the previous lesson, we learned about Python Operators and how they are used to perform calculations, comparisons, and logical operations. Today, we will learn about Conditional Statements, which allow programs to make decisions based on certain conditions.
Conditional Statements are very important because they help programs respond differently depending on user input or specific situations.
What are Conditional Statements?
Conditional Statements are used to execute different blocks of code depending on whether a condition is "True" or "False".
Python mainly uses:
- "if"
- "if...else"
- "if...elif...else" -_ Nested_ "if"
The if Statement
The "if" statement executes a block of code only if the condition is "True".
Syntax
if condition:
# code to execute
Example
age = 18
if age >= 18:
print("You are eligible to vote")
Output
You are eligible to vote
The if...else Statement
The "else" block executes when the condition is "False".
Syntax
if condition:
# code if condition is true
else:
# code if condition is false
Example
number = 5
if number % 2 == 0:
print("Even Number")
else:
print("Odd Number")`
Output
Odd Number
The if...elif...else Statement
The "elif" statement allows checking multiple conditions.
Syntax
if condition1:
# code
elif condition2:
# code
else:
# code
Example
marks = 75
if marks >= 80:
print("Grade A")
elif marks >= 70:
print("Grade B")
elif marks >= 60:
print("Grade C")
else:
print("Fail")
Output
Grade B
Nested if Statements
A Nested "if" means placing an "if" statement inside another "if" statement.
Example
age = 20
citizen = True
if age >= 18:
if citizen:
print("Eligible to vote")
Output
Eligible to vote
Indentation in Python
Python uses indentation (spaces) to define blocks of code.
Example
age = 18
if age >= 18:
print("Adult")
Incorrect indentation causes errors.
Incorrect Example
age = 18
if age >= 18:
print("Adult")
This will produce an "IndentationError".
Comparison Operators in Conditions
Conditional Statements commonly use Comparison Operators.
Operator| Meaning
"=="| Equal to
"!="| Not equal to
">"| Greater than
"<"| Less than
">="| Greater than or equal to
"<="| Less than or equal to
Example
x = 10
if x > 5:
print("x is greater than 5")
Logical Operators in Conditions
Logical Operators help combine multiple conditions.
Operator| Meaning
"and"| Both conditions must be True
"or"| At least one condition must be True
"not"| Reverses the condition
Example
age = 22
has_id = True
if age >= 18 and has_id:
print("Access Granted")
Output
Access Granted
Simple Login Program
Example
username = input("Enter username: ")
password = input("Enter password: ")
if username == "admin" and password == "1234":
print("Login Successful")
else:
print("Invalid Credentials")
Why Conditional Statements are Important
Conditional Statements help programs:
- Make decisions
- Validate user input
- Control program flow
- Build login systems
- Create intelligent applications
- Respond to different situations
Without conditions, programs would always behave the same way.
Conclusion
Conditional Statements are essential in Python programming because they allow programs to make decisions and respond based on conditions.
Understanding "if", "else", and "elif" statements is important for building interactive and dynamic applications.
Practice writing programs using different conditions and logical operators to strengthen your understanding.
Top comments (0)