Conditional S-1, ICT Cource
Conditional S-1, ICT Cource
Sequential Conditional if
Statement Statement Statement
Boolean Conditional
Data Type Expression
UNIT 06
1 2 Select country
Access the website
https://www.random
.org/quick-pick/
[8] Add 1 to n } if c == n1 or c == n2 or
c == n3:
[9] if c matches one of 2, 3, 9 then {
n += 1
[10] Add 1 to n }
if n == 3
[11] if n is 3, print ‘You won the lottery!’
print ‘You won the lottery’
[12] End
End
UNIT 06
순차구조
Sequence 선택구조
Selection 반복구조
Iteration
거짓
False 거짓
False
참
True 참
True
UNIT 06
# 100 is printed
# 100 is added to num and 200 is printed
# 100 is added again to num and 300 is printed
100
num = 100 num + 100
200
num = num + 100 num + 100
300
num = num + 100 num
‣ At some stages of the program, there are more than one paths to proceed, among which we have to select one.
‣ If there is no selection structure, the program will always repeat the same action. If the program always does the
same thing, it will always reach a fixed conclusion.
UNIT 06
How can we choose to run only one of several executable statements? Let's express this in a flowchart.
Executable Executable
statement 1 statement 2
• <Executable Statement 1> or <Executable Statement 2> is executed according to specific conditions.
• Conditions are determined by <Conditional Statement>. The conditional statement must have a True or False value.
UNIT 06
1.7. If statement and related terms that are executed in specific circumstances
‣ Implementing this code requires an expression that determines whether the condition is satisfied or not.
‣ This is called a conditional expression. And this conditional expression is evaluated in Boolean type, which contains
True or False values.
‣ The relational operator from the previous chapter is used to compare two operands, and the result of the relational
operator is shown in True or False value. Thus, it can be used in conditional expressions.
UNIT 06
if ` :
# if age is 18
# result of age < 20 is True
('youth discount')
youth discount
‣ When the age value is 18, the conditional statement of age <20 becomes True,
so 'youth discount' is printed on the screen.
‣ When the age value is 20 or more, no output is printed.
False
age < 20
True
print(‘youth discount’)
UNIT 06
18 24
age age
False False
age < 20 age < 20
True True
print(‘youth print(‘youth
discount’) discount’)
# if age is 18 # if age is 24
# result of age < 20 is True # result of age < 20 is False
('youth discount') ('youth discount')
youth discount
UNIT 06
('youth discount')
('youth discount')
‣ A chunk of code that can be executed when a condition is true is called a block.
‣ The conditional block of a conditional statement must be indented. Otherwise, an error occurs as above.
UNIT 06
1.11. Indentation
Focus Python is a programming language in which indentation is significant. Depending on the indentation, the
same code produces different results. Let's take a closer look at the output from indented codes from 1 to 5.
('youth discount')
('Welcome')
youth discount
Welcome
‣ [Indentation code 2] If the conditions of age <20 is not satisfied
('youth discount')
('Welcome')
Welcome
UNIT 06
1.11. Indentation
‣ [Indentation code 3] If the condition ‘age <20’ is satisfied
('age', age)
('youth welcome')
('youth discount')
Age 18
youth welcome
youth discount
Line 3-5
• Since age is 18, age <20 is True and Line 3-5 are printed.
UNIT 06
1.11. Indentation
‣ [Indentation code 4] If the condition age <20 is not satisfied
24
('age', age)
('youth welcome')
('youth discount')
No output
Line 3-5
• Since the age is 24, age <20 is False and Line 3-5 are not printed.
UNIT 06
Wrong Indentation
('age', age)
('youth welcome')
('youth discount')
('youth welcome')
UNIT 06
('youth discount')
youth discount
t
('youth discount')
youth discount
UNIT 06
Line 2
• If the number is divided by 3 and the remainder is all 0, line 3 is executed (If the value of module 3 is 0, it's a
multiple of 3)
• In this way, it is possible to determine whether the number is a multiple of 3.
UNIT 06
Enter an integer : 15
15 is a multiple of 3 and 5.
Line 2, 5
• If the remainder is zero when the number is divided by 3, and the remainder is zero when divided by 5, line 3 is
executed.
• In this way, it is possible to determine whether the number is a multiple of 3 as well as 5.
UNIT 06
2.3. == operator
('ID matches.')
Line 2, 5
• If the string input is 'david', the two strings s and my_id match, and Line 5 is executed. When another string is
given, Line 5 is not executed.
UNIT 06
Enter an integer : 50
n = 50
50 is an even number.
Line 3
• Checks if the remainder is 0 when the input n is divided by 2. If the input value is 50, the equation is True, so
line 4 is executed.
UNIT 06
Line 3
• Runs Line 4 block if the two strings are identical. In this case, it is not printed since the values of str1 and str2
are different.
UNIT 06
Line 5
• If the values of the two strings are different, Line 6 block is executed. In this case, it is printed because the
values of str1 and str2 are different.
UNIT 06
('Even number')
Even number
Line 5
• It leaves a block empty to complete the functionality later.
UNIT 06
Line 4
• func function does not do anything because there is no code inside the func() function.
UNIT 06
‣ Although it does not have a functionality, the pass statement plays a role as a placeholder.
‣ Therefore, if the pass statement is omitted inside the function in the above sentence, an error occurs.
UNIT 06
Time
Write the entire code and the expected output results in the note.
UNIT 06
Enter integer:
x = 50
50 is a natural number.
Example Output
or
Enter integer:
x = -10
Time
Write the entire code and the expected output results in the note.
UNIT 06
1.1. If statements that are executed when certain conditions are satisfied
x is greater than 1.
‣ Check the function of the if statement with an actual code. Put 100 in the variable x and write the if statement as
above. In this case, since the conditional expression x>1 returns True, the following execution is run.
‣ Therefore, 'x is greater than 1' is printed on the screen. Here, the line below :(colon) must be indented.
UNIT 06
‣ This part is mentioned repeatedly, because beginners often make these mistakes. The = operator is an operation that
puts the value of right to the variable on the left. The == operator returns True or False depending on whether the
left and right values are identical or not.
UNIT 06
‣ How can we print that one is accepted and qualified for a scholarship if the score is 90 or higher? In this case,
sentences can be grouped by using indentations as shown below.
‣ If the value of score is 90 or higher, two sentences calling print() are executed in the code below. Note that these
sentences have the same number of spaces. All of these belong to the same block.
if score >= 90 :
print(“You are accepted.”)
print(“You are qualified for a scholarship.”)
UNIT 06
Line 2-5
• Line 3, 4 and 5 are executed because the score is given as 95.
UNIT 06
IndentationError
Line 3-5
• Error occurs because the numbers of indented spaces are different.
UNIT 06
The split uses space as the default value. So, each value is inserted using the space as a separator.
In this way, a character that divides a string into one or more individual strings is called a separator.
UNIT 06
2.3. How to process input values with split and int functions
Output example
Enter age: 16
Youth
Enter age: 33
Adult
Enter age: 5
Kid
UNIT 06
Output example
Enter age: 20
Enter height in cm: 180
You can enter.