Python Interview Questions You'll Most Likely Be Asked
Python Interview Questions You'll Most Likely Be Asked
Questions
Review these typical interview questions and think about how you would
answer them. Read the answers listed; you will find best possible answers
along with strategies and suggestions.
This page is intentionally left blank
Chapter 1
goes here.
return
statement? Is this valid?
Answer:
Yes, this is valid. The function will then return a None object. The
end of a function is defined by the block of code being executed
(i.e., the indenting), not by any explicit keyword.
Python Looping
25: What happens if you put an else statement after a for block?
Answer:
The code in the else block is executed after the for loop completes,
unless a break is encountered in the for loop execution, in which
case the else block is not executed.
26: Explain the use of break and continue in Python looping.
Answer:
The break statement stops execution of the current loop, and
transfers control to the next block. The continue statement ends the
ps to the next iteration of the
loop.
31: Use a for loop and illustrate how you would define and print
the characters in a string out, one per line.
Answer:
formyChar in myString:
printmyChar
formyChar in myString:
break
printmyChar
33:
except for the spaces, using a for loop.
Answer:
formyChar in myString:
continue
printmyChar
i += 1
36: How is execution in the while loop block abandoned, but the
loop itself is not exited?
Answer:
The continue statement is used to terminate processing of the
block, and move control to the next iteration of the loop.
38: Can the else clause be used after a while loop? When is it
executed?
Answer:
Yes. The else block is executed after the while condition becomes
false, but not if the while loop is exited with a break statement.
39: Illustrate how the range() and len() functions will be used to
iterate over the indices of a sequence.
Answer:
for i in range(len(myItems)):
print i, myItems[i]