Shiklo Tools
PDF40
Image20
Calculators & Converters25
Text & Writing13
Developer26
Video14
Audio14
Document14
Data & Spreadsheet12
Web & SEO9
OCR & Scanning5
Archive & File12
Security & Privacy23
Health & Medical29
QR & Barcode5

Python Data Types

Every value in Python has a data type: the kind of value it is. The type decides what you can do with the value. You can divide two numbers but not two names; you can make text upper case but not a number. Python has a set of built-in types that cover almost everything programs need.

The built-in types

KindTypeExample
Whole numbersint42, -7, 1_000_000
Decimal numbersfloat3.14, -0.5, 2.0
Textstr"hello", 'Python'
True or falseboolTrue, False
A list of valueslist[1, 2, 3]
A fixed listtuple(10, 20)
Unique valuesset{"red", "green"}
Key and value pairsdict{"name": "Asha", "age": 21}
No valueNoneTypeNone

You will meet each of these types in its own topic. For now it is enough to recognise them.

Finding a value's type

The built-in function type tells you the type of any value:

print(type(42))
print(type(3.14))
print(type("hello"))
print(type(True))
print(type([1, 2, 3]))
print(type({"a": 1}))
print(type(None))
Output
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
<class 'list'>
<class 'dict'>
<class 'NoneType'>

The word class appears because in Python every type is a class, and every value is an object of its class. This becomes important when you write your own classes later.

The type follows the value

A variable has no type of its own: it takes the type of the value it holds at the moment. isinstance checks whether a value is of a given type:

x = 10
print(type(x).__name__, isinstance(x, int))
x = "ten"
print(type(x).__name__, isinstance(x, int))
print(10 == 10.0, "10" == 10)
Output
int True
str False
True False

The last line shows why types matter: the number 10 and the float 10.0 are equal, but the text "10" is not the number 10. Text that looks like a number must be converted before you can calculate with it, as the Casting topic shows.

Practice exercises

  1. Print the type of 7, 7.0, "7" and [7].
  2. Create one variable of each type in the table and print each value with its type.
  3. Check with isinstance whether True is an int, and find out why the answer surprises many people.
  4. Compare "5" + "5" with 5 + 5 and explain the difference.