Python Summary for Cambridge Lower Secondary Computing
1. Strings (`str`)
What is it?
A string is a sequence of characters, like words or sentences, surrounded by quotation
marks (`"` or `'`).
Examples:
name = "Alia"
greeting = 'Hello, World!'
print(name)
print(greeting)
What can you do with strings?
- Combine them:
full_greeting = "Hello, " + name
print(full_greeting)
- Find their length:
print(len(name)) # Output: 4
2. Integers (`int`)
What is it?
An integer is a whole number, like `5`, `0`, or `-100`.
Examples:
age = 13
print(age)
What can you do with integers?
- Add, subtract, multiply, divide:
Python Summary for Cambridge Lower Secondary Computing
result = age + 2
print(result) # Output: 15
3. Floats (`float`)
What is it?
A float is a number with a decimal point, like `3.14`, `-2.5`, or `10.0`.
Examples:
pi = 3.14
print(pi)
What can you do with floats?
Same as integers! (Add, subtract, etc.)
4. `if`, `elif`, and `else` (Conditions)
What is it?
Conditions let the program make decisions based on a test.
Syntax:
if condition:
# code to run if the condition is true
elif another_condition:
# code to run if the second condition is true
else:
# code to run if no conditions are true
Examples:
age = 13
if age < 10:
Python Summary for Cambridge Lower Secondary Computing
print("You're a child.")
elif age < 18:
print("You're a teenager.")
else:
print("You're an adult.")
5. `input` (User Input)
What is it?
A function to take input from the user as a string.
Examples:
name = input("What is your name? ")
print("Hello, " + name + "!")
6. Numeric Input
How to use?
Convert `input` to an integer or float using `int()` or `float()`.
Examples:
age = int(input("How old are you? "))
print("You will be " + str(age + 1) + " next year!")
7. Loops
What are they?
Loops repeat code until a condition is met.
Types:
- For Loop: Repeats a fixed number of times.
Python Summary for Cambridge Lower Secondary Computing
for i in range(5): # Loops 5 times
print("This is loop number", i)
- While Loop: Repeats while a condition is true.
count = 0
while count < 5:
print("Count is", count)
count += 1
8. Syntax
Python is strict about indentation. Indented lines belong to a block of code.
Examples:
if True:
print("This is correct indentation.")
9. `f-strings`
What are they?
A convenient way to include variables in strings.
Examples:
name = "Alia"
age = 13
print(f"My name is {name} and I am {age} years old.")
10. Calculations
Basic math:
a = 10
Python Summary for Cambridge Lower Secondary Computing
b = 3
print(a + b) # Addition
print(a - b) # Subtraction
print(a * b) # Multiplication
print(a / b) # Division
print(a ** b) # Power
11. Functions
What is it?
A block of reusable code.
Syntax:
def function_name(parameters):
# code
return result
Examples:
def add_numbers(a, b):
return a + b
result = add_numbers(5, 7)
print(result) # Output: 12
Practice Problem
Write a program that:
1. Asks the user for their name.
2. Asks for their age.
3. Calculates the year they will turn 18.
Python Summary for Cambridge Lower Secondary Computing
Solution:
name = input("What is your name? ")
age = int(input("How old are you? "))
years_left = 18 - age
current_year = 2024
year_turn_18 = current_year + years_left
if age >= 18:
print(f"Hello {name}, you are already 18 or older!")
else:
print(f"Hello {name}, you will turn 18 in the year {year_turn_18}.")