Creative Programming
Spring 2025
CUL1122 Lecture #02
Selection in Python
Today
❖Python Conditional Statements
▪ Comparison Operators
▪ Logical Operators
▪ If Statements
❖Python Conditionals II: Creating Complex Conditions
▪ Double Selection Conditionals: if-else
▪ Multiple Selection Conditionals: if-elif-else
▪ Nested Conditionals: if-if
3
Three Control Flows in a Computer Program
1. Sequencing 2. Selection 3. Iteration
In-order Execution Conditional Execution Repeated Execution
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
Comparison Operators
❖Comparison operators compare two values or expressions and produce
a result of the Boolean data type, representing either True or False.
Operators Example Description Result
> a>b Is a greater than b?
< a<b Is a less than b?
True
>= a >= b Is a greater than or equal to b? or
False
<= a <= b Is a less than or equal to b?
== a == b Are a and b equal?
!= a != b Are a and b not equal?
5
Logical Operators
❖Logical operators, such as AND, OR, and NOT, generate True or False
results through their operations.
Operators Example Description and Result
and a and b True only if both a and b are True,
otherwise False.
or a or b False only if both a and b are False,
otherwise True.
not not a False if a is True, True if False.
6
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)
7
Python Indentation
❖Indentation refers to the spaces at the beginning of each line of code.
❖While indentation in other programming languages may be for
readability only, in Python, it serves a functional purpose.
▪ Python uses indentation to indicate a code block.
▪ All statements within the same code block should have the same level of
indentation.
if num > 0 and num == 1:
print(‘Positive Number’) Python identifies a code block by its
print(‘Natural Number’) indentation.
The default indentation in Python is four spaces.
8
Double Selection Conditionals: if-else
❖This is about choosing between two options.
▪ If the condition is True, you execute Code Block 1; if it’s False, you go with Code
Block 2.
if Condition: False
Condition
Code Block 1
else: True
Code Block 2 Code Block 1 Code Block 2
❖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!’
9
Single vs. Double Conditional Statements
❖Example 1: Here, two conditions operate independently.
▪ Both are checked regardless of each other.
▪ In other words, even if condition ① is True, condition ② will still be evaluated.
❖Example 2: In this case, the two conditions are dependent.
▪ Only one of either condition ① or condition ② is chosen.
▪ So, condition ② is executed only when condition ① is False.
Ex.1 if statement Ex.2 if-else statement
if score >= 70: ① if score >= 70: ①
print(‘Pass!’) print(‘Pass!’)
if score < 70: ② else: ②
print(‘Fail!’) print(‘Fail!’) 10
Multiple Selection Conditionals: if-elif-else
❖This is choosing from three or more options.
▪ Conditions are checked one by one, executing the first block that is True.
▪ If all conditions are false and there’s an else statement, that block will be run.
if Condition 1: False if score >= 90:
Cond 1
Code Block 1 print(‘Grade is A.’)
True False
Cond 2 elif score >= 80:
elif Condition 2:
Code Block 1
True False print(‘Grade is B.’)
Code Block 2 Cond 3
Code Block 2 elif score >= 70:
elif Condition 3: True
print(‘Grade is C.’)
Code Block 3 Code Block 3
else:
else: Code Block 4
print(‘Grade is D or F.’)
Code Block 4
11
Nested if Statement
❖A nested if statement includes one or more conditional statements
within a conditional statement.
▪ The code block is executed only if all N conditions are met.
❖Example: Print ‘Smart City’ only if both ‘Seoul’ and ‘Seongdong’ are
True.
if Condition 1: if city == ‘Seoul’:
if Condition 2: if district == ‘Seongdong’:
… print (city + ‘ ’ + district + ‘ is a smart city!’)
if Condition N: else:
Code Block print (city + ‘, my soul!’)
12
Lab 2
Today
❖1) Using a Calculator
❖2) Converting Temperature
❖3) Calculating the Total Price
❖4) Assessing Body Mass Index (BMI)
❖5) Playing FizzBuzz
❖6) Exploring FizzBuzz with Nested Selection
14
Exercise #1: Using a Calculator
❖Create a script that takes two integers and an operation (+, -, *, /, //, %)
as input, performs the specified operation, and outputs the result.
▪ The % operator calculates the remainder of division, and the // operator finds
the integer quotient.
15
Exercise #2: Converting Temperature
❖Create a script that takes a temperature type (C or F) and a temperature
value as input, converts between Celsius and Fahrenheit, and displays
the converted temperature.
❖The formulas are as follows:
▪ F = 1.8 * C + 32
▪ C = (F - 32) / 1.8
16
Exercise #3: Calculating the Total Price
❖Create a script to calculate the drink price based on the order and
display the result.
17
Exercise #4: Assessing Body Mass Index (BMI)
❖Create a script to assess body condition based on BMI values.
▪ Calculate BMI by dividing weight by height squared.
▪ Then, evaluate it according to the criteria below:
18
Exercise #5: Playing FizzBuzz
❖Create a script that provides the FizzBuzz answer based on a given
number according to the following rules:
▪ Print ‘Fizz’ for multiples of 3.
▪ Print ‘Buzz’ for multiples of 5.
▪ Print ‘FizzBuzz’ for numbers that are multiples of both 3 and 5.
… 9 10 11 12 13 14 15 16 …
Fizz Buzz 11 Fizz 13 14 FizzBuzz 16
19
Exercise #6: Exploring FizzBuzz with Nested Selection
❖Modify the previous FizzBuzz script to implement FizzBuzz using nested
selection statements.
▪ Print ‘Fizz’ for multiples of 3.
▪ Print ‘Buzz’ for multiples of 5.
▪ Print ‘FizzBuzz’ for numbers that are multiples of both 3 and 5.
… 9 10 11 12 13 14 15 16 …
Fizz Buzz 11 Fizz 13 14 FizzBuzz 16
20
수고하셨습니다!
21