Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

CODINGS FOR PRACTICAL EXAMS-NESTED IF

21 February 2024

SYNTAX:

If Inside If if (condition1):
# Executes when condition1 is true
You can have if statements inside if statements, this is called nested if statements if (condition2):
# Executes when condition2 is true

CLASS NOTES # if Block is end here


# if Block is end here

#PYTHON PROGRAMMING FOR PRACTICAL EXAM


#1.WEEK DAYS WITH CHOICE

n=int(input("enter a number(1-7):"))
b=input("enter your choice{yes/no}):")
if b=='yes':
if n==1:
print("Monday")
elif n==2:
print("Tuesday")
elif n==3:
print("wednesday")
elif n==4:
print("thrusday")
elif n==5:
print("friday")

elif n==6:
print("SATURDAY")
elif n==7:
print("sunday")
else:
print("invalid input")
else:
print("exit loop")

#To find whether a number is positive,negative or zero

a=int(input("enter a number:"))
if a>=0:
if a>0:
print("positive")
else:
print("your number is zero")

else:
print("negative number")

#calculator using nested if

i=int(input("enter a number:"))
I=int(input("enter 2nd number:"))
M=input("enter your OPERATOR{+-*/}):")
t=input("enter your choice{yes/no}):")
print("your output;")
if t=='yes' or 'YES' or 'Yes':
if M=='+':
print(i+I)
elif M=='-':
print(I-i)
elif M=='*':
print(I*i)
elif M=='/':
print(I/i)
else:
print("invalid operator")
else:
print("exit program")

EXTRA STUDY MATERIAL FORM INTERNET AND EXAMPLES OF NESTED IF


# Python program to demonstrate
# nested if statement
Default pattern: if statement inside if
i = 13
There are two main ways to make a nested if statement. The first option is to put the if statement inside
an if code block. The other option is to place the if statement in the else code of an if/else statement. if (i == 13):
# First if statement
if (i < 15):
So the first approach has us place an if statement inside another. Here’s how that looks: print ("i is smaller than 15")

# Nested - if statement
ifconditionA:# Code that executes when 'conditionA' is TrueifconditionB:# Code that runs when 'conditionA' and# 'conditionB' are # Will only be executed if statement above
both True # it is true
Python evaluates this nested if statement when the condition of the preceding if statement is True. if (i < 12):
print ("i is smaller than 12 too")
When conditionA is False, our nested if statement never runs. That happens even when its own condition is True. else:
print ("i is greater than 12 and smaller than
15")
Note that, when the first if statement tests True, the second isn’t guaranteed to run. The condition of that
# Python program to demonstrate
nested if statement also has to test True, after all. So the code of the nested if statement only runs # nested if statement
when conditionA and conditionB are True. If one or both are False, that code doesn’t execute.
num = 15
if num >= 0:
if num == 0:
Default pattern: if statement inside else print("Zero")
else:
print("Positive number")
The second way to make a nested if statement is to place the if statement inside the else code of an if/else else:
statement. Here’s how that looks: print("Negative number")
Default pattern: if statement inside else print("Zero")
else:
print("Positive number")
The second way to make a nested if statement is to place the if statement inside the else code of an if/else else:
statement. Here’s how that looks: print("Negative number")

ifconditionA:# Code here executes when 'conditionA' is Trueelse:# Code that runs when 'conditionA' is FalseifconditionB:# Code that
runs when 'conditionA' is False# and 'conditionB' is True
Now our nested if statement can only run when the condition of the preceding if/else statement is False.
Because when that condition is True, the code block that contains the nested if statement never executes.

However, even when the if/else statement’s condition tests False, the nested if statement doesn’t always run.
After all, its own condition has to be True for that to happen.

So for the snippet above, the nested if statement only executes when conditionA is False and conditionB is True. This
shows an essential feature of the nested if statement: they only run dependent on other if statements.

From <https://kodify.net/python/if-else/nested-if/>

Test age with nested if


When we use a nested if statement, we often make additional comparisons following a regular if statement. The
program below is an example of that. We first check if the person is an adult. Then we make additional comparisons
to see if he or she graduated or gotten a drivers licENSCE :
We first make three variables. age has a value of 19. isGraduated is False. And hasLicense has a True value.

Then an if statement looks if the person is 18 years or older. Since our age variable has a value of 19, that condition
tests True. Code inside that if statement first has the print() function say something cheesy.

Then we make two nested if statements. The first checks the isGraduated variable. Since that variable is False, this if
statement’s code doesn’t execute.

The other nested if statement looks up hasLicense. That variable is True, and so Python executes its code. There
the print() function wishes the person happy driving. Note that we don’t have to check if the person is an adult; that’s
something the top if statement already did. So the ‘happy driving’ message can only show for adults.

Here’s the output that the program generates:

You're 18 or older. Welcome to adulthood!


Happy driving!

You might also like