0% found this document useful (0 votes)
9 views3 pages

Third Week Note

The document explains the use of the if statement in Python for decision-making, detailing its basic syntax and providing examples. It includes exercises for age validation, even and odd number determination, and a grading system based on score ranges. Each example includes a solution and an explanation of how the code works.

Uploaded by

dagiyoni924
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)
9 views3 pages

Third Week Note

The document explains the use of the if statement in Python for decision-making, detailing its basic syntax and providing examples. It includes exercises for age validation, even and odd number determination, and a grading system based on score ranges. Each example includes a solution and an explanation of how the code works.

Uploaded by

dagiyoni924
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
You are on page 1/ 3

5,If statement

The if statement in Python is used for decision-making. It allows the program


to execute a specific block of code only if a certain condition is true.

5.1Basic Syntax of if Statement:


if condition1:

Code that executes if condition 1 is True

elif condition2:

Code that executes if condition 2 is True

else:

Code if none of the conditions are True

Note: if you have more than 2 or 3 conditions you can you as many elif
statements as you want.

5.2 Exercises with Solutions


1. Age Validation (Above 18 Check)

Problem: Write a Python program to check if a user is 18 or older.

Solution:

age = int(input("Enter your age: "))

if age >= 18:

print("You are eligible.")

else:

print("You are not eligible.")

Explanation : If the age is 18 or more, the code prints You are eligible;
otherwise, prints You are not eligible.

2. Even and Odd Number Determination

Problem: Write a Python program to check whether a number is even or odd.

Solution:

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


if num % 2 == 0:

print("Even number")

else:

print("Odd number")

Explanation : if num % 2 == 0(reminder we get when dividing num with two


is equal to zero) means the number is divisible by 2 (Even).

Otherwise, it's an Odd number.

3. Grading System

Problem: Write a program to take a student's average as input and assign a


grade based on the following scale:

90 and above → Grade A

80-89 → Grade B

70-79 → Grade C

60-69 → Grade D

Below 60 → Grade F

Solution:

score = int(input("Enter your score: "))

if score >= 90:

print("A")

elif score >= 80:

print("B")

elif score >= 70:

print("C")

elif score >= 60:

print("D")

else:

print(" F")
Explanation: The program checks the score range and assigns a grade
accordingly.

You might also like