CODING
ADVENTURE
0 / 12 completed

FIELD NOTE 04 / BEGINNER PYTHON

Python input, numbers and type conversion

The value 12 is a number; "12" is text. Types describe what kind of value you have and which operations make sense for it.

The idea

A locker number can be a label or a quantity, depending on your task. Joining "12" and "3" makes "123". Adding 12 and 3 makes 15. input() returns text—even when the reader enters digits. Convert it with int() if you need a whole number. Here the playground supplies input from its Input lines box: put one answer on each line before Run.

A tiny experiment

PYTHON 3PREDICT BEFORE YOU RUN
answer = input("How many clues? ")
clues = int(answer)
print(clues + 1)
print(7 / 2)
print(7 // 2)
print(7 % 2)

Prepared input for this example: 3. The playground supplies it when input() asks.

Run and change this example ↗
Reveal the expected output
How many clues? 4
3.5
3
1

Read it one step at a time

  1. With input 3, int converts the text to an integer and the first calculation produces 4. The prompt appears before that result.
  2. / gives ordinary division. For these positive numbers, // gives the whole-number quotient.
  3. % gives the remainder: 7 contains three groups of 2, with 1 left over.

Make one change

Keep the input as 3, but change all three 7s to 8. What changes?

Check your thinking

The division results become 4.0 and 4; the remainder becomes 0.

Keep following the trail

Sources & further reading

Companion notes for Coding Adventure by James Colby. Historical claims link to their sources; the examples are written for this learning trail.