๐ Python from scratch ยท Session 2
Variables: boxes that remember: practice sheet
Part 1: Making a variable
Remember: A variable is a name that holds a value, like a labelled box: age = 11. In Python = means "put the value on the right into the box on the left".
1. Warm-up The variable name is already made. Print Hello, followed by the name (use a comma).
name = "Zara"
2. Warm-up Make a = 6 and b = 7 and print their product.
3. Core price = 40 and qty = 3. Print Cost: and the cost.
price = 40 qty = 3
4. Stretch ๐ฅ side = 5. Print two lines: Area: โฆ and Perimeter: โฆ of a square with that side.
side = 5
Part 2: Changing a variable
Remember: Giving a variable a new value replaces the old one. score = score + 10 means: take what is in score, add 10, put it back.
5. Warm-up score starts at 0. Add 5 three times using += and print score.
score = 0
6. Core money = 100. Spend 35, earn 20, then double what is left. Print money.
money = 100
7. Stretch ๐ฅ Swap the values of a and b, then print a and b on one line. (Hint: a third box.)
a = 1 b = 2
Part 3: Maths operators
Remember: // divides and drops the remainder, % gives only the remainder, ** is "to the power of". / always gives a decimal answer.
8. Warm-up Print 17 // 5 on one line and 17 % 5 on the next.
9. Core sweets = 47 are shared equally by kids = 6. Print Each: โฆ and Left: โฆ.
sweets = 47 kids = 6
10. Core n = 2468. Print its last digit using %.
n = 2468
11. Stretch ๐ฅ n = 4729 (always 4 digits). Print the sum of its digits using // and %.
n = 4729
12. Stretch ๐ฅ days = 1000. Print how many whole years (365 days) and days that is, like: 2 years 270 days
days = 1000
Part 4: Neat printing with f-strings
Remember: An f-string has f before the quote; variables go inside curly brackets and are filled in: f"{name} is {age}".
13. Warm-up Print exactly: Ravi is 12 years old. using an f-string and the variables.
name = "Ravi" age = 12
14. Core Print: A 8 by 5 rectangle has area 40. (all three numbers from the variables)
l = 8 w = 5
15. Stretch ๐ฅ price = 250 and off = 20 (percent). Print Sale price: and the new price, worked out inside the f-string.
price = 250 off = 20
Activity 1: The shop bill
16. Work out a bill for 2 pencils, 3 notebooks and 1 eraser, a discount of one tenth of the total (whole rupees), and what is left to pay. Print exactly three lines: Total: โฆ, Discount: โฆ and To pay: โฆ. It must still be right if a price changes.
- Work out total = 2 pencils + 3 notebooks + 1 eraser, using the price variables.
- Work out discount = total // 10.
- Work out pay = total - discount.
- Print the three lines with f-strings.
pencil = 12 notebook = 45 eraser = 8 # Step 1: total total = 0 # Step 2: discount (one tenth, whole rupees) # Step 3: pay # Step 4: print Total:, Discount:, To pay:
Activity 2: Time converter
17. seconds = 3725. Print one line: 1 hours 2 minutes 5 seconds. It must work for any number of seconds.
- hours = seconds // 3600 (there are 3600 seconds in an hour).
- rest = seconds % 3600 (seconds left after the whole hours).
- minutes = rest // 60 and secs = rest % 60.
- Print the line with an f-string.
seconds = 3725 # Step 1: whole hours # Step 2: what is left # Step 3: minutes and seconds # Step 4: print
Activity 3: Report card
18. Three test marks out of 100 are given. Print exactly: Total: โฆ, Average: โฆ and Percent: โฆ (the percentage of 300). It must work for any marks.
- total = m1 + m2 + m3.
- average = total / 3.
- percent = total / 300 * 100.
- Print three lines with f-strings.
m1 = 78 m2 = 92 m3 = 85 # Step 1 # Step 2 # Step 3 # Step 4
Predict the output
Work it out on paper first, then type it into the lesson page to check.
19. What does this print?
x = 5 x = x + 2 print(x)
20. What does this print?
print(7 // 2)
21. What does this print?
print(23 % 5)
22. What does this print?
print(2 ** 3)
23. What does this print?
name = "Sam" print(f"Hi {name}")24. What does this print?
a = 3 b = a a = 10 print(b)
25. What does this print?
n = 345 print(n // 10 % 10)
Homework
26. Warm-up length = 12 and width = 5: print the area and the perimeter of the rectangle on two lines.
length = 12 width = 5
27. Core seconds = 500. Print like: 8 minutes 20 seconds
seconds = 500
28. Stretch ๐ฅ n = 357 (3 digits). Print the number with its digits reversed (753), worked out with // and %.
n = 357
Answer key
name = "Zara" print("Hello,", name)a = 6 b = 7 print(a * b)
price = 40 qty = 3 print("Cost:", price * qty)side = 5 print("Area:", side * side) print("Perimeter:", 4 * side)score = 0 score += 5 score += 5 score += 5 print(score)
money = 100 money -= 35 money += 20 money *= 2 print(money)
a = 1 b = 2 temp = a a = b b = temp print(a, b)
print(17 // 5) print(17 % 5)
sweets = 47 kids = 6 print("Each:", sweets // kids) print("Left:", sweets % kids)n = 2468 print(n % 10)
n = 4729 a = n // 1000 b = n // 100 % 10 c = n // 10 % 10 d = n % 10 print(a + b + c + d)
days = 1000 print(days // 365, "years", days % 365, "days")
name = "Ravi" age = 12 print(f"{name} is {age} years old.")l = 8 w = 5 print(f"A {l} by {w} rectangle has area {l * w}.")price = 250 off = 20 print(f"Sale price: {price * (100 - off) / 100}")pencil = 12 notebook = 45 eraser = 8 total = 2 * pencil + 3 * notebook + eraser discount = total // 10 pay = total - discount print(f"Total: {total}") print(f"Discount: {discount}") print(f"To pay: {pay}")seconds = 3725 hours = seconds // 3600 rest = seconds % 3600 minutes = rest // 60 secs = rest % 60 print(f"{hours} hours {minutes} minutes {secs} seconds")m1 = 78 m2 = 92 m3 = 85 total = m1 + m2 + m3 average = total / 3 percent = total / 300 * 100 print(f"Total: {total}") print(f"Average: {average}") print(f"Percent: {percent}")- 7
- 3
- 3
- 8
- Hi Sam
- 3
- 4
length = 12 width = 5 print(length * width) print(2 * (length + width))
seconds = 500 print(f"{seconds // 60} minutes {seconds % 60} seconds")n = 357 print(n % 10 * 100 + n // 10 % 10 * 10 + n // 100)