Unit 1:
Python Control Structures and Conditional Statements
1. Introduction to Control Structures
Control structures allow the flow of a program to change based on
conditions or loops. Python offers various constructs for decision-making
and iteration.
Types of Control Structures
• Conditional Statements
• Loops
• Branching Statements
2. Conditional Statements
Conditional statements enable decision-making in Python by executing
code blocks based on conditions.
2.1 The if Statement
• Executes a block of code if a specified condition is True.
• Syntax:
if condition:
# Code block
• Example:
age = 18
if age >= 18:
print("You are eligible to vote.")
2.2 The if-else Statement
• Provides an alternate block of code if the condition is False.
• Syntax:
if condition:
# Code block for True condition
else:
# Code block for False condition
• Example:
num = 10
if num % 2 == 0:
print("Even number")
else:
print("Odd number")
2.3 The if-elif-else Statement
• Used to check multiple conditions.
• Syntax:
if condition1:
# Code block for condition1
elif condition2:
# Code block for condition2
else:
# Code block if all conditions are False
• Example:
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: F")
2.4 Nested if Statements
• if statements inside another if statement.
• Example:
num = 10
if num > 0:
if num % 2 == 0:
print("Positive even number")
3. Logical Operators in Conditional Statements
• Combine multiple conditions using logical operators.
• Operators:
o and: True if both conditions are True.
o or: True if at least one condition is True.
o not: Reverses the truth value.
• Example:
age = 20
income = 50000
if age > 18 and income > 30000:
print("Eligible for loan")
4. Practice Exercises
1. Write a program to check if a given number is positive, negative, or
zero.
2. Create a program to determine the largest of three numbers using if-
elif-else.
3. Use nested if statements to categorize a person’s age group (child,
teenager, adult, senior).
4. Combine and & or to write a program for checking login credentials.