(use the Space key to navigate through all slides)
|
Prof. Andrea Gallegati |
Prof. Dario Abbondanza |
ifEvery day, we make decisions based on conditions:
write Python programs that react to different conditions
if condition:
# Code that runs if
# the condition is True
passit allows us to execute a block of code only when a
specific condition is met.
True or False.:) fends the header.import random
# Define different weather conditions
w_conditions = ["rainy", "sunny", "cloudy"]
# Pick a random value from the above list
weather = random.choice(w_conditions)
# Print the random value
print("The weather is " + weather + ".")
if weather == "rainy":
print("Don't forget your umbrella!")<!=>
| Operator | Meaning | Example (x = 10, y = 5) |
|---|---|---|
> |
Greater than |
x > y → True
|
< |
Less than |
x < y → False
|
>= |
Greater than or equal to |
x >= 10 → True
|
<= |
Less than or equal to |
y <= 5 → True
|
== |
Equal to |
x == y → False
|
!= |
Not equal to |
x != y → True
|
import random
# Define the list of scores
grades = [20, 45, 56, 68, 75, 100]
# Pick a random value from the list
score = random.choice(grades)
# Print the random score
print("Score: " + str(score))
if score >= 60:
print("Congratulations!")
print(" You passed the exam.")else Statementimport random
# Define the range of possible scores
min_score = 20
max_score = 100
# Pick a random score
score = random.randint(min_score,
max_score)
# Print the random score
print("Score: " + str(score))
# Check if score meets the condition
if score >= 60:
message_1 = "Congratulations! "
message_2 = "You passed the exam."
else:
message_1 = "Sorry, you didn't pass."
message_2 = " Try Again!"
print(message_1 + message_2)elif Statementimport random
# Define the range of possible scores
min_score = 20
max_score = 100
# Pick a random score
score = random.randint(min_score,
max_score)
# Print the random score and
# the corresponding Grade
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: F")score >= 90, it prints "Grade: A".score >= 80... and so forth!
The program stops checking after the first True condition.
| Operator | Meaning | Example (x = 8, y = 5) |
|---|---|---|
| and | Both conditions must be True
|
x > 5 and y > 2 → True
|
| or | At least one condition is True
|
x > 10 or y < 10 → True
|
| not | Negates the condition |
not (x > 10) → True
|
age = 17
is_student = True
if age < 18 and is_student:
print("You get a student discount!")number = 7
if number % 2 == 0:
print(str(number) + " is even.")
else:
print(str(number) + " is odd.")
if Statementsage = 20
citizenship = "US"
if age >= 18:
if citizenship == "US":
print("You are eligible to vote.")
else:
print("You must be a US citizen to vote.")
else:
print("You must be at least 18 to vote.")age >= 18 is True.
US citizen is True.
True, it prints "You are eligible to vote."age < 18, it prints "You must be at least 18 to vote."
if statement executes a code block (condition True).else statement provides an alternative action.elif statement allows multiple conditions.==, !=, >, <, >=, <=) and, or, not) if statements allow deeper decision-making.temperature = 15 # Try changing this value!
if temperature > 30:
print("It's hot outside! Stay hydrated.")
elif temperature > 20:
print("The weather is nice and warm.")
elif temperature > 10:
print("It's a bit chilly, grab a sweater.")
else:
print("Brrr! It's freezing outside!")
A Framework created by Hakim El Hattab and contributors
to make stunning HTML presentations