Learn Python if elif else
- Categories Python if-else, Python, Python
In this post we will cover if-elif-else in Python. You can watch our videos on if-elif-else in Python – Click Here
Q. 1 What is an if statement?
Ans.
- The if statement contains a logical expression using which a decision is made based on the result of the comparison.
- Syntax:
- if expression:
statement(s) if expression:
statement(s)
else:
statement(s)if expression1:
statement(s)
elif expression2:
statement(s)
else:
statement(s)
- if expression:
- If the boolean expression evaluates to TRUE, then the block of statement(s) inside the if statement is executed.
- If boolean expression evaluates to FALSE,
- The first set of code after the end of the if statement(s) is executed
- Or the else block is executed
- Or the next elif block is executed.
- If the boolean expression evaluates to TRUE, then the block of statement(s) inside the if statement is executed.
- If boolean expression evaluates to FALSE,
- The first set of code after the end of the if statement(s) is executed
- Or the else block is executed
- Or the next elif block is executed.
Q. 2 Which are two types of else clauses in Python?
Ans. The two types of python else clauses are:
else in an if statement– the else clause of an if statement is executed when the condition of the if statement results into false
else in a loop statement-The else clause of a loop is executed when the loop is terminating normally. i.e. when its test- condition has gone false for a while loop or when the for loop has executed for the last value in sequence
Q. 3 Is there any limit of statement that can appear under an if block.
Ans. No
Q.4 What is pass in Python?
Ans. In python pass statement does nothing. It is just an indication to interpreter to move to next executable statement.
Q. 5 What is the output of following code ?
if(4+5==10):
print(“True”)
else:
print(“False”)
print(“True”)
Ans. False
True
Q. 6 What is the output of the following code if a=1 and a =4
if(a==0):
print(“zero”)
if(a==1):
print(“one”)
else :
print(“a > 1”)
Ans.
- one
- a > 1
Q. 7 Under what conditions will this code fragment will print ”water”
if temp < 32:
print("ice")
elif temp < 212:
print("water")
else:
print("steam")
Ans. if temp greater than > 32 and less than < 212 it will print water
Q. 8 What is the output produced by the following code?
x=1
if x>3 :
if x>4:
print(“a”, end=”)
else:
print(‘b’,end=’ ‘)
elif x < 2:
if(x != 0):
print(“c”, end=’ ‘)
print(“d”)
Ans. c d
Q.9 What is the error in following code ? Correct the code?
weather='raining'
if weather = "sunny":
print("wear sunblock")
elif weather="snow":
print("going skiing")
else:
print(weather)
Ans.
weather='raining'
if weather == "sunny":
print("wear sunblock")
elif weather=="snow":
print("going skiing")
else:
print(weather)
>>>raining
Q. 10 What is the error in following code ? Correct the code?
n=2
if n == 0
print("zero")
elif : n==1
print("one")
elif n==2:
print("two")
else n==3:
print("three")
Ans.
n=2
if n == 0:
print("zero")
elif n==1:
print("one")
elif n==2:
print("two")
else:
print("three")
Q. 11 Write a program to check if the number is buzz nos or not .
(BUZZ nos are numbers which are divisible by 7 or end with 7)
Ans.
n=int(input("Enter a number:"))
if(n % 7 == 0 or n % 10 == 7):
print (("It’s a BUZZ no"))
else:
print ("Not a BUZZ no")
Q. 12 Write a program to check if the character is a vowel or not?
Ans.
ch=input("Enter a character:")
if ch in "aeiouAEIOU":
print("It’s a vowel")
else:
print ("Not a vowel")
Q. 13 Predict the output of the program
a = 10
if 5 < a < 15 :
print (a / 2)
else:
print (a / 5)
Ans. >>>5.0
Q. 14 Write a program to accept percentage from the user and display the grade according to the following criteria:
Marks Grade
> 90 A
> 80 and <= 90 B
>= 60 and <= 80 C
below 60 D
Ans.
per = int(input("Enter marks"))
if per > 90:
print("Grade is A")
if per > 80 and per <=90:
print("Grade is B")
if per >=60 and per <= 80:
print("Grade is C")
if per < 60:
print("Grade is D")
Q.15 Write a program to check whether an years is leap year or not.
Ans.
yr=int(input("Enter the year"))
if yr%100==0:
if yr%400==0:
print("Entered year is leap year")
else:
print("Entered year is not a leap year")
else:
if yr%4==0:
print("Entered year is leap year")
else:
print("Entered year is not a leap year")
Q. 16 Accept three numbers from the user and display the second largest number.
Ans.
num1=int(input("Enter first number"))
num2=int(input("Enter second number"))
num3=int(input("Enter third number"))
if (num1 > num2 and num1 < num3) or (num1 < num2 and num1 > num3):
print("Middle number is " , num1)
if (num2 > num1 and num2 < num3) or (num2 < num1 and num2 > num3):
print("Middle number is" , num2)
if (num3 > num2 and num3 < num1) or (num3 < num2 and num3 > num1):
print("Middle number is" , num3)
Q.17 Write a Python program to test if a variable is a list or tuple or a set.
Ans.
#x = ['a', 'b', 'c', 'd']
#x = {'a', 'b', 'c', 'd'}
x = ('tuple', False, 3.2, 1)
if type(x) is list:
print('x is a list')
elif type(x) is set:
print('x is a set')
elif type(x) is tuple:
print('x is a tuple')
else:
print('Neither a list or a set or a tuple.')
Q. 18 Consider the following code: If y has the value 2 after executing the above program fragment, then what do you know about the initial value of x?
if x > 3:
if x <= 5:
y = 1
elif x != 6:
y = 2
else:
y = 3
else:
y = 4
Ans. x has to be >= 7 for y =2