# simple if
x = 7
if x > 5:
print("x is greater than 5")
'''
No parentheses are needed around the condition. (But if added means also its fine!)
A colon : follows the if statement. (mandatory)
Indentation (usually 4 spaces) is used to indicate the block of code to run.
'''
x is greater than 5
# But if there is only one line in the if, no need to write in next line
y = 50
if y % 5 == 0: print(f"{y} is divisible by 5!")
50 is divisible by 5!
# if else
num = int(input())
if num % 2 == 0: print("Even number")
else: print("Odd number")
2025 Odd number
# multiple statements in single line but WITHOUT Indentation
age = 7
if age >= 18: print(f"You are {age} years old."); print("You are eligible to vote!") # multiple statements separated with ;
else:
print(f"You are {age} years old.");
print("You are a minor.")
years = 18 - age
print(f"You will be eligible to vote after {years} years.")
You are 7 years old. You are a minor. You will be eligible to vote after 11 years.
# if-elif-else
marks = int(input("Enter your marks out of 100: "))
if marks > 100 or marks < 0:
print("Invalid Marks.")
elif marks >= 90:
print("Grade: A")
elif marks >= 80:
print("Grade: B")
elif marks >= 70:
print("Grade: C")
elif marks >= 60:
print("Grade: D")
elif marks >= 50:
print("Grade: E")
else:
print("Grade: F")
'''
The program checks from top to bottom.
As soon as a condition is True, it runs that block and skips the rest.
'''
Enter your marks out of 100: 97 Grade: A
# Nested if else
num = 7
if num >= 0:
if num % 2 == 0:
print("The number is positive and even.")
else:
print("The number is positive and odd.")
else:
if num % 2 == 0:
print("The number is negative and even.")
else:
print("The number is negative and odd.")
'''
First, it checks if the number is positive or negative.
Inside each case, it then checks if the number is even or odd.
'''
The number is positive and odd.
# Single line if else
a = 7
b = 18
print(a if a > b else b) # greatest of two numbers
18
# if-else with not for Negation
is_raining = False
if not is_raining:
print("It is not raining today.")
else:
print("Take an umbrella.")
It is not raining today.
# FizzBuzz Program with logical mistake
num = 15
if num % 3 == 0:
print("Fizz")
elif num % 5 == 0:
print("Buzz")
elif num % 3 == 0 and num % 5 == 0:
print("FizzBuzz")
else:
print(num)
'''
First if condition (num % 3 == 0): This checks if the number is divisible by 3.
For numbers like 3, 6, 9, 12, it prints "Fizz". This part works fine.
Second elif condition (num % 5 == 0): This checks if the number is divisible by 5.
If the number is 5, 10, 20, it prints "Buzz". This is also correct.
Third elif condition (num % 3 == 0 and num % 5 == 0): This checks if the number is divisible by both 3 and 5.
Here's the mistake: This condition will never execute because of the order of the conditions.
If a number is divisible by both 3 and 5, the first if (num % 3 == 0) will already be true, and the program will never reach the third elif block.
Example: For num = 15, the first if condition will be True, so "Fizz" will be printed, and the program will stop there, without checking the third condition.
'''
Fizz
# Corrected Version
num = 7
# Check if divisible by both 3 and 5 first
if num % 3 == 0 and num % 5 == 0:
print("FizzBuzz")
# If not, check if divisible by 3
elif num % 3 == 0:
print("Fizz")
# If not, check if divisible by 5
elif num % 5 == 0:
print("Buzz")
# If none of the above, print the number
else:
print(num)
7
# Single Line FizzBuzz
num = 10
print("Fizz" if num % 3 == 0 else "Buzz" if num % 5 == 0 else num)
Buzz
# Check Whether a Number is a Perfect Square
import math
num = 49
if num < 0:
print("Negative numbers cannot be perfect squares.")
else:
square_root = math.sqrt(num)
if square_root == int(square_root):
print(f"{num} is a Perfect Square.")
else:
print(f"{num} is Not a Perfect Square.")
49 is a Perfect Square.
# Leap Year Checker - using nested if else
year = 2025
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.")
2025 is Not a Leap Year.
# Leap Year Checker - using if elif else
year = 2100
if year % 4 == 0 and year % 100 != 0:
print(f"{year} is a Leap Year.")
elif year % 400 == 0:
print(f"{year} is a Leap Year.")
else:
print(f"{year} is Not a Leap Year.")
2100 is Not a Leap Year.
# Leap Year Checker - using if else with Chained Conditions
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.")
2024 is a Leap Year.
# Leap Year Checker - one line if else
year = 2000
print(f"{year} is a Leap Year." if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0) else f"{year} is Not a Leap Year.")
2000 is a Leap Year.
'''
Purchase Amount: The user is prompted to enter the total purchase amount.
Discount Calculation: The program uses if-elif-else to determine the applicable discount based on the amount.
If the purchase amount is less than ₹500, there’s no discount.
If the amount is between ₹500 and ₹999, a 10% discount is applied.
A 20% discount is given for amounts between ₹1000 and 1999.
If the amount is ₹2000 or more, a 30% discount is applied.
Final Price: After calculating the discount, the program displays the final price after applying the discount.
'''
amount = float(input("Enter your purchase amount: ₹"))
if amount < 50:
discount = 0
print(f"Your purchase amount is ₹{amount:.2f}. You get no discount.")
elif amount >= 500 and amount <= 999:
discount = 0.10
print(f"Your purchase amount is ₹{amount:.2f}. You get a 10% discount.")
elif amount >= 1000 and amount <= 1999:
discount = 0.20
print(f"Your purchase amount is ₹{amount:.2f}. You get a 20% discount.")
else:
discount = 0.30
print(f"Your purchase amount is ₹{amount:.2f}. You get a 30% discount.")
dis = (amount * discount)
bill = amount - dis
print(f"The discounted amount is ₹{(dis):.2f}.")
print(f"Your final price after discount is: ₹{bill:.2f}")
Enter your purchase amount: ₹1800 Your purchase amount is ₹1800.00. You get a 20% discount. The discounted amount is ₹360.00. Your final price after discount is: ₹1440.00
# A simple Quiz Application
score = 0
a1 = input("The `**` operator in Python is used for multiplication. (True/False): ")
if a1 == "false" or a1 == "False" or a1 == "FALSE" or a1 == "F" or a1 == "f":
score += 1
a2 = input("In Python, the `int()` function can convert a string containing a number into an integer. (True/False): ")
if a2 == "true" or a2 == "True" or a2 == "TRUE" or a2 == "T" or a2 == "T":
score += 1
a3 = input("In Python, the `input()` function always returns a string. (True/False): ")
if a3 == "true" or a3 == "True" or a3 == "TRUE" or a3 == "T" or a3 == "T":
score += 1
print(f"\nYour final score is: {score} / 3.")
The `**` operator in Python is used for multiplication. (True/False): False In Python, the `int()` function can convert a string containing a number into an integer. (True/False): T In Python, the `input()` function always returns a string. (True/False): True Your final score is: 3 / 3.