Learn Python with me : If...Else
Understanding Python If-Else Statements
Explanation:
In programming, we often need to make decisions based on certain conditions. For example, you might want your program to perform a certain action if a condition is true, and a different action if the condition is false. This is where if-else statements come into play.
In Python, if-else statements allow you to control the flow of your program based on conditions. Here's how they work:
- if statement: This checks a condition. If the condition is true, the block of code inside the if statement is executed.
- else statement: This is optional and comes after the if statement. If the condition in the if statement is false, the block of code inside the else statement is executed.
Syntax:
if condition:
# code to execute if condition is true
else:
# code to execute if condition is false
Real-life Example:
Imagine you are organizing a school sports day and you want to check if a student is eligible to participate based on their age.
- If the student's age is 12 or older, they can participate.
- If the student's age is less than 12, they cannot participate.
Here's how you can write this in Python:
age = 13
if age >= 12:
print("You are eligible to participate in the sports day.")
else:
print("You are not eligible to participate in the sports day.")
Sample Programs:
Easy Level:
Check if a number is positive or negative:
number = -5
if number >= 0:
print("The number is positive.")
else:
print("The number is negative.")
Check if a student passed or failed based on their score:
score = 75
if score >= 50:
print("You passed the exam!")
else:
print("You failed the exam. Try again!")
Intermediate Level:
Check if a year is a leap year:
year = 2024
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
Check if a number is even or odd:
number = 7
if number % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
Expert Level:
Check the grade of a student based on their score:
score = 85
if score >= 90:
grade = 'A'
elif score >= 80:
grade = 'B'
elif score >= 70:
grade = 'C'
elif score >= 60:
grade = 'D'
else:
grade = 'F'
print(f"Your grade is: {grade}")
Homework Questions:
- Write a Python program to check if a person is eligible to vote (age should be 18 or older).
- Write a Python program to check if a given number is divisible by 5.
- Write a Python program to check if a given character is a vowel or consonant.
- Write a Python program to determine if a number is positive, negative, or zero.
- Write a Python program to check if a student has passed with distinction (score 85 or above), passed (score 50-84), or failed (below 50).
- Write a Python program to find the largest of three numbers.
- Write a Python program to check if a given year is a leap year.
- Write a Python program to determine the type of triangle based on the lengths of its sides (equilateral, isosceles, or scalene).
- Write a Python program to check if a string is a palindrome.
- Write a Python program to calculate the electricity bill based on the number of units consumed (use different rates for different ranges of units).