We have compiled most frequently asked Python Interview Questions or Python If Else Questions which will help you with different expertise levels.
Python Interview Questions on Python Conditionals Or Interview Questions On Conditional Statements In Python
Question 1. What constitutes “True” in Python?
Answer:
A true expression is any expression that does not evaluate to 0, the empty list [ ], tuple ( ), dictionary { } or the objects None or False.
Question 2: What are the three main conditional statements in Python?
Answer:
if, elif, and else
Question 3: What are the comparison operators in Python?
Answer:
< Less than, > Greater than, <= Less than or equal to, >= Greater than or equal to, = Equal to, != not equal, o alternative not equal. Note a single = is NOT a Python comparison operator, it is an assignment operator only.
Question 4: Illustrate a basic if, elif, else structure.
Answer:
if <condition>:
. . .
elif<another condition>:
. . .
else:
. . .
- Python Program to Find nth Prime Number
- Python Data Persistence – Decision Control
- Python Program to Calculate BMI
Python Viva Questions or Python Viva Questions And Answers
Question 5: In Python 2.5+, the equivalent of a tertiary operator has been added to the language. Provide an example of its use.
Answer:
myValue = ‘Positive’ if myNumber > 0 else ‘Negative or Zero’
Question 6: What does elif mean?
Answer:
It means else if. It is used after an if statement, to do another comparison.
Question 7. What would the output be from the following code? a =4 If a = 5:
Print “True”
Else:
Print “False”
Answer:
This is a trick question. The a = 5 is not a comparison operator, but an assignment. It will yield “True”. The correct coding would be a == 5.
Question 8: How are if, elif, and else blocks defined?
Answer:
All blocks in Python are defined by indenting. All lines of a particular code block must have the same level of indenting.
Question 9: Illustrate a switch-case equivalent using if-elif-else.
Answer:
if item=valueA:
. . .
elif item == valueB:
. . .
elif item = = valueC:
. . .
elifitem = valueN:
. . .
else:
… #default code
Question 10: How is the Python switch statement used?
Answer:
This is a trick question, there is no built-in switch statement in Python, which is unusual. A switch statement can be easily created using if-elif using lambda or with Python dictionaries.
Question 11: Using a dictionary, create an equivalent to a switch case statement.
Answer:
deffunc1( ):
. . .
deffunc2( ):
. . .
switch = {
‘Aardvark’: fund1,
‘Armadillo’: fund2,
}
mySwi tchKey= “Armadillo ”
switch[mySwitchKey]( ) #callsJunc2( )
switch[‘Aardvark’]( ) #calls func1( )
Question 12: Illustrate comparing two strings for equality in a case insensitive manner.
Answer:
if stringl. lower ( ) = string2.lower ( ):
#Note: .upper( ) is equally valid.
Question 13: Illustrate comparing two strings, printing if the first string is longer, equal, or shorter than the second string.
Answer:
if len(stringl) > len(string2):
print “Stringl is longer than string2.”
elif len(stringl) < len(string2):
print “String1 is shorter than string2.”
else:
print “String1 is the same length as string2.”
Question 14: When comparing two d^tes, what method is used?
Answer:
Date.toordinal( ) Otherwise, Python would compare the dates by their object address.
Question 15: In comparing dates and DateTime objects, what happens when one comparand is naive and the other aware?
Answer:
A TypeError is raised.
Question 16: What happens when you try to compare a DateTime object with other classes of objects?
Answer:
A TypeError is raised.
Question 17: When are dictionaries considered equal?
Answer:
If and only if their sorted lists compare equally.
Question 18: How is collection membership determined?
Answer:
Using the in and not in operators.
Question 19: Illustrate how collection membership determination would be written.
Answer:
if x in collection:
print “It is in the collection”
else:
print “Not in the collection.”
Question 20: How is object identity tested? Illustrate with an example.
Answer:
Using the is and is not operators.
if x is objecttype:
print “x is the type you thought it was.
else:
print “x isn’t an objecttype.”
Recommended Reading On: Python Interview Questions and Answers for Freshers & Experienced
Conclusion
There are lot of chances to ask from any corner of the python language so, the questions not only present in the above pattern but also starts with Python If Statement Questions, Python Conditional Statements Questions, Python3 Switch Case, If Elif Else Questions In Python, If Statement In Python Questions, Python Programming Viva Questions, Or interrogative way like Viva Questions For Python, Viva Questions On Python.