Docsity

Topic 3 – Selection: Thinking Like a Programmer | Programming Lesson Notes, Lecture notes of Computer Science

Toggle sidebar
bg1
Topic 3 – Selection: Thinking Like a Programmer
(Python)
1. What is Selection?
Selection allows a program to decide based on a condition.
Example 1:
age = 20
if age >= 18:
print("Adult")
else:
print("Under 18")
Think: If something is true, do this. Otherwise, do that.
2. Conditions
A condition produces either True or False.
Common comparison operators:
Operator Meaning Example
== Equal to x == 10
!= Not equal x != 10
> Greater than x > 10
< Less than x < 10
>= Greater/equal x >= 10
<= Less/equal x <= 10
Example 2:
pf3
pf4

Partial preview of the text

Download Topic 3 – Selection: Thinking Like a Programmer | Programming Lesson Notes and more Lecture notes Computer Science in PDF only on Docsity!

Topic 3 – Selection: Thinking Like a Programmer

(Python)

1. What is Selection?

Selection allows a program to decide based on a condition. Example 1: age = 20 if age >= 18 : print("Adult") else: print("Under 18") Think: If something is true, do this. Otherwise, do that.

2. Conditions

A condition produces either True or False. Common comparison operators: Operator Meaning Example == Equal to x == 10 != Not equal x != 10 > Greater than x > 10 < Less than x < 10 >= Greater/equal x >= 10 <= Less/equal x <= 10

mark = 65 print(mark >= 40 ) Output: True

3. if Statement

Used when something should happen only if a condition is true. Example 3: temperature = 30 if temperature > 25 : print("It's hot")

4. if + else

Used when there are two possible outcomes. Example 4: mark = 35 if mark >= 40 : print("Pass") else: print("Fail") Output: Fail

5. Multiple Decisions

Use elif when there are more than two possible outcomes.

A student passes with 40 or more. Input: mark Condition: mark >= 40 True → Pass False → Fail

8. Testing Selection

Always test values around the boundary. If 40 is a pass: Mark Expected 39 Fail 40 Pass 41 Pass This helps find errors in conditions.

Key Things to Remember

 Selection = decision-making.  Conditions produce True or False.  if → one decision.  if/else → two outcomes.  elif → multiple outcomes.  and, or, not → combine conditions.  Condition order matters.  Always test boundary values.