top of page

Data Types in Python

Updated: Feb 9, 2023

There are some fundamental or built-in data types in Python. .

We can use type () function to get the data type of an object, and we can use id () function to get the identity (address) of the object.

Numeric types - int, float, complex

There are three distinct numeric types: integers, floating point numbers, and complex numbers.

Every type is an object in python, and it uses automatic memory management using reference counting.

>>> a = 10
>>> print(type(a))
<class 'int'>

>>> b = 1.23
>>> print(type(b))
<class 'float'>

>>> c = 11j
>>> print(type(c))
<class 'complex'>

>>> print(hex(id(a)))

>>> print(hex(id(b)))

Booleans are subtype of integers, The boolean type has only two values, True or False.

Arithmetical operators: +, - *, /, % //, **

Sequence types: list, tuple, range, str

list, and tuple both can contain instances of different types, the main difference is list is a mutable sequence, whereas tuple is the immutable sequence.

  • list is denoted by the one pair of square brackets []

  • tuple is denoted by the one pair of parentheses ()

The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.

Example # list

fruits = [] 

#### list #######
fruits = ['apples', 'grapes', 'mangoes']

## output
<class 'list'>
['apples', 'grapes', 'mangoes', 'bananas']

Example # tuple

fruits = ('apples', 'grapes', 'mangoes')
fruits[0] = 'bananas'  # error, tuple doesnot support item assignment

<class 'tuple'>
('apples', 'grapes', 'mangoes') 
TypeError: 'tuple' object does not support item assignment

Example # range

for x in range(5):
    print(x, end=' ')
0 1 2 3 4 

Text Sequence Type: str

Textual data in Python is handled with str objects, or strings. Strings are immutable sequences of Unicode code points. String literals are written in a variety of ways:

  • single quotes: 'Learner Landmark'

  • double quotes: "Learner Landmark"

  • triple quoted: '''Learner Landmark'''

name = 'Learner Landmark'

## output
<class 'str'>
Learner Landmark

Set Types: set, frozenset

A set object is an unordered collection of distinct hashable objects.

The set type is mutable — the contents can be changed using methods like add() and remove().

The frozenset type is immutable and hashable — its contents cannot be altered after it is created;

set will not allow the duplicates:

fruits = {'apples', 'grapes', 'mangoes', 'bananas'}

fruits.add('grapes') # this has no effect, if item already exist

## output
{'grapes', 'apples', 'mangoes', 'bananas'}
{'grapes', 'apples', 'mangoes', 'bananas'}

Empty set created by using set () constructor.

empty_set = {} # this is not a set, it is dict
empty_set2 = set()

<class 'dict'>
<class 'set'> 

frozenset is an immutable set object:

countries = frozenset(['India', 'USA', 'China', 'Russia', 'India'])

<class 'frozenset'>
frozenset({'Russia', 'China', 'India', 'USA'})

Mapping Types: dict

Dictionary in python is a key : value pair collection, keys are unique and they can be different types of objects. Keys are immutable they cannot be changed. Values are mutable, they can be changed.

An empty dictionary created with one pair of curly braces {}

A dictionary can be created with dict () constructor

mydict1 = {}

## output
<class 'dict'>

mydict2 = dict([(101, "Satish"), (102, "Nagesh")])

## output
{101: 'Satish', 102: 'Nagesh'}

loop through all key,value pairs:

Thanks for reading!!!

Your Rating and Review will be appreciated!!

Twitter: @LearnerLandmark

306 views0 comments

Recent Posts

See All


Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page