Python Control Flow

Python Control Flow & Logic - Cmpnote

Topic: Control Flow & Decision Making

Teaching Python how to "Think" and choose paths.

Understanding Control Flow

In a simple script, Python reads code line-by-line from top to bottom. However, real programs need to make choices. Control Flow is the order in which individual statements are executed. We use Conditional Statements to tell Python: "Only run this code IF something is true."

The Tools of Logic: Comparison Operators

To make a decision, Python compares two values. The result is always True or False.

==
Equal to
!=
Not equal to
>
Greater than
<
Less than
>=
Greater or Equal
<=
Less or Equal

The If... Elif... Else Structure

Python uses three main keywords to handle decisions:

  • if: The first condition to check.
  • elif: (short for "else if") Used to check more conditions if the first one was false.
  • else: The "fallback" plan if none of the above conditions were met.
# A simple Grading System
score = int(input("Enter student score: "))

if score >= 70:
    print("Result: Distinction")
elif score >= 50:
    print("Result: Pass")
else:
    print("Result: Fail")
⚠️ The Golden Rule: Indentation
In Python, the 4-space gap (or Tab) after an if statement is not optional. It defines the "scope." If you don't indent, Python won't know which code belongs to the decision!

Practical Problems

Let's look at how this works in real-world software development:

A. Security Login

Verifying if a user is authorized to enter.

pin = input("Enter ATM Pin: ")
if pin == "0550":
    print("Welcome, User!")
else:
    print("Invalid Pin. Locked.")

B. Smart Discount

Applying a discount based on total purchase.

bill = float(input("Total Bill: "))
if bill > 10000:
    final = bill * 0.90
    print("Discounted Price:", final)
else:
    print("Amount Due:", bill)

๐Ÿ’ป Practical Coding Lab

The Challenge: Write an "Odd or Even" checker. Hint: Use the modulo operator (%) which gives the remainder of a division.

1. Ask the user for any whole number.
2. If the number divided by 2 has a remainder of 0, print "Even".
3. Otherwise, print "Odd".
Click to reveal Solution Code
num = int(input("Enter number: "))
if num % 2 == 0:
    print("This is an Even number.")
else:
    print("This is an Odd number.")

๐Ÿ“ Topic 2 Quiz

1. Which keyword is used to check a secondary condition if the first one fails?
2. What is the correct symbol for "Not Equal To"?
3. What character is required at the end of an "if" or "else" line?

Support Cmpnote: GTB 0229727053 | Joseph Okolo

© 2026 cmpnote.blogspot.com | Topic 2: Control Flow

Comments

Popular posts from this blog

Complete Computer Studies/ICT Curriculum for JSS 1 to SSS 3

90 Objective Examination Questions in Major Subjects

JSS 3 Objective Questions and Answers in Computer studies