Python Unit1 Notes QA Format
Python Unit1 Notes QA Format
The 'if' statement is used to check a condition. If the condition is true, the block of code under it will
run.
Example:
if x > 0:
print("Positive")
The 'if-else' statement runs one block if the condition is true, and another block if it is false.
Example:
if x > 0:
print("Positive")
else:
print("Not Positive")
'elif' means 'else if'. It is used to check multiple conditions one by one.
Example:
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
Example:
if x >= 0:
if x == 0:
print("Zero")
else:
print("Positive")
Example:
i=1
while i <= 5:
print(i)
i += 1
Example:
for i in range(5):
print(i)
Example:
for i in range(10):
if i == 5:
break
print(i)
'continue' skips the current iteration and continues with the next one.
Example:
for i in range(5):
if i == 2:
continue
print(i)
Example:
for i in range(2):
for j in range(2):
print(i, j)