0% found this document useful (0 votes)
3 views4 pages

Python Programs for Conditional Logic

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Python Programs for Conditional Logic

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

🧩 1.

Program using if — Check Leap Year

🔹 Question:

Write a Python program to check if a given year is a leap year or not using an if statement only.

💻 Code:

year = int(input("Enter a year: "))

if (year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)):


print(year, "is a Leap Year")

🧠 Explanation:

 A leap year is divisible by 4.


 But if it’s divisible by 100, it must also be divisible by 400.
 Example:
Input → 2024
Output → 2024 is a Leap Year

🧩 2. Program using if-else — Check Even or Odd

🔹 Question:

Write a Python program to check whether a number is even or odd using if-else.

💻 Code:

num = int(input("Enter a number: "))

if num % 2 == 0:
print(num, "is Even")
else:
print(num, "is Odd")

🧠 Explanation:

 If number divided by 2 gives remainder 0 → Even.


 Otherwise → Odd.
 Example: Input: 7 → Output: Odd
🧩 3. Program using if-elif-else — Grading System

🔹 Question:

Write a Python program that takes a student's marks and displays their grade.

💻 Code:

marks = float(input("Enter your marks: "))

if marks >= 90:


print("Grade: A+")
elif marks >= 80:
print("Grade: A")
elif marks >= 70:
print("Grade: B")
elif marks >= 60:
print("Grade: C")
elif marks >= 50:
print("Grade: D")
else:
print("Grade: Fail")

🧠 Explanation:

 The program checks multiple conditions in order.


 The first condition that is True will execute.
 Example: Input 85 → Output: Grade: A

🧩 4. Program using Nested if — Check Largest of Three Numbers

🔹 Question:

Write a Python program to find the largest number among three numbers using nested if.

💻 Code:

a = int(input("Enter first number: "))


b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

if a > b:
if a > c:
print(a, "is the largest")
else:
print(c, "is the largest")
else:
if b > c:
print(b, "is the largest")
else:
print(c, "is the largest")

🧠 Explanation:

 Outer if compares a and b.


 Inner if compares the larger one with c.
 Example: a=12, b=9, c=15 → Output: 15 is the largest

🧩 5. Program using if-elif-else — Electricity Bill Calculator

🔹 Question:

Write a Python program to calculate electricity bill using following conditions:

 Up to 100 units → Rs. 5 per unit


 101–200 units → Rs. 7 per unit
 Above 200 units → Rs. 10 per unit

💻 Code:

units = int(input("Enter electricity units consumed: "))

if units <= 100:


bill = units * 5
elif units <= 200:
bill = (100 * 5) + (units - 100) * 7
else:
bill = (100 * 5) + (100 * 7) + (units - 200) * 10

print("Total Electricity Bill = Rs.", bill)

🧠 Explanation:

 Uses multiple elif conditions to apply correct rate.


 Example: Units = 250 → Bill = Rs. 1850

You might also like