Python Data Types


Every value in Python has one data type (and only one). Because in Python programming everything is an object, data types are actually classes, and variables are actually instances (objects) of classes.

There are various data types in Python, listing some of the more important ones.

Related course: Complete Python Programming Course & Exercises

Common data types for the Python language are:

  • Numbers – int, float, complex
  • Sequences – String, List, Tuple, Set
  • Map – Dict

How to Identify the Data Type of a Variable?

Integer, floating point, and complex belong to Python's number category.

They are defined in Python as int, float, complex respectively.

You can use the type() function to see which class a variable or a value belongs to. The isinstance() function tells if an object belongs to a specific class.

>>> x = 3
>>> print(x,"is type of",type(x))
3 is type of <class 'int'>
>>> 
>>> y = 2.5
>>> print(y,"is type of",type(y))
2.5 is type of <class 'float'>
>>> 
>>> z = 1+3j
>>> print(z,"is type of",type(z))
(1+3j) is type of <class 'complex'>
>>>

The integer length can be arbitrary, and only the memory size of the variable limits it.

>>> x = 1000000000000000000000000000000000000001

The floating point can be accurate to 15 decimal places. Integers and floating-point numbers are separated by decimal points, with 1 being an integer and 1.0 being a floating-point number.

The format of imaginary numbers: x + yj. x is a real number and y is an imaginary number

python data types

Ths image above shows the data type of x is int (integer).

Python String

A string is a sequence of Unicode characters. You can use single quotes, double quotes to represent strings. Triple double quotes """xxx""" or '''xxx''' can be used to indicate a multi-line string.

>>> a = """hello world
... this is an example
... of a python string"""
>>> 
>>> print(a)
hello world
this is an example
of a python string
>>>

You can use the operator [] to access string characters.

>>> a = "Hello world"
>>> a[0]
'H'
>>> a[1]
'e'
>>>

It starts counting from zero, where [0] is the first character. A string is a collection of characters, so for each index you can assign only one character.

>>> a[2] = "ABC"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>>

Python Numbers

Python has several data types for numbers:

  • int regular numbers like 1,2,3
  • float floating point numbers like 2.5, pi
  • complex complex numbers

It can figure out the data type on its own:

>>> type(50)
<class 'int'>
>>> type(2020)
<class 'int'>
>>> 
>>> type(1.99)
<class 'float'>
>>> type(2.5)
<class 'float'>
>>> 
>>> type(1+2j)
<class 'complex'>
>>>

python numbers data types

Python Tuple

A tuple is an ordered sequence of elements. Tuples are immutable (unlike lists). Once created, the tuple cannot modify its content.

It is used to write protected data and is more efficient than the list because the data cannot be changed dynamically.

A tuple is defined by (), where the elements are separated by commas.

>>> x = (1,2,3,"abc",0.99)
>>> type(x)
<class 'tuple'>
>>>

You can use the slicing operator [] to fetch an element, but you cannot change the value of the element.

>>> x[0]
1
>>> x[1]
2
>>> x[2]
3
>>> x[3]
'abc'
>>> x[3] = "nop"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>>

Python List

A List is an ordered sequence of elements. Sometimes named Arrays, the most common data type used in Python.

The data types of the elements in the array can be inconsistent.

An array can be declared and the elements in the array are separated by commas and surroudned by [].

>>> x = [1,2,3,"A","B"]

You can use the slicing operator [] to get one or more elements from the list.

>>> x = [1,2,3,"A","B"]
>>> x[2]
3
>>> x[3]
'A'
>>>

You can count backwards too:

>>> x[-1]
'B'
>>> x[-2]
'A'
>>>

Use a colon to select from start to end:

>>> x[0:2]
[1, 2]
>>> x[1:4]
[2, 3, 'A']
>>>

List variables can be changed:

>>> x = [1,2,3,"A","B"]
>>> x[3] = 4
>>> x[4] = 5
>>> x
[1, 2, 3, 4, 5]
>>>

Python Set

A set is a unique unordered collection of elements defined by comma-separated values in curly brackets {}. Elements in a set are unordered.

>>> x = {1,2,3,4,5}
>>> print(x)
{1, 2, 3, 4, 5}
>>> print(type(x))
<class 'set'>
>>>

You can perform some operations on two sets, such as merging, intersecting. Set sets have unique values and they eliminate duplication.

>>> x = { 1,1,2,2,2,2,3,3,3,3,3,3,3,4,4,4,5,5,5,5,5 }
>>> x
{1, 2, 3, 4, 5}

Because set are unordered collection, indexing has no meaning. That's why the operator [] does not work.

Python Dictionary

Dictionaries are unordered key-value pairs.

A dictionary is optimized for retrieving data: to retrieving data you must know its key.

In Python, define the dictionary in curly brackets {}, where each element uses the key:value format. The data type of the key and value is arbitrary.

>>> a = {1:"a", 2:"c", 3:"e"}
>>> a
{1: 'a', 2: 'c', 3: 'e'}
>>> type(a)
<class 'dict'>

You can use the key (index) to get the value:

>>> print(a[1])
a
>>> print(a[3])
e
>>>

Do Python Functions have a Data Type?

You can test this by calling the type() function. But in short, yes, they have a data type.

>>> def f(x):
...     return x + 1
... 
>>> type(f)
<class 'function'>
>>>

A Python function is an instances of the class function.

Does Python Class Methods have a Data Type?

Lets find out if Python class methods have a data type.

>>> class MyClass:
...     def method(self):
...         pass
... 
>>> obj = MyClass()
>>> type(obj.method)
<class 'method'>
>>>

This shows that Python class methods have the data type method. That means they are instances of the class method.

Conclusion

In Python, everything is an object because it's an object-orientated programming language.

Values can have a data type, which can be a number (int, float, complex), text (string). There are also lists, tuples for sequences.

You can get call the type() function to find out the data type of a variable or object.