IGCSE Computer Science: Python Programming Guide
1. Variables and Constants
•
Variables store values that can change during program execution.
age = 16
name = "Alice"
•
Constants store values that remain fixed.
PI = 3.14159 # Constants are usually written in uppercase
2. Basic Data Types
•
Integer (int): Whole numbers (e.g., 5, -3, 42)
•
Float (float): Decimal numbers (e.g., 3.14, -0.01)
•
String (str): Text (e.g., "Hello")
•
Boolean (bool): True or False (e.g., True, False)
3. Input and Output
•
Getting user input:
name = input("Enter your name: ")
•
Displaying output:
print("Hello,", name)
4. Sequence
•
The order in which statements are executed.
print("Step 1")
print("Step 2")
5. Selection (Conditional Statements)
•
Using if, elif, and else.
age = int(input("Enter your age: "))
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
6. Iteration (Loops)
•
For Loop:
for i in range(5):
print("Iteration", i)
•
While Loop:
count = 0
while count < 5:
print("Count is", count)
count += 1
7. Totalling and Counting
•
Summing values:
total = 0
for i in range(1, 6): # Loops from 1 to 5
total += i # Correctly indented
print("Total:", total) # Prints the total sum
•
Counting occurrences:
count = 0
for char in "banana":
if char == 'a':
count += 1
print("Number of 'a's:", count)
8. String Handling
•
Concatenation:
full_name = "Alice" + " " + "Smith"
•
Slicing:
first_three = "Computer"[:3] # Output: 'Com'
•
Length:
length = len("Hello")
9. Arithmetic, Logical, and Boolean Operators
•
Arithmetic: +, -, *, /, //, %, **
•
Logical: and, or, not
•
Boolean Comparisons: <, >, <=, >=, ==, !=
10. Nested Statements
•
Nested if statements:
num = 10
if num > 0:
if num % 2 == 0:
print("Positive even number")
•
Nested loops:
for i in range(3): # Outer loop (i varies from 0 to 2)
for j in range(2): # Inner loop (j varies from 0 to 1)
print(i, j)
Step-by-Step Execution:
1. First iteration (i = 0)
o
j = 0 → print(0, 0)
o
j = 1 → print(0, 1)
2. Second iteration (i = 1)
o
j = 0 → print(1, 0)
o
j = 1 → print(1, 1)
3. Third iteration (i = 2)
o
j = 0 → print(2, 0)
o
j = 1 → print(2, 1)
Explanation:
•
The outer loop (i in range(3)) runs 3 times (i = 0, 1, 2).
•
The inner loop (j in range(2)) runs 2 times (j = 0, 1) for each i.
•
So, the total number of iterations is 3 × 2 = 6.
11. Procedures, Functions, and Parameters
•
Procedure (no return value):
def greet():
print("Hello!")
greet()
•
Function (returns a value):
def square(x):
return x * x
print(square(4))
•
Function with parameters:
def add(a, b):
return a + b
print(add(3, 5))
12. Local and Global Variables
•
Local: Exists only within a function.
•
Global: Accessible throughout the program.
x = 10 # Global variable
def show():
y = 5 # Local variable
print(y)
13. Library Routines
•
Using built-in libraries:
import math
print(math.sqrt(16))
14. Maintainable Programs
•
Use meaningful variable names, comments, and modular code.
import math # Import the math module (This is a comment)
def calculate_area(radius): # Using meaningful names such as calculate_area
return math.pi * radius ** 2 # Returns the area of a circle.
# Call the function with a radius value
print(calculate_area(5)) # Example: radius = 5
15. One-Dimensional and Two-Dimensional Arrays
•
1D Array (List in Python):
numbers = [1, 2, 3, 4, 5]
print(numbers[0])
•
2D Array (List of Lists):
matrix = [[1, 2], [3, 4]]
print(matrix[1][0])
# matrix[0] refers to [1, 2] and matrix[1] refers to [3, 4]
# matrix[1] accesses the second row: [3, 4]
# matrix[1][0] accesses the first element (3) of that row. So, the output is 3.
16. Iterating Through an Array
•
Writing values into an array using a loop:
numbers = []
for i in range(5):
numbers.append(i) # Adds numbers 0 to 4 to the list
•
Reading values from an array using a loop:
numbers = [1, 2, 3, 4, 5] # Example list of numbers
for num in numbers:
print(num) # Prints each value in the array
•
Using an index-based loop to access array elements:
numbers = [10, 20, 30, 40, 50]
for i in range(len(numbers)):
print(f"Index {i}: {numbers[i]}") # Using f-string
print("Index", i, ":", numbers[i]) # Using space
print("Index " + str(i) + ": " + str(numbers[i])) # Using + for concatenation