Python Interview Questions and Answers- Conditional And Iterative Statements
Q.1 What a range() function does? Give an example.
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. its syntax is range(start, stop, step) e.g.
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. its syntax is range(start, stop, step) e.g.
x = range(3, 6)
for n in x:
print(n)
#This code will print 3 4 5
x = range(1, 10,2)
for n in x:
print(n)
#This code will print 3 4 5
Q.2 What are loops in Python? How many types of loop are there in Python?
Loops are iteration constructs in Python. Iteration means repetition of a set of statements depending upon a condition test. Loops has three basic elements within it to repeat the statements –
- Initialization (Start)
- Check Condition (Stop)
- Updation (Step)
Python provide two types of loop
- Conditional Loop while( (Condition based loop)
- Counting loop for (loop for a given number of times).
Q.3 What is the syntax of if-elif statement in Python?
The syntax of if-elif statement in python is as follows:
If condition1:
#code-block of statements when condition1 is true
elif condion2:
#code-block of statements when condition2 is true
elif condition3:
#code-block of statements when condition3 is true
Q.4 What are jump statements in Python? Name jump statements with example.
Python offers two jump statements to be used with in loops to jump out of loop-iterations.
These are break and continue statements.
The syntax of if-elif statement in python is as follows:
If condition1:
#code-block of statements when condition1 is true
elif condion2:
#code-block of statements when condition2 is true
elif condition3:
#code-block of statements when condition3 is true
The syntax of if-elif statement in python is as follows:
If condition1:
#code-block of statements when condition1 is true
elif condion2:
#code-block of statements when condition2 is true
elif condition3:
#code-block of statements when condition3 is true