Advanced

Changing a value kind on purpose

A value kind is not fixed forever. You can ask the computer to read a value as another kind, and that is how text from the outside becomes a number you can compute with. Conversion is a deliberate step, not something that happens quietly.

What you will see

ValueError

About 2 min of reading

Try It

Convert at both edges and see the refusal.

1

Turn text into a whole number and add.

print(int("40") + 2)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
42
2

Turn a number into text and join.

print("Score: " + str(42))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Score: 42
3

Watch int drop the decimal part instead of rounding.

print(int(4.9))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
4
4

Ask for a conversion that cannot work. The refusal names the value, not the line of maths.

print(int("4.5"))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
ValueError
int accepts only text that is a whole number. Text with a point goes to float instead, which is the distinction that keeps a decimal from being silently truncated.

Data Flow

You name the kind you want, the computer re-reads the value as that kind, and it refuses if the value cannot be read that way.
1 You write int(value)
↓
2 The computer re-reads it
↓
3 A new value of a new kind
↓
4 Compute or refuse
Line by line

Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.

print(int("40") + 2) 42
Why this works
Why nothing is automatic

A computer could guess that "4" means 4, and then it would guess wrong as often as right. Refusing to guess is what makes a program reliable: the mismatch shows up as an error you can read, at the exact line where you should have converted.

Where conversions belong

At the edges of a program. Reading input, reading a file, reading a form: all hand over text, so all need converting inwards. Writing out for a person needs converting outwards. In the middle, values stay in their real kinds.

Examples

Up to a number

print(int("42"))
print(float("4.5"))

int reads text as a whole number, float reads it as a decimal. Both refuse text that is not a number, so the conversion doubles as a check on what arrived.

Down to text

score = 42
print("Score: " + str(score))

The plus only joins text, so a number has to be turned into text first. The f-string does the same conversion for you, which is why it is the shorter way to build a message.

The kinds that convert

print(int(4.9))
print(int(True))

int on a decimal throws away the part after the point, it does not round up. int on a yes/no value gives 1 for True and 0 for False, which is how counters are sometimes written.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← A value can come from outside
Previous Lesson
A value can come from outside
Next Lesson
Yes/no values combine
Yes/no values combine →