hayaliv

hayaliv / Hi! Python / Level 0

Values, variables and types

Every value has a type, and a name refers to a value.

Hi! Python · Level 0: Python core · Checked against the Python 3.12 documentation · Updated 2026-10-17

Think of it like this

Think of luggage tags. The tag says 'Sam' and hangs on a suitcase. You can cut the tag off and tie it to a different suitcase, and the first suitcase stays exactly as it was.

Where the analogy stops being true. A real tag is a physical thing, but a Python name is not. It is a word in your code that refers to a value.

What to know

Example

label = "meeting_request"
count = 3
score = 0.92
is_reply = True
note = None
for v in (label, count, score, is_reply, note):
    print(type(v).__name__, v)
count = "three"
print(type(count).__name__, count)
print(8 / 4)

It prints:

str meeting_request
int 3
float 0.92
bool True
NoneType None
str three
2.0

Predict, then check

1. What does this print?

x = 5
x = "five"
print(type(x).__name__)
Show the answer
str

The name x was first tied to the int 5, then re-tied to the str 'five'. type() reports the type of whatever x points at now.

2. What does this print?

print(7 / 2, type(7 / 2).__name__)
Show the answer
3.5 float

Division with / always returns a float.

3. What does this print?

print("3" + "4")
Show the answer
34

Both are strings, so + joins the text instead of adding numbers.

Try it

Write type_name(value) that returns the name of the value's type as a string, for example 'int' or 'str'.

def type_name(value):
    return 'unknown'
Show a solution
def type_name(value):
    return type(value).__name__

Common mistake

A number that arrives as text is still a str. "3" + "4" gives "34", not 7 (see the third prediction above).

In a small-language-model project

In a fine-tuning project you usually load labels and counts from files as text, so checking type() early helps you catch a count that is still a str.

Sources

Each quote below appears word for word on the cited page of the official Python documentation (version 3.12).

Step through this lesson in the Hi! Python desktop

Strings and f-strings →