Basic Datatypes in Python Dictionary, Tuple, and Set

Building blocks of Python language

Python Datatypes

Please refer to the previous article for Python Datatypes Integer, Float, String, and List data types. In the present article, we will learn and practice Complex Numbers, collection data types Dictionary, Tuple, and Set.


Python dictionaries are stored in Key-Value pairs. Unlike List dictionaries are unordered. Dictionary is mutable i.e key value can be reassigned or changed.

Python dictionaries are written with curly brackets

{'India': 'Sachin', 'England': 'Root'}
#keys are India and England 
#Values are Sachin and Root
#dictionary can be used to store different data types
>>>dict_python={'d1':123,'d2':124.5,'d3':[1,2,3,4],'d4':'Python is Best'}
{'d1': 123, 'd2': 124.5, 'd3': [1, 2, 3, 4], 'd4': 'Python is Best'}

Access Elements through Key in Dictionary.

#cric_stars,dict_python are defined above
Python is Best

If the dictionary contains List or String then We can use slicing/indexing to retrieve particular elements from the list/String.

#what if I have to display Best from dictionary #dict_python
#please think
#I have used slicing#please visit previous article
#for details about slicing
#to do:-display 3 and 4 from key d3 of dictionary

Nested Dictionary

Similar to Nested List,Nested Dictionary is also possible.

#in example cited below value of key d1 is another #dictionary
{'d1': {'d2': ['Sachin', 'Root']}, 'd3': 24}
#now if i have to display root then first I have to get #value of key d1
#after that i have to get value of key d2
#after that we need to access 2nd element of list


Tuples are like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.

#tuple datatype in python
#unlike list they are not mutable,once assigned there #value cannot be changed
#instead of square braces they are enclosed in  #parentheses
#just like list elements of tuple can be accessed by index
('john', 2)
#Tuple are not mutable  i.e its value cannot be changed
#it will throw error if we try to reassign value to any #element of tuple
TypeError  Traceback (most recent call last)
<ipython-input-30-dea060296600> in <module>
      2 #it will throw error if we try to reassign value to any element of tuple
----> 4 var_tuple[0]='Sachin'
TypeError: 'tuple' object does not support item assignment


Set datatype in python is a collection of Unique elements i.e no duplicate are allowed.

The curly bracket is used to define set in python.

Set elements cannot be accessed through an index, unlike List and Tuple.

{1, 2, 3, 'john', 'python', 'sachin'}
#if you type in duplicate value while assigning set then it #will not be saved in set
#for example
#you will get only unique elements
{10, 20, 30, 'john'}
#In list append is used to add elements in the list #whereas #in set add function is used to
#add value in set
{10, 20, 30, 'john', 'michal'}
#update() method can be used to add
#more that one value together in Set
{10, 12, 20, 30, 9, 'Rocky', 'john', 'michal'}
#set can also be used to extract unique elements from #List
#you can think of it as type conversion
#extracting unique value from list
#think of it like type conversion
{1, 2, 3, 4, 'john'}

Accessing elements of Set

Set elements cannot be accessed through Index.

For loop may be used to access Set Elements.

>>>for x in set_var:

Complex Numbers

Complex numbers are specified as <real part>+<imaginary part>j.


Python Datatypes are actually classes and variables are instance (object) of these classes.

Happy learning👇👇👇(Please refresh if you do not see Github gist below )

Previous:- Basic data types in Python

2 thoughts on “Basic Datatypes in Python Dictionary, Tuple, and Set”

Leave a Comment

Your email address will not be published. Required fields are marked *