0% found this document useful (0 votes)
2 views

ControlFlowStatementsInterviewQues

Uploaded by

raankirakshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

ControlFlowStatementsInterviewQues

Uploaded by

raankirakshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.What are control flow statements in Python?

Control flow statements are used to dictate the order in which code is executed. In Python, they
include conditional statements (if, elif, else), loops (for, while), and flow control modifiers (break,
continue, pass).

2.How does a for loop work in Python?

A for loop in Python iterates over a sequence (like a list, tuple, or string) in the order that the
items appear in the sequence. The syntax is for item in sequence: do_something().

3.What is the difference between for and while loops?

A for loop iterates over a sequence of elements and is typically used when the number of
iterations is known or finite. A while loop continually executes as long as a given condition is
true and is used when the number of iterations is not known beforehand.

4.How do you use an else clause with loops in Python?

An else clause can be used with for and while loops in Python. It is executed when the loop
finishes normally (i.e., not terminated by a break statement).

5.What does the break statement do?

The break statement is used to exit a for or while loop immediately, regardless of the loop's
condition. It is typically used in nested loops or when a certain condition is met.

6.What is the use of the continue statement in Python?

The continue statement is used to skip the rest of the code inside a loop for the current iteration
and move to the next iteration.

7.What is the purpose of the pass statement?

The pass statement is a null operation in Python; it is used when a statement is syntactically
required but you do not want any command or code to execute.

1
8.Explain the range function in loops.

The range() function generates a sequence of numbers. It is commonly used in for loops for
specifying the number of iterations. It can take one, two, or three parameters: start, stop, and
step.

9.How does a nested loop work in Python?

A nested loop is a loop inside another loop. The inner loop completes all its iterations for every
single iteration of the outer loop.

10.What are conditional statements in Python?

Conditional statements are used to execute different blocks of code depending on different
conditions. In Python, these are if, elif (else if), and else statements.

11.How can you implement a switch-case statement in Python?

Python does not have a built-in switch-case construct. However, you can implement a similar
structure using a series of if, elif, and else statements, or using a dictionary to map cases to
functions.

12.What is short-circuit evaluation in logical operations?

Short-circuit evaluation is a technique where the interpreter stops evaluating a logical


expression as soon as the overall truth value is determined. In Python, it happens in
expressions using and and or operators.

13.Can you use a for loop to iterate over a dictionary?

Yes, you can iterate over the keys of a dictionary by using a for loop directly on the dictionary, or
you can iterate over both keys and values by using .items().

14.What is list comprehension and how does it relate to control flow?

List comprehension is a concise way to create lists that consist of brackets containing an
expression followed by a for clause, and optionally, if clauses. It's a way to control the flow of
data and generate lists efficiently.

15.How do you use the enumerate function in loops?

The enumerate function adds a counter to an iterable and returns it in an enumerate object. This
can be used in a for loop to get both the index and the value of each item in the sequence.

2
16.What is recursion and how is it used in Python?

Recursion is a technique in which a function calls itself as a subroutine. This can be a powerful
tool in Python for solving problems that can be broken down into simpler, repetitive tasks.

17.What is an infinite loop and how can it be caused?

An infinite loop runs continuously, as its terminating condition is never reached. It can be caused
by a while loop with a condition that always evaluates to True.

18.How can you loop through multiple lists at the same time?

You can loop through multiple lists simultaneously by using the zip function, which aggregates
elements from each of the iterables.

19.What is the global keyword and when is it used?

The global keyword in Python is used inside a function to refer to a global variable outside of the
function's scope. It allows you to modify the variable outside of the current function.

20.What are iterators and generators in Python?

Iterators are objects that can be iterated over in a loop. A generator is a function that produces a
sequence of results instead of a single value and is used to create iterators.

You might also like