Basic types
In the previous lesson a name was a tag, and a value was a token. A token has one more feature—its kind.
Picture a stamp on every token, next to its value: "whole number", "fractional number" or "text". The stamp decides what you can do with the token.
Let's see how this shows up in code. The same + operator gives different results for different values. Digits without quotes are added as numbers, and digits in quotes are joined as text:
print(7 + 3) # 10
print("7" + "3") # 73
print(type(7)) # <class 'int'>
print(type("7")) # <class 'str'>Let's walk through the example line by line:
7 + 3adds two numbers and prints10."7" + "3"joins two strings and prints73.type(7)returns the type of the number7, andprintshows it as<class 'int'>.type("7")returns the type of the string:<class 'str'>.
What a type is
The difference is the value's type: 7 is a whole number of type int, and "7" is text of type str. A type defines which operations a value supports and how they work. In our picture, the type is that stamp on the token.
The type() function returns an object's type, and print shows it as <class '…'>. A value written directly in the code—7, "7", 2.5—is called a literal.
Five basic types
| Type | Examples | What it holds |
|---|---|---|
int | 0, 42, -7 | a whole number |
float | 2.5, -0.1, 3.0 | a fractional number |
str | "hi", 'hi', "" | text |
bool | True, False | a truth value |
NoneType | None | the absence of a value |
Let's look at each type more closely.
The numbers int and float
A number with a point is always a float, even with no fractional part: 3.0 and 3 are values of different types.
When an int and a float meet in addition, subtraction or multiplication, the result is a float. So 2 * 2.5 gives 5.0, and 2 * 2.0 gives 4.0. The same operations on two int values give an int: 7 * 3 is 21.
Strings, str
Text in Python is called a string, and its type is str. A string is written in double or single quotes, and "hi" and 'hi' are the same value. In the course's code we write double quotes. Empty quotes "" give an empty string.
The boolean type bool
The bool type holds one of two boolean values: True or False. Both start with a capital letter. Such a value answers a yes-or-no question: is the shop open, is the item in stock.
The value None
The value None means there is no value. A function without return, for example, returns it. The type of None is called NoneType, and this type has no other values.
The type belongs to the value
Back to the tags. The stamp is on the token, not on the tag. That is why a variable in Python has no type of its own. The type belongs to the object it refers to, and after a new assignment it can be a different one:
x = 42
print(type(x)) # <class 'int'>
x = "forty-two"
print(type(x)) # <class 'str'>
x = None
print(x, type(x)) # None <class 'NoneType'>Let's go step by step:
x = 42ties the tagxto a whole number, andtype(x)prints<class 'int'>.x = "forty-two"retiesxto a string, and the same call prints<class 'str'>.x = NoneretiesxtoNone, andprintoutputsNone <class 'NoneType'>.
type(x) looks at the object x refers to at the moment of the call. An object never changes its own type: 42 stays an int, and a new assignment binds x to a different object. The stamp on a token cannot be changed—you can only retie the tag to another token.
Interviewers like to ask this: "Does a variable in Python have a type?" The answer: the object has a type, and the variable only refers to it. The same name can refer first to an int, then to a str.
Common mistake
A boolean value is written in lowercase:
is_open = true # ✗
print(is_open)NameError: name 'true' is not defined. Did you mean: 'True'?Python tells letter case apart, so true is an ordinary variable name to it, and no such variable exists. The values True, False and None are written only with a capital letter.
Key points
- A type defines which operations a value supports:
7 + 3gives10, and"7" + "3"gives"73". - The basic types are
int,float,str,boolandNoneType;type()returns an object's type. - An
inttogether with afloatin arithmetic gives afloat, and a number with a point is always afloat. - The object has a type, not the variable: after a new assignment
type(x)can be different.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.