0% found this document useful (0 votes)
7 views1 page

For Loop

Uploaded by

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

For Loop

Uploaded by

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

FOR LOOP

INTERVIEW QUESTIONS

1. What is For Loop?


Ans:
A for loop is used for iterating over a sequence (that is either a list, a tuple, a
dictionary, a set, or a string).
This is less like the for keyword in other programming languages, and works more
like an iterator method as found in other object-orientated programming languages.
With the for loop we can execute a set of statements, once for each item in a list,
tuple, set etc.

fruits = ["apple", "banana", "cherry"]


for x in fruits:
print(x)

2. Can we apply Iteration on float number?


Ans:
You cannot directly iterate over a float number using a for loop.
for i in 3.14:
print(i)
This will raise a TypeError
TypeError: 'float' object is not iterable

3. What is range?
Ans:
In Python, range is a built-in function that generates an iterable sequence of
numbers, allowing you to loop over a specified range of values.

Syntax:
range(start, stop, step)

Parameters:
1. start: The initial value (inclusive).
2. stop: The final value (exclusive).
3. step: The increment between values (default is 1).

for i in range(5):
print(i) # 0, 1, 2, 3, 4

for i in range(2, 6):


print(i) # 2, 3, 4, 5
for i in range(0, 10, 2):
print(i) # 0, 2, 4, 6, 8
for i in range(10, 0, -2):
print(i) # 10, 8, 6, 4, 2

You might also like