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

If Else Exercise

The document provides a series of Python programming exercises focused on using if-else statements. Each exercise includes a specific task, such as checking if a number is positive or negative, determining if a year is a leap year, and categorizing age groups. Sample code solutions are also provided for each exercise to demonstrate the implementation of the concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

If Else Exercise

The document provides a series of Python programming exercises focused on using if-else statements. Each exercise includes a specific task, such as checking if a number is positive or negative, determining if a year is a leap year, and categorizing age groups. Sample code solutions are also provided for each exercise to demonstrate the implementation of the concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Basic Python Programming

ExErcisE
if – ElsE statEmEnt

Md. Ayub Islam Prince Reference –


Lab Assistant , University Of Global Village
1. W3 School,
[email protected] 2. Stack overflow
If Else Related Some Exercise

1. Write a Python program to check if a number is positive or negative.


2. Write a Python program to determine if a number is even or odd.
3. Write a Python program to check if a year is a leap year.
4. Write a Python program to find the largest of three numbers.
5. Write a Python program to calculate grades based on marks.
6. Write a Python program to categorize the age into child, teen, adult, or senior .
7. Write a Python program to check if a number is in a given list.

8. Write a Python program to check if a string is uppercase.


9. Write a Python program to check if a number is a multiple of 5.
10. Write a Python program to check if the length of a password is between 8 and 16
character.
Answer
1. Write a Python program to check if a number is positive or negative.
Code :
number = int(input("Enter a number: "))
if number > 0:
print("Positive")
else:
print("Negative")

2. Write a Python program to determine if a number is even or odd.


Code :
number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even")
else:
print("Odd")

3. Write a Python program to check if a year is a leap year.


Code :
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap year")
else:
print("Not a leap year")

4. Write a Python program to find the largest of three numbers.


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

if a > b and a > c:


print(f"The largest number is {a}")
elif b > a and b > c:
print(f"The largest number is {b}")
else:
print(f"The largest number is {c}")
5. Write a Python program to calculate grades based on marks.
Code :
marks = int(input("Enter your marks: "))
if marks >= 90:
print("Grade: A")
elif marks >= 80:
print("Grade: B")
elif marks >= 70:
print("Grade: C")
elif marks >= 60:
print("Grade: D")
else:
print("Grade: F")

6. Write a Python program to categorize the age into child, teen, adult, or senior.
Code :
age = int(input("Enter your age: "))
if age < 13:
print("Child")
elif age < 20:
print("Teen")
elif age < 60:
print("Adult")
else:
print("Senior")

7. Write a Python program to check if a number is in a given list.


Code :
numbers = [1, 5, 8, 3]
num = int(input("Enter a number: "))
if num in numbers:
print("The number is in the list")
else:
print("The number is not in the list")
8. Write a Python program to check if a string is uppercase.
Code :
text = input("Enter a string: ")
if text.isupper():
print("The string is uppercase")
else:
print("The string is not uppercase")

9. Write a Python program to check if a number is a multiple of 5.


Code :
number = int(input("Enter a number: "))
if number % 5 == 0:
print("The number is a multiple of 5")
else:
print("The number is not a multiple of 5")

10. Write a Python program to check if the length of a password is between 8 and 16
characters.
Code :
password = input("Enter a password: ")
if 8 <= len(password) <= 16:
print("Password length is valid")
else:
print("Password length is invalid")

You might also like