🚀 Alguni Start learning

Unit 4: Playing with Numbers

Remainders, dice and patterns.

Unit 4 of 31 in Python for kids. Its 5 lessons are Leftovers, Number Helpers, Roll the Dice, Loops Inside Loops and Dice Duel — below is everything each one explains, and a question or two from it to try.

Every sample on this page was run through real Python before it shipped, and prints exactly what it says it prints.

This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.

🍕 Leftovers

What is left over?

Share 7 slices between 2 people and 1 slice is left over. % gives you that leftover — programmers call it the remainder.

Python

print(7 % 2)
print(10 % 5)

It prints

1
0

The even-or-odd trick

A leftover of 0 means it divided perfectly. So n % 2 == 0 is the neatest way to ask "is this even?" — a trick you will use forever.

Python

n = 6
if n % 2 == 0:
    print("even")
else:
    print("odd")

It prints

even

Two kinds of divide

/ gives the exact answer with a decimal point. // throws the decimals away and keeps the whole number — handy for "how many whole boxes?"

Python

print(7 / 2)
print(7 // 2)

It prints

3.5
3

Try it yourself

What does this print?

Python

print(9 % 4)

Which one is True only for odd numbers?

  • n % 2 == 0
  • n % 2 == 1
  • n / 2 == 0
  • n % 1 == 0

Answer them in the app

🧮 Number Helpers

Powers

** means "to the power of". Two stars, not two multiplies.

Python

print(2 ** 3)
print(5 ** 2)

It prints

8
25

Built-in helpers

Python comes with small tools ready to use. abs drops a minus sign, round rounds, and min / max pick the smallest or biggest.

Python

print(abs(-7))
print(round(3.7))
print(min(4, 9))
print(max(4, 9))

It prints

7
4
4
9

Try it yourself

What does this print?

Python

print(3 ** 3)

What does abs(-12) give?

  • -12
  • 12
  • 0
  • an error

Answer them in the app

🎲 Roll the Dice

Borrowing extra tools

Python keeps some tools in boxes called modules. import random opens the box of random things — then you use them with random. in front.

Python

import random

roll = random.randint(1, 6)
print(roll)

It prints

A different number from 1 to 6 every time

Picking from a list

random.choice reaches into a list and pulls one item out at random. Perfect for a magic-8-ball or a random name picker.

Python

import random

answers = ["Yes", "No", "Maybe"]
print(random.choice(answers))

It prints

One of Yes, No or Maybe

Try it yourself

What can random.randint(1, 6) give you?

  • Any number from 1 to 5
  • Any whole number from 1 to 6, including both ends
  • Always 1
  • A decimal between 1 and 6

Why can a test never check the exact answer of random.randint(1, 6)?

  • randint is broken
  • Because it is meant to be different every time — that is the whole point
  • Because 6 is too big
  • It always returns 1 in tests

Answer them in the app

🔳 Loops Inside Loops

Counting in steps

Give range a third number and it counts in jumps. Great for every second number, or counting down.

Python

for i in range(0, 10, 2):
    print(i)

It prints

0
2
4
6
8

A loop inside a loop

Put a loop inside another and the inner one runs all the way through every single time the outer one turns. Two turns outside × three inside = six lines.

Python

for row in range(2):
    for star in range(3):
        print("*", end="")
    print()

It prints

***
***

Try it yourself

What is the LAST number this prints?

Python

for i in range(10, 0, -3):
    print(i)

What does end="" do in print("*", end="")?

  • Ends the program
  • Stops print adding a new line, so the next one carries on beside it
  • Prints an empty star
  • Nothing at all

Answer them in the app

🏆 Dice Duel

Try it yourself

What does this print?

Python

print(17 % 5, 17 // 5)

What does this print?

Python

total = 0
for i in range(1, 6):
    if i % 2 == 0:
        total = total + i
print(total)

Answer them in the app