Type Here to Get Search Results !

Write simple Python program to demonstrate use of conditional statements: if’ statement, ‘if … else’ statement, Nested ‘if’ statement

 Write simple Python program to demonstrate use of conditional statements: if’ statement, ‘if … else’ statement, Nested ‘if’ statement


a) IF Statement: if statement is the simplest decision making statement. It is used to

decide whether a certain statement or block of statements will be executed or not i.e if

a certain condition is true then a block of statement is executed otherwise not.


Write simple Python program to demonstrate use of conditional statements: if’ statement, ‘if … else’ statement, Nested ‘if’ statement

Syntax:

If condition:

 Statement(s)

Example:

i=10

if(i > 20):

 print ("10 is less than 20")

print ("We are Not in if ")

Output: We are Not in if


b) If-else Statement: The if statement alone tells us that if a condition is true it will

execute a block of statements and if the condition is false it won’t. But what if we

want to do something else if the condition is false. Here comes the else statement. We

can use the else statement with if statement to execute a block of code when the

condition is false.

Syntax:

if (condition):

 # if Condition is true, Executes this block

else:

 # if condition is false, Executes this block

Example:

i=10;

if(i<20):

 print ("i is smaller than 20")

else:

 print ("i is greater than 25")

Output:

i is smaller than 20


c) Nested-if Statement:

A nested if is an if statement that is the target of another if statement. Nested if

statements means an if statement inside another if statement. Yes, Python allows us to

nest if statements within if statements. i.e, we can place an if statement inside another

if statement.

Syntax:

if (condition1):

 # Executes when condition1 is true

 if (condition2):

 # Executes when condition2 is true

 # if Block is end here

# if Block is end here

Example:

i =10

if(i ==10):

 # First if statement

 if(i < 20):

 print ("i is smaller than 20")

 # Nested - if statement will only be executed if statement above is true

 if (i < 15):

 print ("i is smaller than 15 too")

 else:

 print ("i is greater than 15")

Output:

i is smaller than 20

i is smaller than 15 too


Examples - 

1. Write a program to check whether a number is even or odd


a= int(input("Enter the number :"))
if(a%2==0):
    print(a,"is even number")
else:
    print(a,"is odd number")


3. Write a program to check the largest number among the three numbers


no1 = int(input("Enter first number : "))
no2 = int(input("Enter second number : "))
no3 = int(input("Enter third number : "))

if(no1>=no2 and no1>=no3):
    print(no1," is greatest")
elif(no2>=no1 and no2>=no3):
    print(no2," is greatest")
else:
    print(no3," is greatest")


4. Write a program to check if the input year is a leap year of not


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

if year % 4 == 0:
    print(year,"is Leap year..!!")
else:
    print(year,"is Not a leap year..!!")

5. Write a program that takes the marks of 5 subjects and displays the grade.


sub1=int(input("Enter first subject marks :"))
sub2=int(input("Enter first subject marks :"))
sub3=int(input("Enter first subject marks :"))
sub4=int(input("Enter first subject marks :"))
sub5=int(input("Enter first subject marks :"))

average_marks=(sub1+sub2+sub3+sub4+sub5)/5

if(average_marks>=75):
    print("You got A grade....!!!!!")
elif(average_marks>55):
    print("You got B grade....!!!!!")
elif(average_marks>35):
    print("You got C grade....!!!!!")
else:
    print("Failed")
Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Below Post Ad