Python · Module 2Module material
Control Flow interview questions
25 questions interviewers ask on this module’s topics, from first principles to the follow-ups. Each comes with a model answer.
Conditions
- How does Python know where an
ifblock ends?● - Which values are falsy in Python?●
- What is wrong with
if day == "Saturday" or "Sunday":?●● - What is the difference between a chain of
if/elifand a sequence of separateifstatements?●● - Why does the order of the conditions in an
elifchain matter?●● - What is the conditional expression, and when should you not use it?●
- What does
x = name or "Guest"do, and how does it differ fromx = name if name else "Guest"?●● - How can a condition avoid dividing by zero?●●
- When would you use
matchinstead ofif/elif?●● - What is the most common mistake with
match?●●
while loops
- When do you use
whilerather thanfor?● - How do you make sure a
whileloop terminates?● - Can a
whileloop's body run zero times? When exactly does the loop stop?●● - What is a sentinel value?●●
- What is the walrus operator, and what goes wrong without brackets?●●
for loops
- What is an iterable?●
- What does
rangereturn, and why islen(range(10**12))fast?●● - Why is
for i in range(len(items)):discouraged?●● - What does
zipdo when its arguments have different lengths?●● - After
for steps in week:finishes, doesstepsstill exist? Does assigning to the loop variable inside the body change the loop?●● - How many times does a nested loop's inner body run?●●
break, continue and else
- What is the difference between
break,continueandpass?● - Does
breakin a nested loop leave both loops?●● - When does a loop's
elseblock run?●● - When is
while True:withbreakthe right form?●●