Conditional Statements in Python
Conditional statements are used to make decisions in a Python program.
They allow a program to execute different blocks of code depending on whether a condition is True or False.
Types of Conditional Statements
Python provides the following conditional statements:
ifif...elseif...elif...else- Nested
if
1. The if Statement
The if statement executes a block of code only when the given 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
2. The if...else Statement
The else block executes when the if condition is False.
Syntax
if condition:
# code if condition is True
else:
# code if condition is False
Example
age = 16
if age >= 18:
print("You are eligible to vote")
else:
print("You are not eligible to vote")
Output:
You are not eligible to vote
3. The if...elif...else Statement
When you need to check multiple conditions, you can use elif.
Syntax
if condition1:
# code
elif condition2:
# code
else:
# code
Example
marks = 75
if marks >= 90:
print("Grade A+")
elif marks >= 75:
print("Grade A")
elif marks >= 60:
print("Grade B")
else:
print("Grade C")
Output:
Grade A
4. Nested if Statement
An if statement inside another if statement is called a nested if statement.
Example
age = 20
has_id = True
if age >= 18:
if has_id:
print("Entry allowed")
Output:
Entry allowed
Using Comparison Operators
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
a = 10
b = 20
if a < b:
print("a is smaller than b")
Output:
a is smaller than b
Using Logical Operators
You can combine multiple conditions using logical operators.
and
Returns True when both conditions are true.
age = 25
salary = 30000
if age >= 18 and salary >= 25000:
print("Eligible")
or
Returns True when at least one condition is true.
age = 17
has_permission = True
if age >= 18 or has_permission:
print("Allowed")
not
Reverses the result of a condition.
is_raining = False
if not is_raining:
print("You can go outside")
Important: Indentation
Python uses indentation to define a block of code.
Correct:
age = 20
if age >= 18:
print("Adult")
Incorrect:
age = 20
if age >= 18:
print("Adult")
The second example produces an IndentationError.
Real-World Example
username = "admin"
password = "1234"
if username == "admin" and password == "1234":
print("Login successful")
else:
print("Invalid username or password")
Output:
Login successful
Summary
Conditional statements allow Python programs to make decisions.
The main conditional statements are:
if
if...else
if...elif...else
nested if
They are essential for controlling the flow of a Python program.
WordPress settings
Title: Conditional Statements in Python
Slug: conditional-statements
Category: Python
URL:
cybercodeacademy.com/python/conditional-statements/
This will automatically appear in your Python Tutorial sidebar along with What is Python and Variable.