ποΈ Python Interview Series - Part 2
π― Topics: Conditional Statements & Loops
π§βπΌ Interviewer: What are conditional statements in Python?
π¨βπ» Candidate:
Conditional statements allow us to execute specific blocks of code based on certain conditions.
Python uses if, elif, and else to control decision-making.
Example:
β Only one block executes depending on the condition that evaluates to True.
π§βπΌ Interviewer: Can you explain the difference between if and elif?
π¨βπ» Candidate:
if starts the conditional chain.
elif (short for else if) allows checking multiple conditions sequentially.
If none are True, the else block runs.
Example:
π§βπΌ Interviewer: Is there a way to write a single-line if statement in Python?
π¨βπ» Candidate:
Yes, we can use the ternary (conditional) expression.
Example:
This makes the code concise for simple conditions.
π§βπΌ Interviewer: What are loops in Python?
π¨βπ» Candidate:
Loops allow repeating a block of code multiple times.
Python supports two main loops:
- for loop β used to iterate over a sequence (like list, tuple, dict, string).
- while loop β runs as long as a condition is True.
π§βπΌ Interviewer: Can you explain how a for loop works in Python?
π¨βπ» Candidate:
A for loop iterates over any iterable object (like a list or string).
Example:
Here, Python automatically fetches each item from the list one by one β no index or counter is required (unlike C or Java).
π§βπΌ Interviewer: How does the range() function work in loops?
π¨βπ» Candidate:
range() generates a sequence of numbers and is often used for looping a fixed number of times.
Syntax:
Example:
Default values β start=0, step=1.
It doesnβt create a list; it returns a range object (saves memory).
π§βπΌ Interviewer: Whatβs the difference between for and while loops?
π¨βπ» Candidate:
- for loop: Used when we know how many times to iterate.
- while loop: Used when we donβt know the number of iterations β runs until the condition becomes false.
Example:
π§βπΌ Interviewer: What is the difference between break, continue, and pass statements?
π¨βπ» Candidate:
They control the loop flow:
Statement β Function
break β Exits the loop immediately
continue β Skips the current iteration and moves to the next
pass β Does nothing (placeholder for future code)
Example:
π§βπΌ Interviewer: What is a nested loop? Give an example.
π¨βπ» Candidate:
A nested loop means having a loop inside another loop.
Used for matrix traversal or pattern printing.
Example:
It executes the inner loop completely for each iteration of the outer loop.
π§βπΌ Interviewer: Can you use an else clause with loops?
π¨βπ» Candidate:
Yes. In Python, a loop can have an else clause that runs only if the loop completes normally (not terminated by break).
Example:
If break is used, the else block is skipped.
π§βπΌ Interviewer: How do we iterate through a dictionary?
π¨βπ» Candidate:
We can loop through its keys, values, or both:
π― Topics: Conditional Statements & Loops
π§βπΌ Interviewer: What are conditional statements in Python?
π¨βπ» Candidate:
Conditional statements allow us to execute specific blocks of code based on certain conditions.
Python uses if, elif, and else to control decision-making.
Example:
age = 18
if age < 18:
print("Minor")
elif age == 18:
print("Just eligible")
else:
print("Adult")
β Only one block executes depending on the condition that evaluates to True.
π§βπΌ Interviewer: Can you explain the difference between if and elif?
π¨βπ» Candidate:
if starts the conditional chain.
elif (short for else if) allows checking multiple conditions sequentially.
If none are True, the else block runs.
Example:
x = 0
if x> 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
π§βπΌ Interviewer: Is there a way to write a single-line if statement in Python?
π¨βπ» Candidate:
Yes, we can use the ternary (conditional) expression.
Example:
result = "Even" if num % 2 == 0 else "Odd"
This makes the code concise for simple conditions.
π§βπΌ Interviewer: What are loops in Python?
π¨βπ» Candidate:
Loops allow repeating a block of code multiple times.
Python supports two main loops:
- for loop β used to iterate over a sequence (like list, tuple, dict, string).
- while loop β runs as long as a condition is True.
π§βπΌ Interviewer: Can you explain how a for loop works in Python?
π¨βπ» Candidate:
A for loop iterates over any iterable object (like a list or string).
Example:
for i in [1, 2, 3]:
print(i)
Here, Python automatically fetches each item from the list one by one β no index or counter is required (unlike C or Java).
π§βπΌ Interviewer: How does the range() function work in loops?
π¨βπ» Candidate:
range() generates a sequence of numbers and is often used for looping a fixed number of times.
Syntax:
range(start, stop, step)Example:
for i in range(1, 6, 2):
print(i) # 1, 3, 5
Default values β start=0, step=1.
It doesnβt create a list; it returns a range object (saves memory).
π§βπΌ Interviewer: Whatβs the difference between for and while loops?
π¨βπ» Candidate:
- for loop: Used when we know how many times to iterate.
- while loop: Used when we donβt know the number of iterations β runs until the condition becomes false.
Example:
# for loop
for i in range(5):
print(i)
# while loop
i = 0
while i < 5:
print(i)
i += 1
π§βπΌ Interviewer: What is the difference between break, continue, and pass statements?
π¨βπ» Candidate:
They control the loop flow:
Statement β Function
break β Exits the loop immediately
continue β Skips the current iteration and moves to the next
pass β Does nothing (placeholder for future code)
Example:
for i in range(5):
if i == 2:
continue # skips 2
if i == 4:
break # stops loop
print(i)
π§βπΌ Interviewer: What is a nested loop? Give an example.
π¨βπ» Candidate:
A nested loop means having a loop inside another loop.
Used for matrix traversal or pattern printing.
Example:
for i in range(3):
for j in range(2):
print(i, j)
It executes the inner loop completely for each iteration of the outer loop.
π§βπΌ Interviewer: Can you use an else clause with loops?
π¨βπ» Candidate:
Yes. In Python, a loop can have an else clause that runs only if the loop completes normally (not terminated by break).
Example:
for i in range(3):
print(i)
else:
print("Loop finished")
If break is used, the else block is skipped.
π§βπΌ Interviewer: How do we iterate through a dictionary?
π¨βπ» Candidate:
We can loop through its keys, values, or both:
data = {"a": 1, "b": 2}
for key, value in data.items():
print(key, value)Do not forget to React β€οΈ to this Message for More Content Like this
@python_programming_42
#python_series #part1 #pythonprogramming
Thanks For Joining All β€οΈβ€3π1π₯1
Q: What does the input() function always return?
Anonymous Quiz
10%
a) int
4%
b) float
48%
c) string
38%
d) depends on user input
π2β€1
π Top 10 Python Tuple Operations You Must Know π¦π
1.
βΊ
2.
βΊ
3.
βΊ
4.
βΊ
5.
βΊ
6.
βΊ
7.
βΊ
8. * operator β Repeat tuple
βΊ
9.
βΊ
10.
βΊ
βΊ
π‘ Bonus: Tuples are immutable β they canβt be changed after creation.
tup = (1, 2, 3, 4, 2)
1.
len() β Get length of tuple βΊ
len(tup) β 52.
count() β Count occurrences of a value βΊ
tup.count(2) β 23.
index() β Find index of a value βΊ
tup.index(3) β 24.
in keyword β Check existence βΊ
2 in tup β True5.
for loop β Iterate through elements βΊ
for i in tup: print(i)6.
slicing β Access sub-parts βΊ
tup[1:4] β (2, 3, 4)7.
+ operator β Concatenate tuples βΊ
tup + (5, 6) β (1, 2, 3, 4, 2, 5, 6)8. * operator β Repeat tuple
βΊ
tup * 2 β (1, 2, 3, 4, 2, 1, 2, 3, 4, 2)9.
tuple() β Convert iterable to tuple βΊ
tuple([7, 8]) β (7, 8)10.
min() / max() β Get smallest/largest item βΊ
min(tup) β 1 βΊ
max(tup) β 4π‘ Bonus: Tuples are immutable β they canβt be changed after creation.
Do not forget to React β€οΈ to this Message for More Content Like this
Thanks For Joining All β€οΈβ€5π₯°1
β€3