0% found this document useful (0 votes)
14 views6 pages

Python Conditional Statements Guide

This document outlines the execution of programs related to conditional statements in Python, including if, elif, nested if, and switch statements. It provides explanations, syntax, and examples for each type of statement, along with practical problems such as calculating grades, BMI, electricity bills, and displaying days and months using a dictionary-based switcher. The conclusion confirms the successful implementation of these conditional statements.

Uploaded by

dqnull
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)
14 views6 pages

Python Conditional Statements Guide

This document outlines the execution of programs related to conditional statements in Python, including if, elif, nested if, and switch statements. It provides explanations, syntax, and examples for each type of statement, along with practical problems such as calculating grades, BMI, electricity bills, and displaying days and months using a dictionary-based switcher. The conclusion confirms the successful implementation of these conditional statements.

Uploaded by

dqnull
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

EXPERIMENT NO: 03

AIM:- To execute program related to conditional statements if, elif, nested if, switch statements.
THEORY:- Conditional Statement in python perform different computations or actions
depending on whether a specific Boolean constraint evaluates to true or false. Conditional
statements are handled by many statement in python.
What is python if statement?
Python if statement is used for decision-making operations. It contains a body of code which
runs only when the condition given in the if statement is true. If the condition is false, then the
optional else statement runs which contains some code for else condition.

If statement syntax :-


If expression:
Statement

What is python if/else statement?


The python if…..else statement evaluates test expression and will execute the body of if only
when the test condition is true. If the condition is false, the body of else is executed. Indentation
is used to separate the blocks.

If…else statement syntax:


If test expression:
Body of if
Else:
Body of else

What is python if…elif…else statement?


The elif is short for else if. It allows is to check for multiple expressions. If the condition for if is
false, it checks the condition of the next elif block and so on. If all the conditions are false, the
body of else is executed. Only one block among the several if…elif…else blocks is executed
according to the condition. The if block can have only one else block, but it can have multiple
elif blocks.
 If…elif...else statement syntax :-
If test expression:
Body of if
Elif test expression:
Body of elif
Else:
Body of else

 What is python Nested if statement?


We can have an if…elif…else statement inside another if…elif…else statements. This is
called nesting in computer programming. Any number of these statements can be nested inside
one another. Indentation is the only way to figure out the level of nesting. They can get
confusing, so they must be avoided unless necessary.

 Nested if statement syntax:-


If expression 1:
Statement (s)
If expression 2:
Statement (s)
elif expression 3:
Statement (s)
Else:
Statement (s)
Else:
Statement (s)
 What is switch statement in python?
Python doesn’t have a switch or case statement. To get around this fact, we use dictionary
mapping which is also known as associative arrays, that provide simple one-to-one key-value
[Link] is dictionary data type her. Get() method of dictionary data type returns value
of passed argument if it is present in dictionary otherwise second argument will be assigned as
default value of passed argument.
Problem Definition:
Write a program to accept marks and display the grade as follows:

 Marks ≥ 90 → Grade A
 Marks ≥ 75 → Grade B
 Marks ≥ 50 → Grade C
 Marks < 50 → Fail

marks = int(input("Enter the marks: "))

if marks >= 90:

print("Grade A")

elif marks >= 75 :

print("Grade B")

elif marks >= 50:

print("Grade C")

else:

print("Fail")

Write a python script to calculate the BMI of a person


Code:-
def BMI (height, weight):
bmi = weight/(height**2)
return bmi
height = 1.79832
weight = 70
bmi = BMI(height, weight)
print("The BMI is: ", bmi, "so ", end='')
if (bmi<18.5) :
print("Underweight")
elif (bmi >= 18.5 and bmi <24.9):
print("Healthy")
elif (bmi>= 24.9 and bmi < 30):
print("Overnight")
elif (bmi >=30):
print("Suffering from obesity")

Problem Definition:
Write a program to calculate the electricity bill based on units consumed:

 First 100 units → ₹1/unit


 Next 100 units → ₹2/unit
 Above 200 units → ₹3/unit

units=int(input("Enter the units of electricity used"))

if(units==100):

print("Bill is 100 Rs")

elif(units>100 and units<200):

bill=100+2*(200-units)

print("Bill is",bill)

elif(units==200):

print("Bill is Rs.300")

else:
bill=300+3*(units-200)

print("Bill is",bill)

Write a menu driven program o display day based on parameter number while calling.
Use Switcher function.
def week(i):
switcher={
0:'Sunday',
1:'Monday',
2:'Tuesday',
3:'Wednesday',
4:'Thursday',
5:'Friday',
6:'Saturday'
}
return [Link](i,"Invalid day of week")
print(week(7)

Python does not have a built-in switch statement.


A dictionary-based switcher is commonly used instead.

Write a program to display the name of the month based on the month number (1–12) using a
switcher approach.

month_switcher = {

1: "January",

2: "February",

3: "March",

4: "April",

5: "May",

6: "June",
7: "July",

8: "August",

9: "September",

10: "October",

11: "November",

12: "December"

month_number = int(input("Enter month number (1-12): "))

# Get month name using switcher

month_name = month_switcher.get (month_number, "Invalid month number")

print("Month Name:", month_name)

CONCLUSION: Hereby we have performed a program related to conditional statements if,


elif, nested if, switch statement.
**************************************************************************

You might also like