(use the Space key to navigate through all slides)
|
Prof. Andrea Gallegati |
Prof. Dario Abbondanza |
Every day life, we often repeat tasks:
write Python programs that
repeat the same code execution
for variable in sequence:
# Code to repeat
passit allows us to execute a block of code multiple times, iterating over items in a sequence.
names = ["Alice", "Bob", "Charlie"]
for name in names:
print(name)The loop picks the first name
("Alice") and prints it.
Then, it picks "Bob" ...
Then, ...
Each iteration automatically
moves to the next item!
word = "Python"
for letter in word:
print(letter)What Happens?
The loop prints each letter
one at a time!
(a string is also a sequence of characters)
range() Function# Starts at 1, stops before 6
for num in range(1, 6):
print(num)range(1, 6) generates[1, 2, 3, 4, 5]
We can use range(n) to repeat an action n times.
# `_` is a placeholder (we
# don't need the variable)
for _ in range(3):
print("Hello!")for With if to find even numbers| 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