0% found this document useful (0 votes)
3 views

CLO3 - Part 1 (Week06) (1)

This document covers the concept of selections in programming, specifically focusing on the 'if statement' in Python. It outlines the structure and rules for using if statements, comparison operators, and logical operators, along with examples and exercises for practical application. The document also emphasizes the importance of algorithms in programming and provides guidance on translating algorithms into Python code.

Uploaded by

dominiccondarco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

CLO3 - Part 1 (Week06) (1)

This document covers the concept of selections in programming, specifically focusing on the 'if statement' in Python. It outlines the structure and rules for using if statements, comparison operators, and logical operators, along with examples and exercises for practical application. The document also emphasizes the importance of algorithms in programming and provides guidance on translating algorithms into Python code.

Uploaded by

dominiccondarco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

ICT 2013-COMPUTATIONAL

THINKING & CODING


CHAPTER - SELECTIONS:
THE IF STATEMENT

CHAPTER: CLO3 Develop computer


algorithms to solve real life
programming problems.
Versio Author​ Effectiv Change Des DRC DOCUMENT
n​
1.0​ Anand
e Date​ cription​
Sept Define the
No​
001​
REVISION
Pandyan 2020 first version​ CONTROL
1.1​ Dr Sept Major 001​
(DRC)
Madeleine 2021 changes:
Togher updating the
formatting
and adding
the
algorithms

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

 Please read textbook Chapter 3 from page


 94 to 95

3
1. SELECTIONS: IF STATEMENT

2. PYTHON COMPARISON OPERATORs

3. LOGICAL OPERATORS

4. PYTHON LOGICAL OPERATORS


5. TASK- SELECTION
LECTURE
OUTLINE
6. EXERCISE – SELECTION

7. ALGORITHMS

KEY TERMS

KAHOOT!QUIZ

4
LECTURE OBJECTIVES

 At the end of lecture, the student should be able to:


 Apply if statement to single or multiple conditions.
 Structure conditions in Python using the correct logical operators.
 Write Python codes to solve various computational problems that require the
use of the if statement.
 Use Algorithms to assist in defining code

5
1. SELECTIONS: IF STATEMENT

 In life there are moments when we need to decide on something using


conditions.

 Example: “If it is raining outside, then I carry umbrella.”

 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

 In programming such logic is written using if statement.

 If the answer to the if statement is True, then a certain process will be executed, else other processes
will be executed.

 Python programming language provides following types of decision making statements.

• If..else
• nested if
• nested else if
7
1. SELECTIONS: IF STATEMENT

Structure of if statement in Python:

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)

emirates = input("How many emirates are in UAE ")


emirates = int(emirates ) Input
if(emirates == 7): Condition

print("Yes you are correct ") When True


else: Otherwise

print("No, there are 7 emirates ") When False

Sample output
2. PYTHON COMPARISON OPERATORS –
EXAMPLE 2 (CONDITION WITH NON-NUMERIC)

capital = input("What is capital of UAE Input


") Condition
if(capital == "Abu Dhabi"):
print("Yes you are correct ") When True
else: Otherwise
print("No, it is Abu Dhabi ") When False

Sample output

Note: String comparision is case sensitive. 12


2. PYTHON COMPARISON OPERATORS –
EXAMPLE 3
Write a Python
program to calculate
the cost of insurance
based on the age. If
the age of the
subscriber is more
than or equal 40
years, the cost will be
500 each month,
otherwise the cost is
300 a month.
2. PYTHON COMPARISON OPERATORS –
EXERCISE

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.

Operator Description Example

and If both the operands are 3>7 and 2<3


true then condition
becomes true.

or If any of the two operands 7 > 7 or 2 < 3


are non-z ero then 15
condition becomes true
4. PYTHON LOGICAL OPERATORS
When you have more than one condition, it is
recommended to group them using brackets.
Think of a situation when condition 1 to be met and
one of the other two conditions

16
4. PYTHON LOGICAL OPERATORS - EXAMPLE

A child is eligible to enter a age= input("What is your age") Input


ride if its age is between 4 age= int(age)
to 10. Write a Python Condition
program to read child age, if(age > 3 and age < 11):
decide and display if the True
child is eligible for ride or print("Yes you are eligible to ride ")
not. Otherwise
else:

print("Sorry you not eligible to ride") False

17
5. TASK - SELECTION

Modify the above Python program to read height and


age of a child and check if the child is eligible for ride,
age should be between 5 to 12, or height is more than
48 inch.

18
6. EXERCISE - SELECTION

What it the output of


the following code?

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

 There are four main components of an algorithm: data


acquisition, sequence, selection and iteration.
 Writing an algorithm to solve a simple problem may not require all
these components.
 An algorithm is usually written in plain (simple) human language,
and then translated to a high-level programming language such as
Python.

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

IF ELSE KEY TERMS

CONDITION

COMPOUND CONDITION
25
FORMATIVE KAHOOT! Q
ASSESSMENT UIZ1

26
27

You might also like