Python Basics: Loops & Conditions
Python Loops & Conditions - Short Notes
Conditional Statements
- if condition:
# code
- if-else:
# code
- if-elif-else:
# multiple conditions
Example:
x = 10
if x > 0:
print("Positive")
elif x == 0:
print("Zero")
else:
print("Negative")
Logical Operators
- and: True if both are True
- or: True if at least one is True
- not: Inverts the condition
Looping
1. for loop
- Used to iterate over sequences like list, string, range
Example:
for i in range(3):
print(i)
2. while loop
- Runs until a condition becomes False
Example:
i=0
while i < 3:
print(i)
i += 1
range(start, stop, step)
- start: optional, default 0
- stop: required (not included)
- step: optional, default 1
break and continue
- break: exits the loop
- continue: skips current iteration
Nested Loops
for i in range(2):
for j in range(2):
print(i, j)
Mind Map:
Conditions & Loops
- Conditional Statements
- if
- if-else
- if-elif-else
- Logical Operators
- and
- or
- not
- Loops
- for loop
- while loop
- range(start, stop, step)
- nested loops
- Loop Control
- break
- continue