Basic Python Programming- 4
1. What is the difference between for and while loops?
Answer:The for loop is used when the number of iterations is known in
advance. It includes initialization, condition, and update in a single line.
It is best for iterating over sequences or ranges.
The while loop is used when the number of iterations is unknown and
depends on a condition. It continues executing as long as the condition
remains true.
2. Can we use ternary operators to replace an if-else statement?
Answer:Yes
3. How does the elif statement work in Python? Can we use it multiple
times?
Answer: The elif (else if) statement in Python is used for multiple
condition checks. It follows an if statement and is checked only if the
previous condition is False. If an elif condition is True, its block
executes, and the rest are skipped
Yes we can use it multiple times
4. How do we use the break statement to exit an infinite loop?
Answer:You can use the break statement to exit an infinite loop when
a certain condition is met.
while True:
num = int(input("Enter a number: "))
if num == 0:
print("Exiting loop...")
break
5. How can we iterate over a range in reverse order using range()?
Answer:You can iterate over a range in reverse order using range()
with a negative step.
For example
for i in range(10, 0, -1):
print(i)
6. Can we create an XOR gate using an if-else statement?
Answer:Yes
7. What is the difference between local and global variables in Python?
Answer:Local Variable: A variable declared inside a function and
accessible only within that function
Global Variable: A variable declared outside any function and
accessible throughout the program, including inside functions
8. What is the Big-O complexity of an if statement?
Answer:The Big-O complexity of an if statement is usually O(1)
(constant time) unless the condition involves loops or searches, which
can make it O(n) or more.
9. How does the time complexity of loops compare to an if-else
statement?
Answer:Loops generally have a higher time complexity than an
if-else statement.
● If-else statements usually run in O(1) (constant time) since they
execute a single condition check.
● Loops can run multiple times, leading to complexities like O(n) (linear
time) for a single loop or O(n²) (quadratic time) for nested loops.
10. How do we determine the efficiency of different loop structures?
Answer:Loop efficiency is determined by time complexity (Big-O):
● Single loop → O(n)
● Nested loops → O(n²) or more
● Logarithmic loops (e.g., binary search) → O(log n) (more efficient)
● Optimize by avoiding redundant iterations & unnecessary
operations
11.What happens if we use range(0,10,-1)?
Answer:range(0,10,-1) produces an empty sequence because the
step -1 moves backward, but the start 0 is already less than 10
12. Why does range(0,10,5) only output 0 5 and not 0 5 10?
Answer:
13. If a = 13, b = 20, c = 14, d = 12, how do we find the
minimum value using conditional statements?
Answer:
min_val = a
if b < min_val:
min_val = b
if c < min_val:
min_val = c
if d < min_val:
min_val = d
print(min_val)
14. Why does bwater = bwater + 1 increase the value of bwater?
Answer:bwater = bwater + 1 increases the value of bwater
because it takes the current value of bwater, adds 1 to it, and then
updates bwater with the new value. This is called incrementing a
variable
For eg -> bwater =5
bwater = bwater + 1 // now it will became 6