An if statement lets your program decide. It checks whether something is true, and only runs a block of code when it is.
temperature = 35
if temperature > 30:
print("It is hot today.")If the temperature is above 30, the message prints. If not, that line is skipped entirely and the program carries on.
Conditions are just questions with yes/no answers#
Everything inside an if boils down to a true or false value — a boolean. You can print one directly to see it:
print(10 > 3) # True
print(10 == 3) # False
print("a" == "a") # TrueThe comparison operators you will use constantly:
| Operator | Means | Example |
|---|---|---|
== |
is equal to | age == 18 |
!= |
is not equal to | name != "" |
> < |
greater / less than | score > 50 |
>= <= |
greater / less than or equal | age >= 18 |
Adding an alternative with else#
else covers every case the if did not.
age = 15
if age >= 18:
print("You can vote.")
else:
print("Not yet — come back later.")Exactly one of those two blocks runs. Never both, never neither.
More than two paths#
Use elif in Python (else if in JavaScript) to chain options. The first matching branch wins and the rest are skipped.
score = 74
if score >= 90:
grade = "A"
elif score >= 75:
grade = "B"
elif score >= 60:
grade = "C"
else:
grade = "Needs work"
print(grade) # COrder matters. If you put score >= 60 first, a score of 95 would match it and stop there, and nobody would ever get an A.
The same thing in JavaScript:
let grade;
if (score >= 90) {
grade = "A";
} else if (score >= 75) {
grade = "B";
} else {
grade = "Needs work";
}Combining conditions#
- and — both must be true
- or — at least one must be true
- not — flips true and false
age = 22
has_ticket = True
if age >= 18 and has_ticket:
print("Come in.")
if not has_ticket:
print("You need a ticket.")JavaScript uses &&, || and ! for the same three ideas.
Indentation and braces#
Python decides what belongs inside the if purely by indentation. This is why misaligned spaces cause IndentationError.
if logged_in:
print("Welcome") # inside the if
print("Goodbye") # always runsJavaScript uses braces instead, and ignores your spacing. Omitting the braces around a single statement is legal but a classic source of bugs, so most teams always use them.
A worked example#
password = input("Enter your password: ")
if len(password) < 8:
print("Too short — use at least 8 characters.")
elif password.isdigit():
print("Numbers only is easy to guess. Add letters.")
elif password.lower() == "password":
print("Please, no.")
else:
print("That will do.")Notice the checks run cheapest-and-most-likely first. That is a habit worth building.
Questions people ask#
Can I nest an if inside another if?
Yes, and sometimes it is the clearest option. But three or four levels deep gets hard to read. Often you can flatten it by combining conditions with and, or by returning early from a function.
What counts as true if I do not use a comparison?
Most languages treat empty things as false: an empty string, an empty list, the number zero, and None or null. Everything else is true. So if name: is a compact way of asking “did they type anything?”.
What is a ternary or one-line if?
A short form for choosing between two values. Python writes it status = "adult" if age >= 18 else "minor". It is fine for simple choices and unreadable for complicated ones.
Why does my condition never run?
The usual cause is comparing text to a number — input() returns text, so "7" == 7 is false. Print the value and its type just before the if to check. That habit is covered in how to read programming errors.
Where to go next#
- Loops explained — repeating work without repeating yourself
- Python if/else in more depth