CLO3 - Part 1 (Week06) (1)
CLO3 - Part 1 (Week06) (1)
X: MAJOR CHANGE
2
LECTURE NOTES
Contents of lectures are based on the textbook, recommended text, and supplementary
material
Please read supplementary material from page
44 to 48
3
1. SELECTIONS: IF STATEMENT
3. LOGICAL OPERATORS
7. ALGORITHMS
KEY TERMS
KAHOOT!QUIZ
4
LECTURE OBJECTIVES
5
1. SELECTIONS: IF STATEMENT
Another example, if you would like to buy an item and you found two similar
products. You will then put some conditions such as a price limit, rating of the
product by experts, etc.
6
1. SELECTIONS: IF STATEMENT
If the answer to the if statement is True, then a certain process will be executed, else other processes
will be executed.
• If..else
• nested if
• nested else if
7
1. SELECTIONS: IF STATEMENT
Rules:
• A lower case if keyword must be used
• The if statement must end with a colon :
• The keyword else must be lower case and start at the same level
as the keyword if
• The keyword else must end with a colon : 8
2. PYTHON COMPARISON OPERATORS
9
5. EXERCISES
Identify which of the following is a valid condition.
1. if (total = 500)
2. if (rate =>”FIVE” )
3. if (age = 2.5)
4. if (mark =< 40)
5. if (2.85 <= price)
6. if (5+8)
7. if ((1 - 1) <= 0)
8. if (ticketType == "Golden Class")
10
2. PYTHON COMPARISON OPERATORS –
EXAMPLE 1 (CONDITION WITH NUMERIC)
Sample output
2. PYTHON COMPARISON OPERATORS –
EXAMPLE 2 (CONDITION WITH NON-NUMERIC)
Sample output
Task 1:
Write a Python program that read mark of a student and
display Pass when given mark is more than or equal to 60,
otherwise display Fail.
Task 2:
Write a Python program that read absent percentage of a
student and display Drop from the course when the absent
percentage more than equal to 15, otherwise display
Continue the course. 14
3. LOGICAL OPERATORS
When you have more than one condition in the same if statement [compound condition], then
you need to use a logical operator. These logical operators simply allow you to request that both
conditions must be met or only one of them.
• If both are conditions must be True then use and.
• If Any one of the conditions is True then use or.
16
4. PYTHON LOGICAL OPERATORS - EXAMPLE
17
5. TASK - SELECTION
18
6. EXERCISE - SELECTION
19
7. ALGORITHMS - A REMINDER
Algorithm: is the step by step solution to the given problem which can be
translated into computer code.
It is clear and specific, include all steps and has correct order.
The algorithm can be expressed in plain spoken language and/or series of
calculations, or charts such flow charts.
Sometimes there are different solutions or algorithms to solve the same problems.
Some algorithms are faster and more efficient than others.
There are many ways to solve the same problem.
We can use our algorithm to translate into code.
20
7. ALGORITHMS - A REMINDER
21
7. ALGORITHMS – TRANSLATE TO PYTHON CODE
Define an algorithm which will determine if a student has passed or failed a course.
Then translate the algorithm into python code.
ALGORITHM CODE
1-2 Read in a value and assign it to studentGrade
3 if the studentGrade is greater than or equal to 70
4 the student has passed, print it
5 else
6 the student has failed, print it
22
SELECTION
LOGICAL OPERATOR
CONDITION
COMPOUND CONDITION
25
FORMATIVE KAHOOT! Q
ASSESSMENT UIZ1
26
27