0% found this document useful (0 votes)
15 views3 pages

Interview Questions 18 10

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)
15 views3 pages

Interview Questions 18 10

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/ 3

Interview Questions:

Q. 1 What is while loop?


Ans:
A while loop in Python is a control structure that repeatedly executes a block of
code as long as a specified condition is true.

Syntax:
Initialization
while condition:
# Block of while loop
#Update

For Example:
i = 0
while i < 5:
print(i)
i=i+1

Output:

0
1
2
3
4

Q.2 What is difference between while and for loop?


Ans:
While Loop:

1. Repeats a block of code while a condition is true.


2. No fixed number of iterations.
3. Condition evaluated before each iteration.

Syntax:
Initialization
while condition:
# Block of while loop
#Update

For Loop:

1. Repeats a block of code for a specified number of iterations.


2. Iterations based on a sequence.
3. Condition evaluated once, before loop starts.

Syntax:
#For Loop

for var in iterable:

Q.3 What is the purpose of


a.pass
b.continue
c.break

Ans:
a. pass

Purpose: Do nothing, skip the current iteration.

Usage: Used as a placeholder when a statement is required syntactically.

for i in range(5):
if i == 3:
pass # Skip iteration when i is 3
print(i)

b. continue

Purpose: Skip the current iteration and move to the next.

for i in range(5):
if i == 3:
continue # Skip printing when i is 3
print(i)

c. break

Purpose: Exit the loop entirely.

for i in range(5):
if i == 3:
break # Exit loop when i is 3
print(i)

Q.4 How to iterate char from string by using while loop?

Ans:
You can iterate characters from a string using a while loop by maintaining an index
variable.

name="The Kiran Academy"

char=0

while char<len(name):
print(name[char])
char=char+1

# T
# h
# e

# K
# i
# r
# a
# n

# A
# c
# a
# d
# e
# m
# y

You might also like