By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Without conditional logic, every program runs the same way regardless of its input. You need a way to make your code behave differently based on the data it receives. Python's if, elif, and else statements let you branch execution based on conditions, and loops let you repeat code without writing it out manually.
The basic structure is straightforward. You write a condition, and if it's true, Python runs that block. If it's false, it checks the next condition. This continues until one condition matches or you hit the else block, which always runs if nothing before it matched.
score = 85
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
else:
grade = "F"
print(f"Grade: {grade}") # Grade: BNotice the indentation in that example. Python uses 4 spaces to define what belongs inside each block, not curly braces like other languages. This is non-negotiable—get the spacing wrong and your code won't run.
Another critical detail: only the first matching condition executes. Once Python finds a true condition, it skips every other elif and the else entirely. This means order matters. If you put a broad condition first, more specific ones below it will never run.
Often you need to check multiple things at once. Python gives you three logical operators: and, or, and not. Use and when both conditions must be true. Use or when at least one needs to be true. Use not to flip a boolean value.
age, has_id = 20, True
if age >= 18 and has_id:
print("Entry allowed")
if age < 13 or age > 65:
print("Discount applies")
if not has_id:
print("Show your ID")These operators let you write complex conditions without nesting too many if statements. A condition like age >= 18 and has_id is more readable than nested if blocks checking the same things.
Loops let you run the same code multiple times without rewriting it. Python has two main types: for loops and while loops.
# for loop over a range
for i in range(5): # 0,1,2,3,4
print(i)
# for loop over a list
langs = ["Python","JS","Rust"]
for lang in langs:
print(lang)
# while loop
count = 3
while count > 0:
print(count)
count -= 1A for loop iterates over a sequence—a range of numbers, items in a list, or characters in a string. Each iteration, the loop variable takes on the next value. When you know how many times you need to repeat something, for loops are your go-to.
A while loop keeps running as long as a condition is true. You're responsible for changing something inside the loop to eventually make the condition false, or you'll have an infinite loop. While loops are useful when you don't know in advance how many iterations you need.
Break stops the loop entirely and moves to the code after it. Continue skips the rest of the current iteration and jumps to the next one. Both are useful for handling special cases without restructuring your whole loop.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.