Python Worksheet Answers
1. Explain Boolean operators with an example.
Boolean operators combine conditions and return True or False. The main Boolean
operators are and, or, and not.
Example:
a=5
b = 10
print(a < b and b > 5)
2. What do you mean by iterative statements? Give an example.
Iterative statements repeat a block of code multiple times.
Example:
for i in range(5):
print(i)
3. Distinguish between the for and while loop.
For loop: Used when the number of repetitions is known.
While loop: Used when a loop runs based on a condition.
4. State the use of the membership operators in Python.
Membership operators check whether a value exists in a sequence such as a list or string.
The operators are 'in' and 'not in'.
Example:
x = [1, 2, 3]
print(2 in x)
5. What is an infinite loop? How is it created?
An infinite loop is a loop that runs continuously and never stops because the condition
always remains true.
Example:
while True:
print('Hello')