Skip to content
Theory · Variables and types · lesson 3 of 7

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:

Python
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:

  1. 7 + 3 adds two numbers and prints 10.
  2. "7" + "3" joins two strings and prints 73.
  3. type(7) returns the type of the number 7, and print shows it as <class 'int'>.
  4. 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

TypeExamplesWhat it holds
int0, 42, -7a whole number
float2.5, -0.1, 3.0a fractional number
str"hi", 'hi', ""text
boolTrue, Falsea truth value
NoneTypeNonethe 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:

Python
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:

  1. x = 42 ties the tag x to a whole number, and type(x) prints <class 'int'>.
  2. x = "forty-two" reties x to a string, and the same call prints <class 'str'>.
  3. x = None reties x to None, and print outputs None <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:

Python
is_open = true  # ✗
print(is_open)
TEXT
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 + 3 gives 10, and "7" + "3" gives "73".
  • The basic types are int, float, str, bool and NoneType; type() returns an object's type.
  • An int together with a float in arithmetic gives a float, and a number with a point is always a float.
  • 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.

Sign in another way