Book 8 · Chapter 8
Programming fundamentals
B8·8This chapter is in the ebook.
Read it in full on cswithzak.com. Every question has its mark scheme and every program is ready to run. The free practice for each section is below, with the short code the book prints beside it.
For the whole chapter
- Topic pageProgramming fundamentals (2029–2031)
B8·8·T - QuizProgramming fundamentals
B8·8·Q1 - LabPython Playground
B8·8·X1 - LabProgramming tasks
B8·8·X2 - LabPython example py-hello-io
B8·8·X3 - LabPython example py-data-types
B8·8·X4 - LabPython example py-constants
B8·8·X5 - LabPython example py-string-numbers
B8·8·X6 - LabPython example py-if-elif
B8·8·X7 - LabPython example py29-match-nested
B8·8·X8 - LabPython example py-match
B8·8·X9 - LabPython example py-for-range
B8·8·X10 - LabPython example py-while
B8·8·X11 - LabPython example py29-post-condition
B8·8·X12 - LabPython example py-nested-loops
B8·8·X13 - LabPython example py-max-min-avg
B8·8·X14 - LabPython example py29-string-methods
B8·8·X15 - LabPython example py29-operators
B8·8·X16 - LabPython example py-div-mod
B8·8·X17 - LabPython example py-boolean
B8·8·X18
§8.1 Programming concepts
Short code B8·8·1
- FlashcardsFlashcards §8.1: Variables, constants, data types & casting
B8·8·F1 - FlashcardsFlashcards §8.1: Sequence, selection & iteration
B8·8·F2 - FlashcardsFlashcards §8.1: String handling
B8·8·F3
§8.2 Programming operators
Short code B8·8·2
- FlashcardsFlashcards §8.2: Arithmetic, relational & logical operators
B8·8·F4
Variables and constants
Short code B8·8·10
# A constant: its name is in capital letters
VAT_RATE = 0.2
# Variables, each initialised before it is used
each = 40
quantity = 3
cost = each * quantity
tax = cost * VAT_RATE
print("Cost:", cost)
print("Tax:", tax)
quantity = quantity + 2
print("Quantity now:", quantity)VAT_RATE is written in capitals and never changes, while quantity is given a new value near the end.Output
Cost: 120 Tax: 24.0 Quantity now: 5
Data types
Short code B8·8·11
pupils = 28 # int
height = 1.5 # float
team = "Falcons" # str
paid = True # bool
print(team, "has", pupils, "pupils")
print(pupils + 2)
print(height * 100)
print(pupils / 4)
print(paid)/ gave a float even though both numbers were whole, and that True prints without quotation marks.Output
Falcons has 28 pupils 30 150.0 7.0 True
Input and output
Short code B8·8·12
name = input()
age = int(input())
print("Hello", name)
print("Hello " + name + "!")
print("Next year you will be", age + 1)
print(name, "is", age, "years old")print() adds a space, but + joins strings with no space.Output
Hello Bilal Hello Bilal! Next year you will be 15 Bilal is 14 years old
Casting: changing the type
Short code B8·8·13
first = input()
second = input()
print(first + second)
print(int(first) + int(second))Output
53 8
text = "12"
number = int(text)
print(text + text)
print(number + number)
print(float(number))
print(int(7.9))
print(str(3) + str(4))
mark = 85
print("Mark: " + str(mark))1212 as strings but 24 as integers.Output
1212 24 12.0 7 34 Mark: 85
Sequence and selection: if, elif and else
Short code B8·8·14
mark = int(input())
if mark >= 80:
grade = "A"
elif mark >= 65:
grade = "B"
elif mark >= 50:
grade = "C"
else:
grade = "U"
print("Grade", grade)Output
Grade B
Output
Cold Mild Mild Hot
Output
Charge: 3 Charge: 6 Charge: 6 Charge: 10
Nested selection
Short code B8·8·15
username = input()
if username == "zara07":
pin = int(input())
if pin == 4321:
print("Welcome back")
else:
print("Wrong PIN")
else:
print("Unknown user")Output
Wrong PIN
match-case
Short code B8·8·16
day = int(input())
match day:
case 6 | 7:
print("Weekend")
case 1 | 2 | 3 | 4 | 5:
print("School day")
case _:
print("Not a day number")Output
Weekend
choice = input()
match choice:
case "A":
print("Add a pupil")
case "D":
print("Delete a pupil")
case "Q":
print("Quit")
case _:
print("Invalid choice")case "D" does not match a lower-case d, so case _ runs.Output
Invalid choice
Count-controlled loops: for
Short code B8·8·17
number = int(input())
for i in range(1, 6):
print(number, "x", i, "=", number * i)range(1, 6) stops at 5.Output
7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35
Output
0 1 2 3 4 -- 2 3 4 5 -- 1 4 7 -- 10 8 6 4 2
Pre-condition loops: while
Short code B8·8·18
age = int(input())
while age < 11 or age > 18:
print("Age must be 11 to 18")
age = int(input())
print("Accepted:", age)Output
Age must be 11 to 18 Age must be 11 to 18 Accepted: 15
Output
3 0 6 1 12 2 24 3 48 4 96 5 output: 96 5
Output
Too short - at least 8 characters Too short - at least 8 characters Password accepted
Output
Choose 1 to 6 tickets Choose 1 to 6 tickets Cost: 30.0
Post-condition loops: while True and break
Short code B8·8·19
total = 0
while True:
amount = float(input())
total = total + amount
more = input()
if more == "no":
break
print("Total to pay:", total)while True with break. Inputs: 2.5, yes, 4.25, yes, 1.25, no. Notice that the body always runs at least once.Output
Total to pay: 8.0
Nested loops
Short code B8·8·20
for row in range(1, 4):
line = ""
for col in range(1, 5):
line = line + str(row * col) + " "
print(line)Output
1 2 3 4 2 4 6 8 3 6 9 12
Output
1 0 1 2 0 2 2 1 3 3 0 4 3 1 5 3 2 6 output: 6
Totalling and counting
Short code B8·8·21
total = 0
count = 0
for day in range(7):
rain = int(input())
total = total + rain
if rain > 10:
count = count + 1
print("Total rainfall:", total)
print("Average:", total / 7)
print("Days over 10 mm:", count)Output
Total rainfall: 56 Average: 8.0 Days over 10 mm: 3
total = 0
count = 0
highest = -1
score = int(input())
while score != -1:
total = total + score
count = count + 1
if score > highest:
highest = score
score = int(input())
print("Scores entered:", count)
print("Average:", total / count)
print("Highest:", highest)count, not by a fixed number.Output
Scores entered: 4 Average: 15.0 Highest: 20
Output
Total goals: 13 Matches with no goals: 4
Output
Total sales: 12191 Months over 1000: 6
String handling
Short code B8·8·22
place = "lahore fort"
print(len(place))
print(place.upper())
print(place.title())
print(place.find("fort"))
print(place.find("city"))
print(place.count("o"))
print(place.replace("fort", "museum"))
print(place[0])
print(place[-1])
print(place[0:6])
print(place + "!").find() gives -1 when the text is not there.Output
11 LAHORE FORT Lahore Fort 7 -1 2 lahore museum l t lahore lahore fort!
first = input()
family = input()
year = input()
username = first[0:3].lower() + family[0].upper() + year[2:4]
print("Username:", username)
print("Length:", len(username))Output
Username: hamI11 Length: 6
Output
0 b 0 1 a 1 2 n 1 3 a 2 4 n 2 5 a 3 output: 3 check: 3
Output
9 AB-4471-X 2 ab/4471/x
Output
3 The Cat Sat On The Mat 12 the cat sat on the mat!
Arithmetic operators
Short code B8·8·23
print(17 + 5)
print(17 - 5)
print(17 * 5)
print(17 / 5)
print(17 // 5)
print(17 % 5)
print(2 ** 5)
print(20 / 4)
print(2 + 3 * 4)
print((2 + 3) * 4)/ gives a float, // the whole-number part, % the remainder.Output
22 12 85 3.4 3 2 32 5.0 14 20
minutes = int(input())
hours = minutes // 60
left = minutes % 60
print(hours, "hours and", left, "minutes")
if minutes % 2 == 0:
print("an even number of minutes")
else:
print("an odd number of minutes")Output
2 hours and 15 minutes an odd number of minutes
Output
4725 - 0 472 5 5 47 2 7 4 7 14 0 4 18 output: 18
Relational and logical operators
Short code B8·8·24
a = 7
b = 10
print(a == b)
print(a != b)
print(a < b)
print(a >= 7)
print(a > 5 and b > 12)
print(a > 5 or b > 12)
print(not a > 5)
passed = b >= 10
print(passed)Output
False True True True False True False True
mark = int(input())
if mark >= 0 and mark <= 100:
print("Valid")
else:
print("Invalid")
mark = int(input())
if not (mark < 0 or mark > 100):
print("Valid")
else:
print("Invalid")and, and the same test written with not and or. Inputs: 104, then 104 again.Output
Invalid Invalid
day = "Mon"
if day == "Sat" or "Sun":
print("Weekend")
else:
print("Weekday")
if day == "Sat" or day == "Sun":
print("Weekend")
else:
print("Weekday")or is not a comparison. The day is Mon, yet the program prints Weekend.Output
Weekend Weekday
Command-word coach
Short code B8·8·25
Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.
Workbook: Programming fundamentals
Short code W8·8 · Algorithms, Programming and Databases Workbook
The write-in workbook for this chapter has drills, exam-style practice, trace tables, fix-the-mistake and mark-it-yourself pages. Its full mark schemes and an interactive version arrive here with the chapter.
Starter files
Short code W8·8·K
Data files and skeleton code in Python for this chapter's tasks arrive here with the chapter.


