2 - Aug - Condition Statement
2 - Aug - Condition Statement
if-else
if-elif-else
nested if-else
In [ ]:
In control statements, 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
In [ ]:
In [1]: number = 6
if number > 5: # condition
print(number * number) #statement
print('Next lines of code')
36
Next lines of code
In [ ]:
In [ ]:
if else
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.
In [4]: password = input('Enter password ')
if password == "python":
print("Correct password")
else:
print("Incorrect Password")
print('Next lines of code')
In [ ]:
In [ ]:
In [5]: number = 6
if number > 5: # condition
print(number * number) # statement
else:
print("nothing to print")
print('Next lines of code')
36
Next lines of code
In [ ]:
In [ ]:
if elif else
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.
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: a = 9
b = 8
c = 3
d = 2
if a>b:
print("a is smaller than b")
if c>d:
print("c is greater than d")
else:
print("c is smaller than d")
else:
print("a is greater than b")
print("Rest of the code")
In [ ]:
In [ ]:
In [ ]: number = 5
# outer if statement
if (number >= 0):
# inner if statement
if number == 0:
print('Number is 0')
In [ ]:
Q1. write a programme to check whether a person is eligible for voting or not.
In [ ]:
Q3. write a programme to check whether a number entered by user is even or odd.
In [ ]:
In [ ]:
Question 6:
else = fail
In [ ]: A = int(input("enter the percentage : "))
if A>=70:
print('first division')
elif A>=50 and A<=70:
print('second divison')
elif A>=35 and A<=50:
print("third divison")
else:
print("fail")
In [ ]:
In [ ]:
Question 7 :
age 6 to 14 = free
15 to 25 = 50% dis
26 to 36 = 20% dis
In [ ]:
In [ ]:
In [ ]:
Question 8 :
take two integer input from the user and one operation sign
"+" = a+b
"-" = a-b
"" = ab
"/" = a/b
In [ ]:
9.Take values of length and breadth of a rectangle from user and check if it is square or
not.
In [5]: leng = int(input("enter length = "))
bre = int(input("enter breadth = "))
if leng==bre:
print("square")
else:
print("rectangle")
enter length = 5
enter breadth = 5
square
In [ ]:
10 Take two int values from user and print greatest among them.
In [4]: num1=int(input("enter first number:"))
num2=int(input("enter second number:"))
if(num1>num2):
print("FIRST NUMBER IS GREATER")
elif(num1<num2):
print("SECOND NUMBER IS GREATER")
else:
print("THEY ARE EQUAL")
In [ ]:
In [ ]:
a. Below 25 - F
b. 25 to 45 - E
c. 45 to 50 - D
d. 50 to 60 - C
e. 60 to 80 - B
f. Above 80 - A
In [ ]:
12. Take input of age of 3 people by user and determine youngest among them.
In [ ]: a=int(input("enter the age of first person:-"))
b=int(input("enter the age of second person:-"))
c=int(input("enter the age of third person:-"))
if(a<b and a<c):
print("person 1 is youngest")
elif(b<c and b<a):
print("person 2 is youngest")
else:
print("person c is youngest")
In [ ]:
In [ ]:
enter age1: 6
enter age2: 5
enter age3: 5
all are equal
In [ ]: