Python Day- 4
(RECAP OF PREVIOUS DAY)
if statement, if-else statement, Nested if and elif statements, Practical examples and exercises
1. if Statement
Definition:
The if statement is used to execute a block of code only if a specified condition evaluates to
True.
Syntax:
if condition:
# Code to execute if the condition is True
Example:
age = 18
if age >= 18:
print("You are eligible to vote.")
# Output: You are eligible to vote.
2. if-else Statement
Definition:
The if-else statement provides an alternate block of code to execute when the condition
evaluates to False.
Syntax:
if condition:
# Code to execute if the condition is True
else:
# Code to execute if the condition is False
Example:
number = 5
if number % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
# Output: The number is odd.
3. Nested if Statements
Definition:
A nested if statement is an if statement inside another if statement. It is used to check
multiple levels of conditions.
Syntax:
if condition1:
if condition2:
# Code to execute if both condition1 and condition2 are True
Example:
age = 20
citizenship = "India"
if age >= 18:
if citizenship == "India":
print("You are eligible to vote in India.")
else:
print("You are not an Indian citizen.")
else:
print("You are not eligible to vote.")
# Output: You are eligible to vote in India.
4. elif Statement
Definition:
The elif statement stands for "else if" and is used to check multiple conditions in sequence.
Once a condition evaluates to True, the corresponding block of code is executed, and the rest
of the elif statements are ignored.
Syntax:
if condition1:
# Code to execute if condition1 is True
elif condition2:
# Code to execute if condition2 is True
else:
# Code to execute if none of the above conditions are True
Example:
marks = 85
if marks >= 90:
print("Grade: A+")
elif marks >= 80:
print("Grade: A")
elif marks >= 70:
print("Grade: B")
elif marks >= 60:
print("Grade: C")
else:
print("Grade: F")
# Output: Grade: A
Practical Examples and Exercises
1. Check if a number is positive, negative, or zero
number = int(input("Enter a number: "))
if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
print("The number is zero.")
2. Find the largest of three numbers
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
c = int(input("Enter the third number: "))
if a > b and a > c:
print(f"The largest number is {a}.")
elif b > c:
print(f"The largest number is {b}.")
else:
print(f"The largest number is {c}.")
3. Check if a year is a leap year
year = int(input("Enter a year: "))
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
else:
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
4. Simple calculator using if-elif-else
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
operation = input("Enter operation (+, -, *, /): ")
if operation == "+":
print(f"Result: {num1 + num2}")
elif operation == "-":
print(f"Result: {num1 - num2}")
elif operation == "*":
print(f"Result: {num1 * num2}")
elif operation == "/":
if num2 != 0:
print(f"Result: {num1 / num2}")
else:
print("Division by zero is not allowed.")
else:
print("Invalid operation.")
5. Write a program to check whether a number is divisible by both 5 and 3.
number = int(input("Enter a number: "))
if number % 5 == 0 and number % 3 == 0:
print(f"{number} is divisible by both 5 and 3.")
else:
print(f"{number} is not divisible by both 5 and 3.")
6. Write a program to check whether a character entered by the user is a vowel or
consonant.
# Input from user
char = input("Enter a single character: ").lower()
# Check if the input is a valid single alphabet
if len(char) == 1 and char.isalpha():
if char in 'aeiou': # Check if the character is a vowel
print(f"'{char}' is a vowel.")
else:
print(f"'{char}' is a consonant.")
else:
print("Invalid input. Please enter a single alphabet.")
7. Write a program to categorize a person's age into child (0-12), teenager (13-19),
adult (20-59), and senior (60+).
age = int(input("Enter the person's age: "))
if age < 0:
print("Invalid age. Age cannot be negative.")
elif 0 <= age <= 12:
print("The person is a child.")
elif 13 <= age <= 19:
print("The person is a teenager.")
elif 20 <= age <= 59:
print("The person is an adult.")
else: # age >= 60
print("The person is a senior.")
8. Write a program to calculate the electricity bill based on the following rules:
○ First 100 units: $0.5/unit
○ Next 100 units: $0.75/unit
○ Above 200 units: $1/unit
# Input the number of units consumed
units = float(input("Enter the number of units consumed: "))
if units < 0:
print("Invalid input. Units cannot be negative.")
else:
# Initialize the bill amount
bill = 0
if units <= 100:
bill = units * 0.5
elif units <= 200:
bill = (100 * 0.5) + ((units - 100) * 0.75)
else:
bill = (100 * 0.5) + (100 * 0.75) + ((units - 200) * 1)
# Print the total bill
print(f"The total electricity bill is: ${bill:.2f}")
9. Write a program to determine if a triangle is valid based on the lengths of its sides.
# Input the lengths of the three sides of the triangle
side1 = float(input("Enter the length of the first side: "))
side2 = float(input("Enter the length of the second side: "))
side3 = float(input("Enter the length of the third side: "))
# Check if the sides are positive
if side1 <= 0 or side2 <= 0 or side3 <= 0:
print("Invalid input. Side lengths must be positive.")
else:
# Check the triangle inequality theorem
if (side1 + side2 > side3) and (side1 + side3 > side2) and (side2 + side3 > side1):
print("The triangle is valid.")
else:
print("The triangle is not valid.")
List of programs to practice (Home work)
10. Write a program to find the greatest between two numbers.
11. Write a program to Accept two Integers and Check if they are Equal.
12. Write a program to Check if a given Integer is Positive or Negative.
13. Write a program to Check if a given Integer is Odd or Even.
14. Write a program to Check if a given Integer is Divisible by 5 or not.
15. Write a program to Accept two Integers and Check if they are Equal.
16. Write a program to find the greatest of three numbers using if…elif.
17. Write a program to find the greatest of three numbers using Nested if without elif.
18. Write a program to convert an Upper case character into lower case and
vice-versa.
19. Write a program to check whether an entered year is a leap year or not.
20. Write a Program to check whether an alphabet entered by the user is a vowel or
a constant.
21. Write a program to Read a Coordinate Point and Determine its Quadrant.
22. Write a program to Add two Complex Numbers.
23. Write a Program to find roots of a quadratic expression.
24. Write a program to print the day according to the day number entered by the
user.
25. Write a program to print color names, if the user enters the first letter of the color
name.
26. Write a program to Simulate Arithmetic Calculator using switch.
27. Write a menu driven program for calculating areas of different geometrical
figures such as circle, square, rectangle, and triangle.