Book a free demo

๐Ÿ Python from scratch ยท Session 2

Variables: boxes that remember: practice sheet

Name ____________________Date __________Score ____ / 28

โ† Back to the lesson

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. 1. Warm-up The variable name is already made. Print Hello, followed by the name (use a comma).

    name = "Zara"
    
  2. 2. Warm-up Make a = 6 and b = 7 and print their product.

  3. 3. Core price = 40 and qty = 3. Print Cost: and the cost.

    price = 40
    qty = 3
    
  4. 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.

  1. 5. Warm-up score starts at 0. Add 5 three times using += and print score.

    score = 0
    
  2. 6. Core money = 100. Spend 35, earn 20, then double what is left. Print money.

    money = 100
    
  3. 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.

  1. 8. Warm-up Print 17 // 5 on one line and 17 % 5 on the next.

  2. 9. Core sweets = 47 are shared equally by kids = 6. Print Each: โ€ฆ and Left: โ€ฆ.

    sweets = 47
    kids = 6
    
  3. 10. Core n = 2468. Print its last digit using %.

    n = 2468
    
  4. 11. Stretch ๐Ÿ”ฅ n = 4729 (always 4 digits). Print the sum of its digits using // and %.

    n = 4729
    
  5. 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}".

  1. 13. Warm-up Print exactly: Ravi is 12 years old. using an f-string and the variables.

    name = "Ravi"
    age = 12
    
  2. 14. Core Print: A 8 by 5 rectangle has area 40. (all three numbers from the variables)

    l = 8
    w = 5
    
  3. 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

  1. 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.

    1. Work out total = 2 pencils + 3 notebooks + 1 eraser, using the price variables.
    2. Work out discount = total // 10.
    3. Work out pay = total - discount.
    4. 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

  1. 17. seconds = 3725. Print one line: 1 hours 2 minutes 5 seconds. It must work for any number of seconds.

    1. hours = seconds // 3600 (there are 3600 seconds in an hour).
    2. rest = seconds % 3600 (seconds left after the whole hours).
    3. minutes = rest // 60 and secs = rest % 60.
    4. 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

  1. 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.

    1. total = m1 + m2 + m3.
    2. average = total / 3.
    3. percent = total / 300 * 100.
    4. 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.

  1. 19. What does this print?

    x = 5
    x = x + 2
    print(x)
  2. 20. What does this print?

    print(7 // 2)
  3. 21. What does this print?

    print(23 % 5)
  4. 22. What does this print?

    print(2 ** 3)
  5. 23. What does this print?

    name = "Sam"
    print(f"Hi {name}")
  6. 24. What does this print?

    a = 3
    b = a
    a = 10
    print(b)
  7. 25. What does this print?

    n = 345
    print(n // 10 % 10)

Homework

  1. 26. Warm-up length = 12 and width = 5: print the area and the perimeter of the rectangle on two lines.

    length = 12
    width = 5
    
  2. 27. Core seconds = 500. Print like: 8 minutes 20 seconds

    seconds = 500
    
  3. 28. Stretch ๐Ÿ”ฅ n = 357 (3 digits). Print the number with its digits reversed (753), worked out with // and %.

    n = 357
    

Answer key

  1. name = "Zara"
    print("Hello,", name)
  2. a = 6
    b = 7
    print(a * b)
  3. price = 40
    qty = 3
    print("Cost:", price * qty)
  4. side = 5
    print("Area:", side * side)
    print("Perimeter:", 4 * side)
  5. score = 0
    score += 5
    score += 5
    score += 5
    print(score)
  6. money = 100
    money -= 35
    money += 20
    money *= 2
    print(money)
  7. a = 1
    b = 2
    temp = a
    a = b
    b = temp
    print(a, b)
  8. print(17 // 5)
    print(17 % 5)
  9. sweets = 47
    kids = 6
    print("Each:", sweets // kids)
    print("Left:", sweets % kids)
  10. n = 2468
    print(n % 10)
  11. n = 4729
    a = n // 1000
    b = n // 100 % 10
    c = n // 10 % 10
    d = n % 10
    print(a + b + c + d)
  12. days = 1000
    print(days // 365, "years", days % 365, "days")
  13. name = "Ravi"
    age = 12
    print(f"{name} is {age} years old.")
  14. l = 8
    w = 5
    print(f"A {l} by {w} rectangle has area {l * w}.")
  15. price = 250
    off = 20
    print(f"Sale price: {price * (100 - off) / 100}")
  16. 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}")
  17. seconds = 3725
    hours = seconds // 3600
    rest = seconds % 3600
    minutes = rest // 60
    secs = rest % 60
    print(f"{hours} hours {minutes} minutes {secs} seconds")
  18. 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}")
  19. 7
  20. 3
  21. 3
  22. 8
  23. Hi Sam
  24. 3
  25. 4
  26. length = 12
    width = 5
    print(length * width)
    print(2 * (length + width))
  27. seconds = 500
    print(f"{seconds // 60} minutes {seconds % 60} seconds")
  28. n = 357
    print(n % 10 * 100 + n // 10 % 10 * 10 + n // 100)
Chat on WhatsApp
Esc

Try