hayaliv / Hi! Python / Level 0
Values, variables and types
Every value has a type, and a name refers to a value.
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
- An int is a whole number such as 3, and a float has a fractional part such as 0.92.
- A str is text, a bool is True or False, and None is a special object of its own type, NoneType.
- The built-in type() tells you the type of any value.
- A name refers to an object. You can later point the same name at an object of a different type.
- Dividing with / always gives a float, even when the numbers divide evenly.
- A bool is a subtype of int.
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
strThe 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 floatDivision with / always returns a float.
3. What does this print?
print("3" + "4")Show the answer
34Both 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).
The integer numbers (e.g. 2 , 4 , 20 ) have type int , the ones with a fractional part (e.g. 5.0 , 1.6 ) have type float .
docs.python.org/3.12/tutorial/introduction.html — An int has no fractional part and a float does.Division ( / ) always returns a float.
docs.python.org/3.12/tutorial/introduction.html — Division with / always returns a float.With one argument, return the type of an object .
docs.python.org/3.12/library/functions.html — type() returns the type of an object.None is the sole instance of the NoneType type.
docs.python.org/3.12/library/constants.html — None is the sole instance of NoneType.Objects have individuality, and multiple names (in multiple scopes) can be bound to the same object.
docs.python.org/3.12/tutorial/classes.html — Several names can be bound to one object.In addition, Booleans are a subtype of integers.
docs.python.org/3.12/library/stdtypes.html — bool is a subtype of int.Every object has an identity, a type and a value.
docs.python.org/3.12/reference/datamodel.html — Every object has a type.