0% found this document useful (0 votes)
10 views26 pages

PBBML L5-2

This document provides an overview of basic Python programming concepts, focusing on control flow statements such as if, for loops, and the use of break, continue, and pass commands. It explains the syntax and functionality of these statements, including examples and use cases for each. Additionally, it outlines steps for environmental data projects that utilize these programming constructs.

Uploaded by

Ooi Wei Ying
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views26 pages

PBBML L5-2

This document provides an overview of basic Python programming concepts, focusing on control flow statements such as if, for loops, and the use of break, continue, and pass commands. It explains the syntax and functionality of these statements, including examples and use cases for each. Additionally, it outlines steps for environmental data projects that utilize these programming constructs.

Uploaded by

Ooi Wei Ying
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Python Based

Basic Machine Learning


Dr. –Ing. Youjin Kim
Video2
인문대학2호관 331
1:00pm ~
Lec 5-2
• Programming
• Break, Continue, Pass Syntax
Review
If Statement (Conditional Statement)
• The if statement in Python is used to make decisions based on conditions.
• A condition is an expression that evaluates to either True or False.
• Based on the result, Python decides whether to execute the block of code
inside the if statement.
Structure of an if statement
• condition: This is the expression or comparison that
is evaluated. It could be a comparison (like x > 5), a
check on a value (if x:), or any other logical
expression.
• : The colon indicates that the next indented block of
code belongs to the if statement.
• Indentation is essential in Python to indicate which
code should run if the condition is met.
If ~ else
if
True False
condition

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

commandment1 commandment2 commandment3

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.

Image Credit: Geeksforgeeks


continue

Image Credit: Geeksforgeeks


pass
• Purpose: The pass statement is a placeholder; it does nothing
when executed.
• Common Use: Use pass in places where code is syntactically
required but no action is needed yet (e.g., function or loop
placeholders).
• Example
Comparing break, continue, and pass
Content
• break: Exits the loop entirely.
• continue: Skips the current iteration and moves to the next.
• pass: Does nothing, used as a placeholder.
Use Cases
• break: Stopping loops early.
• continue: Skipping certain conditions.
• pass: Leaving code for future implementation.
Pass vs Continue
Pass vs Continue
Pass vs Continue

sensor =
Dictionary

sensor_data=
List

sensor = {“location”: “Sensor A”, “value”: 25, “status”: ”normal”}


key value

sensor[‘key’] calls the corresponding value


Pass vs Continue
for Loop

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

Use Cases Continue


False
• break: Stopping loops early.
• continue: Skipping certain conditions.
• pass: Leaving code for future implementation. If True
commandment 2
condition2

False

commandment 3

Print(“\n”)

You might also like