Python / Core Python Fundamentals Interview Questions
What do the pass, break, and continue statements do in Python loops?
These three keywords control loop flow in different ways, and confusing them is a common source of bugs.
pass: Does absolutely nothing. It is a syntactic placeholder used wherever Python requires a statement but you have nothing to write yet — an empty function body, an empty class, a stub except block. It is not a loop-control statement; it just lets the loop body be syntactically valid.
def todo():
pass # implement later
for i in range(5):
pass # loop runs 5 times doing nothingbreak: Immediately exits the innermost enclosing loop. Any else clause on the loop is skipped. Typical use: linear search where you want to stop as soon as you find the item.
for name in ['Alice', 'Bob', 'Carol']:
if name == 'Bob':
print('Found Bob')
breakcontinue: Skips the rest of the current iteration and jumps immediately to the next one. The loop itself keeps running.
for i in range(10):
if i % 2 == 0:
continue # skip even numbers
print(i) # prints 1 3 5 7 9A common interview trick question: what does pass do in a loop that also prints something before the pass? The answer is: the print still executes — pass only affects the statement position, not any previous code in the block.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
