Header Ads

If else Statement & if-elif-else statement in python with program Examples

If else Statement & if-elif-else statement  in python & its Program Examples with definitions:

if statement/condition:



Description:

    The if statement is the simplest form. It takes a condition and evaluates to either True or False. If the condition is True, then the True block of code will be executed, and if the condition is False, then the block of code is skipped, and The controller moves to the next line.

Examples: 

1)

a=10

b=20

if a<b:

  print("yes a is less then b",'A=',a,'B=',b)

2)

a=input("please enter one number")

b=input("please enter two number")

if a==b:

    print("yes a==b")

    # print(a,b)

3rd
a=input("please tell your mobile bettery %")
if int(a)>=80:
    print("The your cell is charged 80% ")


if else statement in python:


 Description:

The if-else statement checks the condition and executes the if block of code when the condition is True, and if the condition is False, it will execute the else block of code. 

Examples:

1)

a=input("Please enter your password to loing ")

if a=="Tom":

    print("Your are login")

else:

    print("you enter wrong password")

 

2) 

a=input("enter your brother age")

b=input("enter your sister age")

if a>b:

    print("your brother is elder ")

else:

    print("your sister is younger ")


Chain multiple if statement in python/if-elif-else statement:



Description:

In Python, the if-elif-else condition statement has an elif blocks to chain multiple conditions one after another. This is useful when you need to check multiple conditions.

With the help of if-elif-else we can make a tricky decision. The elif statement checks multiple conditions one by one and if the condition fulfills, then executes that code.

Example:

print("_____Welcome to Result show system_____")
a=input("Please Enter your Department Name=")
b=input("Please Enter your Roll Number=")
if b=='2k20/nf/it/07':
   print("Your name = Mr Kashif \n Your Department =",a,"\n Your roll number=",b,"\n you secure 3.5 CGPA")
elif b=='2k20/nf/it/10':
    print("Your name = Mr Mubeen \n Your Department =",a,"\n Your roll number=",b,"\n you secure 3.6 CGPA")
elif b=='2k20/nf/it/11':
    print("Your name = Mis Sadaf \n Your Department =",a,"\n Your roll number=",b,"\n you secure 3.8 CGPA")
else:
    print("Your Roll Number is Not Found ")

Nested if-else statement:


Description:

In Python, the nested if-else statement is an if statement inside another if-else statement. It is allowed in Python to put any number of if statements in another if statement.
Indentation is the only way to differentiate the level of nesting. The nested if-else is useful when we want to make a series of decisions.

Examples:

print("*****Welcome to TCSS service*****")
a=input("please Enter number of parsal ")
b=input("please enter sending province location")
if int(a)<=10:
    if b=="Sindh ":
        print("you will pay 10$ for entire sindh ")
elif int(a)<=20:
    print("you will pay 15$ for entire sindh ",)
elif int(a)<=30:
    print("you will pay 20$ for entire sindh ",)
else:
    print("you are entering out of order")

Try these programs in your Python IDLE.....

No comments

Powered by Blogger.