PBBML L5-2
PBBML L5-2
commandment1 commandment2
Next
If statement
If ~ elif
if
False
condition1
False
condition2
True
True
commandment1 commandment2
Next
If statement
If ~ elif ~ else
if
False
condition1
False
condition2
True
True
Next
If statement
for loop
• The for loop in Python allows you to iterate over a sequence (such as a list,
string, or tuple) or to repeat actions a certain number of times.
• When you need to loop a specific number of times or create a sequence of
numbers, the range() function is often used.
for loop
• The for loop in Python allows you to iterate over a sequence (such as a list,
string, or tuple) or to repeat actions a certain number of times.
• When you need to loop a specific number of times or create a sequence of
numbers, the range() function is often used.
for loop with range() function
• The range() function generates a sequence of numbers, which can be
used to control how many times the for loop should run.
• It doesn't directly create a list but generates numbers on demand (which is
more memory-efficient).
• The syntax for range() is
✓ start (optional): The starting value of the sequence (inclusive). Defaults to 0 if not provided.
✓ stop (required): The end value of the sequence (exclusive). The loop stops before
reaching this number.
✓ step (optional): The difference between each number in the sequence. Defaults to 1.
Lec5-2
Python Control Flow: break, continue, and pass
• Control flow statements manage the execution of loops or other
block structures.
• We will explore three common control flow commands: break,
continue, and pass
break
• Purpose: The break statement immediately terminates the current
loop.
• Common Use: Exiting a loop early when a condition is met.
• Example
continue
• Purpose: The continue statement skips the current iteration and
proceeds to the next one.
• Common Use: Skipping certain values or conditions within a loop.
sensor =
Dictionary
sensor_data=
List
for Loop
commandment commandment
Condition 1
commandment1
If True Condition 2
commandment 1
condition1 commandment2
commandment3
False
If True
commandment 2
condition2
False
commandment 3
Print(“\n”)
Pass vs Continue
for Loop
for Loop
commandment
commandment
Condition 1
commandment1
Condition 2
If True commandment2
commandment 1
condition1
commandment3
Continue
False
If True
commandment 2
condition2
False
commandment 3
Print(“\n”)
Environmental Data Project 1
Step 1
• for syntax makes variable 7 days
• Users put 7 temperatures
• .append() saves the input data in the list
temperature_data
Step 2
• sum() The sum of the temperature values
• len() The number of the elements
Step 3
• If, elif, else flow makes the different prints
depending on the values of average_temperature
Environmental Data Project 2
Step 1
• while True: loop means that the code inside
the loop will keep running indefinitely, as the
condition is always True. It doesn't depend on
any variable or condition to continue running.
• When users put -1, (temperature == -1)
break is operated.
• The values are added by the function.append()
in the list temperature_data
• day += 1 makes the whole loop be repeated.
Environmental Data Project 2
Step 2
• sum() addes all the values of temperature_data
• len() finds the number of the elements in
temperature_data
• if temperature_data else 0
• If temperature_data has values:
It calculates the average by dividing
total_temperature by the length of
temperature_data
(i.e., len(temperature_data)).
• If temperature_data is empty:
The expression if temperature_data else
0 kicks in. Since an empty list evaluates to False,
the code after else is executed, which sets
average_temperature to 0.
This avoids trying to divide by zero (which would
raise an error).
Summary for Loop
Content commandment
• break: Exits the loop entirely.
• continue: Skips the current iteration and moves to the
next.
If True
• pass: Does nothing, used as a placeholder. condition1
commandment 1
False
commandment 3
Print(“\n”)