Python Foundations I
Complete Lesson
Computer Science Syllabus 9618
Learning Objectives
• Understand basic programming concepts and Python syntax
• Write simple scripts using variables, operators, and control structures
• Master input/output operations
• Apply control flow logic to solve problems
1 Variables and Data Types
Variables are containers for storing data values. In Python, you don’t need to declare variable
types explicitly.
1 # Variable assignment
2 name = "Alice" # String
3 age = 25 # Integer
4 height = 5.6 # Float
5 is_student = True # Boolean
Common Data Types:
int Whole numbers (e.g., 42, -10)
float Decimal numbers (e.g., 3.14, -0.5)
str Text enclosed in quotes (e.g., ”Hello”)
bool True or False values
2 Input and Output
1 # Output - print() function
2 print("Hello, World!")
3 print("Your age is:", age)
4
5 # Input - input() function (always returns a string)
6 name = input("Enter your name: ")
7 age = int(input("Enter your age: ")) # Convert to integer
1
3 Operators
Arithmetic Operators
• + Addition
• - Subtraction
• * Multiplication
• / Division (returns float)
• // Floor division (returns integer)
• % Modulus (remainder)
• ** Exponentiation
Comparison Operators
• == Equal to
• != Not equal to
• > Greater than
• < Less than
• >= Greater than or equal to
• <= Less than or equal to
Logical Operators
and Both conditions must be True
or At least one condition must be True
not Reverses the boolean value
4 Control Flow: If/Elif/Else
1 # Simple if statement
2 if age >= 18:
3 print("You are an adult")
4
5 # If-else statement
6 if temperature > 30:
7 print("It's hot!")
8 else:
9 print("It's cool")
10
11 # If-elif-else statement
12 if score >= 90:
13 print("Grade: A")
14 elif score >= 80:
15 print("Grade: B")
16 elif score >= 70:
17 print("Grade: C")
18 else:
19 print("Grade: F")
2
5 Match-Case Statements (Python 3.10+)
Python 3.10 introduced match-case statements (similar to switch statements in other languages).
1 # Basic match-case
2 day = "Monday"
3
4 match day:
5 case "Monday":
6 print("Start of the work week")
7 case "Friday":
8 print("Last day of work!")
9 case "Saturday" | "Sunday": # Multiple patterns
10 print("Weekend!")
11 case _: # Default case (like 'else')
12 print("Midweek day")
1 # Match-case with values
2 choice = 2
3
4 match choice:
5 case 1:
6 print("You selected option 1")
7 case 2:
8 print("You selected option 2")
9 case 3:
10 print("You selected option 3")
11 case _:
12 print("Invalid choice")
1 # Match-case is more powerful than traditional switch
2 # It can match patterns, not just equality
3 status_code = 404
4
5 match status_code:
6 case 200:
7 print("Success")
8 case 404:
9 print("Not Found")
10 case 500 | 502 | 503:
11 print("Server Error")
12 case code if code >= 400: # Guard clause
13 print("Client Error")
14 case _:
15 print("Unknown status")
Note: If you’re using Python 3.9 or earlier, use if-elif-else chains instead.
6 Writing Python Scripts
Create a file with .py extension and run it:
python script_name.py
3
7 Practice Questions
Basic Variables and Data Types (1-5)
1. Create variables to store your first name (string), last name (string), age (integer), and GPA
(float). Print all variables.
2. Write a program that calculates the area of a rectangle. Ask the user for length and width,
then display the result.
3. Create a variable temperature with value 25. Change it to 30, then print it. What type of
data is temperature?
4. Write a program that takes a user’s name and age as input, then prints: ”Hello [name], you
are [age] years old.”
5. Create a boolean variable is_raining set to True. Print the value and its data type using
type().
Arithmetic Operators (6-10)
6. Write a program that takes two numbers as input and displays their sum, difference, prod-
uct, and quotient.
7. Calculate the remainder when 47 is divided by 5. Also calculate 47 divided by 5 using floor
division.
8. Write a program that converts temperature from Celsius to Fahrenheit using the formula:
F = (C × 9/5) + 32.
9. Calculate the result of 2 raised to the power of 10. Then calculate the square root of 144
(hint: use ** 0.5).
10. A shop gives a 15% discount on an item. Write a program that takes the original price as
input and displays the discounted price.
Comparison and Logical Operators (11-15)
11. Write a program that takes two numbers as input and checks if the first number is greater
than, less than, or equal to the second number.
12. Create a program that checks if a number is even or odd. (Hint: use the modulus operator
%).
13. Write a program that takes a person’s age as input and determines if they can vote (age ≥
18) AND drive (age ≥ 16).
14. Check if a number is between 10 and 20 (inclusive). Use logical operators.
15. Write a program that checks if a year is a leap year. A leap year is divisible by 4 AND (not
divisible by 100 OR divisible by 400).
4
Control Flow with If/Elif/Else (16-18)
16. Write a program that takes a student’s score (0-100) and displays the letter grade:
• A: 90-100
• B: 80-89
• C: 70-79
• D: 60-69
• F: below 60
17. Create a simple login system. Define a correct username and password. Ask the user for
input and check if both match. Display ”Login successful” or ”Invalid credentials”.
18. Write a program that determines if a triangle is valid. Take three sides as input. A triangle
is valid if the sum of any two sides is greater than the third side.
Match-Case Statements (19-22)
19. Create a program using match-case that takes a day number (1-7) and prints the corre-
sponding day name (1=Monday, 2=Tuesday, etc.). Include a default case for invalid num-
bers.
20. Write a program that uses match-case for a simple menu system:
• 1: Display ”Account Balance”
• 2: Display ”Withdraw Money”
• 3: Display ”Deposit Money”
• 4: Display ”Exit”
• Any other input: Display ”Invalid option”
21. Create a traffic light simulator using match-case. Take a color input (red, yellow, green)
and display the appropriate action:
• Red: ”Stop”
• Yellow: ”Slow down”
• Green: ”Go”
• Combine yellow and red using the OR operator (|)
22. Write a program that categorizes a person’s BMI using match-case with guard clauses:
• Underweight: BMI < 18.5
• Normal: 18.5 ≤ BMI < 25
• Overweight: 25 ≤ BMI < 30
• Obese: BMI ≥ 30
• (BMI = weight / height2 )
5
Integration Challenge (23-24)
23. Create a calculator using match-case that:
• Takes two numbers as input
• Asks for an operation symbol (+, -, *, /, %, **)
• Uses match-case to perform the operation
• Displays the result
• Handles division by zero
24. Build a season identifier:
• Ask for a month number (1-12)
• Use match-case to determine and display the season:
• Winter: 12, 1, 2
• Spring: 3, 4, 5
• Summer: 6, 7, 8
• Fall: 9, 10, 11
6
8 Mini-Project: Enhanced Text-Based Calculator
Project Requirements
Build a calculator program that:
1. Displays a menu of operations (add, subtract, multiply, divide, power, modulus)
2. Takes two numbers as input
3. Performs the selected operation using match-case
4. Displays the result
5. Asks if the user wants to perform another calculation
6. Includes input validation and error handling
Example Output:
=== Calculator ===
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Power
6. Modulus
Select operation (1-6): 1
Enter first number: 15
Enter second number: 7
Result: 15 + 7 = 22
Continue? (y/n): n
Thank you!
Bonus Challenge: Implement the calculator using match-case statements for the menu
selection instead of if-elif-else chains.
9 Study Tips
• Practice daily: Code for at least 2 hours on coding days
• Type, don’t copy: Manually type all examples to build muscle memory
• Experiment: Modify examples and see what happens
• Debug: Make intentional errors to understand error messages
• Comment your code: Explain your logic in comments
10 Additional Resources
• Python Official Documentation: [Link]
7
• Practice more problems on: HackerRank, LeetCode (Easy), Codewars
• Use Python’s built-in help() function to learn about functions