[CreativeProgramming]Lecture2_Selection in Python
[CreativeProgramming]Lecture2_Selection in Python
Spring 2025
CUL1122 Lecture #02
Selection in Python
Today
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
Comparison Operators
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
❖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
12
Lab 2
Today
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
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)
18
Exercise #5: Playing FizzBuzz
19
Exercise #6: Exploring FizzBuzz with Nested Selection
20
수고하셨습니다!
21