[CreativeProgramming]Lecture8_Study Tips for the Midterm Exam
[CreativeProgramming]Lecture8_Study Tips for the Midterm Exam
Spring 2025
CUL1122 Lecture #08
Study Tips for the Midterm Exam
Week 2: Selection in Python
3
Three Control Flows in a Computer Program
Code 1 Code
If expression If expression
is true Expression is false If expression
is true
Expression
Code 2
Code 1 Code 2
If expression is false
Code 3
4
Python Selection: if Statement
❖The code inside the ‘if’ block is executed based on the evaluation of a
given condition.
▪ If <expression> evaluates to True, then <statement(s)> is executed.
▪ If <expression> evaluates to False, then <statement(s)> is skipped and not
executed.
if <expression>:
expression False
<statement(s)>
True
statement(s)
5
Python Indentation
❖Example:
▪ If the score is 70 or higher,
if score >= 70:
➢Code Block 1: ‘Pass!’ print(‘Pass!’)
▪ Otherwise, else:
print(‘Fail!’)
➢Code Block 2: ‘Fail!’
7
Multiple Selection Conditionals: if-elif-else
9
Week 3: Selection in Python (cont.)
10
Exercise 4: Assessing Body Mass Index (BMI)
Regarding the solution for Exercise 4, please refer to Lab4_Solution.py on the LMS.
11
Week 4: Functions in Python
❖Functions
▪ Importance of Functions
▪ Defining a Python Function
▪ Scope of Variables
12
Basic Idea of Functions
4 0 100 13
Importance of Functions
14
Defining a Python Function
❖The function declaration consists of the function name on the first line,
followed by the function body.
def name(parameters): # Function name is the identifier used to call the function
# Parameters are a list of inputs passed to the function
code # Function body must be indented
code Function # Function body is executed each time the function is called
… Body
return value # ‘return’ ends the function call and sends result back to the caller
# You can omit the ‘return’ if you want to return nothing
: Indentation
15
Scope of Variables: Types
❖1) Local Variables
▪ Valid only within the function.
▪ Created upon function call and disappear when the function ends.
❖2) Global Variables
▪ Valid throughout the script (or program).
▪ Created at the start of the program and cease to exist when the program ends.
❖Example:
▪ Global Variable: A person known throughout the country, such as Elon Musk.
▪ A person who is well-known in a specific local area, such as Mayor Oh Se-hoon.
➢He is famous in Korea but may not be recognized outside of the country.
16
Scope of Variables: Accessing Global Variables
17
Weeks 6-7: Iteration in Python
• Python Loops
• Syntax for Using the while Loop
❖range() and for Loop
▪ Syntax of the range() Function
▪ Steps to Use range() Function
▪ range() Examples
▪ for Loop with range() Function
❖for loop vs. while loop
❖Exercise 6: FizzBuzzWoof Implementation
18
The while Loop in Python
❖A while loop begins with ‘while,’ includes a condition, and ends with a
colon.
❖During each iteration:
▪ 1) The loop condition is checked.
▪ 2) If the condition is True, the code block inside the loop is executed.
▪ 3) Otherwise, the script leaves the The while Loop False
loop and proceeds to the next line while Condition: 1) Cond
of code. Code Block True
2) Code Block
3) …
19
Terminating the Loop Immediately
▪ This function requires a stop value, while the start and step values are optional.
❖Parameters
Name Description Default Value
start Specifying the initial value of the sequence. 0
stop Denoting the end value of the sequence (exclusive). N/A
step Defining the increment between consecutive numbers in the sequence. 1
21
range() Function Method #1: range(stop)
❖The most basic way to use the range() function is by specifying only the
end value, denoted as range(stop).
❖When using this form, the range() function automatically starts from 0,
increments by 1, and ends at stop - 1.
❖For example, to print the first 6 numbers starting from 0, you would use
the following code:
range(6)
for i in range(6): 0 0 1 2 3 4 5 6
print(i) sequence
start stop
▪ In this case, the default values are start = 0 and step = 1.
22
range() Function Method #2: range(start, stop)
❖You can also use the range() function by specifying both the start and
stop values, denoted as range(start, stop).
▪ This method is useful when you want the sequence to start from a value other
than 0.
❖When you provide two arguments to range(), it produces integers
starting from the start value up to, but not including, the stop value.
❖For example, to print numbers from 10 to 15, you would use the
following code:
range(10, 16)
for i in range(10, 16): 10 10 11 12 13 14 15 16
print(i) sequence
start stop
23
range() Function Method #3: range(start, stop, step)
❖You can use the range() function with start, stop, and step parameters
to specify increments.
❖When all three arguments are provided, the function creates a
sequence starting from the start value, incrementing by the step value,
and stopping before reaching the stop value.
❖For example, to print multiples of 5 from 10 to 35, you can use the
following code with a step value of 5:
range(10, 40, 5)
for i in range(10, 40, 5): 10 10 15 20 25 30 35 40
print(i) sequence
start stop
24
Syntax for a for Loop
26
for Loop vs. while Loop
❖In programming, there are two types of iteration: definite and indefinite.
❖Definite iteration involves specifying the number of times a designated
block of code will be executed when the loop starts.
❖Using a for loop for a specific number of iterations:
attempts = 0
for i in range(5): # Allow five attempts
password = input(‘Enter your password: ‘)
if password == ‘password123’:
print(‘Access granted!’)
break
else:
print(‘Incorrect password. Try again!’)
attempts = attempts + 1
27
for Loop vs. while Loop
❖On the other hand, indefinite iteration doesn’t specify the number of
loop executions in advance.
❖Instead, the designated block is executed repeatedly as long as a
condition is met.
❖The while loop is primarily used for indefinite iteration.
print(‘Using a while loop to allow for unlimited attempts:’)
while True: # Allow infinite attempts
password = input(‘Enter your password: ‘)
if password == ‘password123’:
print(‘Access granted!’)
break
28
Exercise 6: FizzBuzzWoof Implementation
29
수고하셨습니다!
30