1
1
programming languages. The decisions are made on the evaluated result of the
conditions.
If
Elif
Else
Nested if
Short hand if
Short hand if …else
If Statement:
--------------
If certain conditions are true we direct the program to do one course of actions.
If certain conditions are false we direct the program to do some other course of
action.
Syntax:
--------
if cond:
#Statements to be executed if the condition is true
Programs:
----------
a=10
if a>0:
print("The number is positive")
--------------------------------------------------
age=21
if age>=18:
print("Person is eligible for voting")
But there can be cases when the condition is False.
==================================================
2. if-else:
------------
a=10
if a>0:
print("The number is positive")
else:
print("Negative")
------------------------------------
age=16
if age>=18:
print("Person is eligible for voting")
else:
print("Person is not eligible")
--------------------------------------------------
5. Pgm to compare two values based on a condition and display the greater one.
a=4
b=9
if a>b:
print("a is positive")
else:
print("b is positive")
------------------------------------------------------------
a and b are variables, so in order to replace them using the values, we use format
strings.
In format strings, the print function starts with f and the variables are written
inside the curly braces.
a=4
b=9
if a>b:
print(f"{a} is positive")
else:
print(f"{b} is positive")
Now, instead of taking numbers directly, we can take numbers from user, so use
input() function to take input from user and the input must be converted into its
respective datatype.
a=int(input("Enter num1"))
b=int(input("Enter num2"))
if a>b:
print(f"{a} is positive")
else:
print(f"{b} is positive")
----------------------------------------------------
6. Write a program to validate a password.
password=input("enter password:")
if password=="123":
print("password is valid")
else:
print("password is invalid, try again..!")
Indentation in Python:
-----------------------
Python doesn't allow the use of parentheses for the block level code.
The statements with in the same indentation block is called as a suite. All the
statements of one block are intended at the same level indentation.
==========================================================
If we want to write the codes in a single line for readability and to make it
concise, we shall use short hand if else.
syntax:
--------
7. Write a program using short hand if else to find the greater number among two.
------------------------------------------
password=input("enter password:")
print("password is valid") if password=="123" else print("password is invalid, try
again..!")
===============================================
3. Nested if:
--------------
When we have a condition that depends on another condition, we use nested if.
username=input("enter username:")
password=input("enter password:")
if username=="admin":
print("account is existing")
if password=="abc":
print("welcome")
else:
print("Incorrect password")
else:
print("account doesnt exist")
print("The username is invalid")
----------------------------------------------
a=int(input("Enter Num1")) # 12
b=int(input("Enter Num2")) # 10
c=int(input("Enter Num3")) # 18
if a>b:
if a>c:
print(f"{a} is greater")
else:
print(f"{c} is greater")
elif b>c:
print(f"{b} is greater")
else:
print(f"{c} is greater")
===================================================
#Elif:
------
a=10
b=8
if a>b:
print(" A is Gr")
if a<b:
print("B is Gr")
if a==b:
print("A eq B")
if a!=b:
print("A not Eq B")
a=10
b=8
if a>b:
print(f"{a} is greater")
elif a<b:
print(f"{b} is greater")
elif a==b:
print(f"{a} is equal to {b}")
else:
print(f"{a} is not equal to {b}")
marks=int(input("enter marks"))
if marks>=90:
print("grade A")
if marks>=80:
print("grade B")
if marks>=70:
print("grade C")
if marks>=60:
print("grade D")
if marks>=50:
print("grade E")
if marks<50:
print("Fail")
Here, multiple grades are assigned to the student. lets try to change the code.
marks=int(input("enter marks"))
if marks>=90:
print("grade A")
if marks<50:
print("Fail")
Now we got the correct output, but the prblm here is we need lot of conditions and
it may lead to a confusion while writing conditions. it is a limitation here, so we
use another conditional construct called "elif"
marks=int(input("enter marks"))
if marks>=90:
print("grade A")
elif marks>=80:
print("grade B")
elif marks>=70:
print("grade C")
elif marks>=60:
print("grade D")
elif marks>=50:
print("grade E")
else:
print("Fail")
========================================================
Lab Activities with Solutions:
--------------------------
=============================================
score=int(input('Enter ur score'))
gender=input("Enter ur Gender(M/F)")
Region=input("Enter ur Region")
========================================================
3. Write a code using short hand if else to tell if a person is eligible to vote or
not.
age = 19
status = "Can Vote" if age >= 18 else "Cannot vote"
print(status)
========================================================
4. WAP to read three numbers from user, if any of three numbers are 0, it should
display the number must be more than 0.
The application has to display after comparing the numbers,
"Entered three numbers are Equal” if all three entered numbers are
equal,
"Entered three numbers are different" if all three numbers are
different and it should display
"Neither all are equal or different" otherwise
print("Enter Num1")
n1=int(input())
print("Enter Num2")
n2=int(input())
print("Enter Num3")
n3=int(input())
if n1==0 or n2==0 or n3==0:
print("Values should be non zero")
elif n1==n2 and n1==n3:
print("All Values are equal")
elif n1==n2 or n1==n3 or n3==n2:
print("Neither all are equal or different")
else:
print("All numbers are different")
==========================================================
5. WAP to read three numbers from user and should evaluate whether
those numbers as increasing order or in decreasing order.
The program has to display
"Increasing Order” if numbers are in increasing order
The program has to display
"Decreasing Order” if numbers are in decreasing order
Otherwise, it should display as
"Not in increasing and decreasing order"
Testcase1
Enter number1: 1234
Enter number2: 3456
Enter number3: 5678
Increasing Order
Testcase2
Enter number1: 1234
Enter number2: 1221
Enter number3: 5678
Not in increasing order and not in decreasing order
print("Enter Num1")
n1=int(input())
print("Enter Num2")
n2=int(input())
print("Enter Num3")
n3=int(input())
if n1==0 or n2==0 or n3==0:
print("Values should be non zero")
elif n1<n2 and n2<n3:
print("Increasing Order")
elif n1>n2 and n2>n3:
print("Decreasing Order")
else:
print("Not in increasing and decreasing order")
=================================================================