Python has the following built-in data types by default:
Primitive (Basic) Data Types
Numeric : int, float, complex
Text : str
Boolean : bool
None : NoneType
Non-Primitive (Collection) Data Types
Sequence: list, tuple, range
Mapping : dict
Set : set, frozenset
Binary : bytes, bytearray, memoryview
Note: For beginners, we will first focus on data types highlighted in green above: int, float, str, bool, list, tuple, range, dict, and set.
Note:Python’s official documentation refers to these as built-in types and does not formally classify them as “primitive” and “non-primitive” data types. This classification is used only as a beginner-friendly way to organize and understand the different types of data in Python.
Primitive data types represent single, basic values.
Non-primitive data types are used to store collections of values or more complex data.
Below are examples of each data type:
Numeric
int : 1, 0, -45
float : 3.14, 0.0, -2.5
complex : 2+3j, 1j, -4+2j
Text
str : 'Hello'
"Python"
'''Hello World'''
"""Hello World"""
Boolean
bool : True, False
None
NoneType : None
Sequence
list : [1, 2, 3]
['apple', 'banana']
[]
tuple: (1, 2, 3)
('apple', 'banana')
()
range: range(5)
range(1, 10)
range(0, 10, 2)
Mapping
dict : {'name': 'John'}
{"age": 25}
{}
Set
set : {1, 2, 3}
{'apple', 'banana'}
set()
frozenset :
frozenset({1, 2, 3})
frozenset({'a', 'b'})
Binary
bytes: b'Hello'
b'\x48\x69'
bytearray :
bytearray(b'Hello')
memoryview:
memoryview(b'Hello')
Try the program below to print examples of various primitive data types.
Output:
------
1
0
-45
3.14
0.0
-2.5
Hello
Python
Hello World
Python Programming
This
is a multi-line
string.
Python
supports multi-line
strings.
True
False
When you run the above program, all the output appears together, which can make it difficult to read.
We can add some space or separators to make the output clearer.
print() : This prints an empty line
We can also print a line of characters such as ---- , **** , ==== , .... , etc. to separate different sections.
----- Numeric -----
1
0
-45
3.14
0.0
-2.5
----- Text -----
Hello
Python
Hello World
Python Programming
This
is a multi-line
string.
Python
supports multi-line
strings.
----- Boolean -----
True
False
So far, we have seen different types of values. But how can we check the type of a value in Python?
Python provides a built-in function called type() that tells us the data type of a value.
We can use type() inside print() to display the result.
----- Numeric -----
1
<class 'int'>
3.14
<class 'float'>
----- Text -----
Hello
<class 'str'>
Hello
This is a multi-line
string.
<class 'str'>
----- Boolean -----
True
<class 'bool'>
Python is an Object-Oriented Programming (OOP) language. In Python, everything is treated as an object, including data such as numbers, text, booleans, lists, and so on. Every object has a type, and that type is represented by a class.
In the output of the above program, we can see the type of various data values (objects).
Example: For print(type(1)) , Output is <class 'int'>.
Here, 1 is an object, and it's type is represented by int class, Python shows it as <class 'int'>.
* If this seems confusing, simply ignore the word class for now and focus on the data type, such as int, float, str, or bool. You don't need to worry about classes and objects or how they work. We will learn about them later when we study Object-Oriented Programming in the advance course