04: Condiții
Control what your program does based on conditions with if, elif, and else.
Lesson 04 · Conditions¶
What you'll learn
- The
if / elif / elsestructure - Comparison operators:
==,!=,<,>,<=,>= - Logical operators:
and,or,not - Nested conditions
The if statement¶
if lets you run code only when a condition is true:
Output:
If temperature were 20, nothing would be displayed.
Indentation is mandatory
The code inside if must be indented by 4 spaces (or one Tab). Python uses indentation to know what belongs to the block.
if / else¶
else runs when the if condition is false.
if / elif / else¶
elif (short for "else if") checks a new condition if the previous one was false:
grade = 8.5
if grade >= 9.5:
print("Excellent — rating: Outstanding")
elif grade >= 8.5:
print("Great — rating: Very good")
elif grade >= 7:
print("Satisfactory — rating: Good")
elif grade >= 5:
print("Rating: Sufficient")
else:
print("Rating: Insufficient")
Python checks the conditions top to bottom and stops at the first true one.
Comparison operators¶
| Operator | Meaning | Example | Result |
|---|---|---|---|
== |
equal to | 5 == 5 |
True |
!= |
different from | 5 != 3 |
True |
< |
less than | 3 < 5 |
True |
> |
greater than | 5 > 3 |
True |
<= |
less than or equal | 5 <= 5 |
True |
>= |
greater than or equal | 6 >= 5 |
True |
Logical operators¶
and — both conditions must be true¶
age = 16
has_license = False
if age >= 18 and has_license:
print("Can drive.")
else:
print("Cannot drive.")
or — at least one must be true¶
not — negates the condition¶
Nested conditions¶
You can place an if inside another if:
score = 85
level = "advanced"
if score >= 50:
print("You passed the test.")
if level == "advanced":
print("You earned the advanced certificate!")
else:
print("You earned the basic certificate.")
else:
print("You did not pass the test.")
Exercises¶
Exercise 1 — Positive, negative, or zero?¶
Ask for a number and display whether it's positive, negative, or zero.
Solution
Exercise 2 — The largest¶
Ask for two numbers and display the larger one.
Solution
Exercise 3 — Divisible?¶
Ask for a number and check whether it is divisible by both 3 and 5.
Solution
Mini-project: Grade classifier¶
Ask for a grade (1–10) and display the rating with a motivational message.
Example:
Solution
grade = float(input("Your grade: "))
if grade < 1 or grade > 10:
print("Invalid grade. Enter a value between 1 and 10.")
elif grade >= 9.5:
print("Rating: Outstanding")
print("Extraordinary! Top performance!")
elif grade >= 8.5:
print("Rating: Very good")
print("Congratulations! You're close to perfection!")
elif grade >= 7:
print("Rating: Good")
print("Good result! Keep practicing.")
elif grade >= 5:
print("Rating: Sufficient")
print("You passed, but you have potential for more.")
else:
print("Rating: Insufficient")
print("Don't lose heart! Review the material and try again.")
Summary¶
if condition:— run a block if the condition is trueelif other_condition:— check another condition if the previous one is falseelse:— runs when no previous condition was true- Comparison operators:
==,!=,<,>,<=,>= - Logical operators:
and,or,not - Indentation (4 spaces) is mandatory!
Next step: → Lesson 05: Loops