Python tutorial

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.

Dictionary

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

>>>cric_stars={'India':'Sachin','England':'Root'}
>>cric_stars
{'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'}
>>>print(dict_python)
{'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
>>>cric_stars['India']
'Sachin'
>>>cric_stars['England']
'Root'
>>>print(dict_python['d4'])
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
>>>print(dict_python['d4'][10:14])
Best
#to do:-display 3 and 4 from key d3 of dictionary
#dict_python

Nested Dictionary

Similar to Nested List,Nested Dictionary is also possible.

#in example cited below value of key d1 is another #dictionary
nested_dict={'d1':{'d2':['Sachin','Root']},'d3':24}
>>>print(nested_dict)
{'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
>>>nested_dict['d1']['d2'][1]
'Root'
>>>type(nested_dict)
dict

Tuple

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
>>>var_tuple=('john',2,3,4)
>>>type(var_tuple)
tuple
#just like list elements of tuple can be accessed by index
>>>var_tuple[0]
'john'
>>>var_tuple[0:2]
('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
>>>var_tuple[0]='Sachin'
---------------------------------------------------------------------------
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
      3 
----> 4 var_tuple[0]='Sachin'
TypeError: 'tuple' object does not support item assignment

Set

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.

>>>set_var={'john','python','sachin',1,2,3}
>>>set_var
{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
>>>set_var={10,10,10,20,20,20,'john','john','john',30}
#you will get only unique elements
>>>set_var
{10, 20, 30, 'john'}
>>>type(set_var)
set
#In list append is used to add elements in the list #whereas #in set add function is used to
#add value in set
>>>set_var.add('michal')
>>>set_var
{10, 20, 30, 'john', 'michal'}
#update() method can be used to add
#more that one value together in Set
>>>set_var.update(["Rocky",9,12])
>>>set_var
{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
var_list=[1,1,2,2,3,3,4,4,'john','john']
#extracting unique value from list
#think of it like type conversion
>>>set(var_list)
{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:
           print(x)
9
10
john
12
Rocky
20
michal
30

Complex Numbers

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

>>>var_complex=5+3j
>>>type(var_complex)
complex

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

By ANISH SINGH

IT Analyst at Tata Consultancy Services.

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

Leave a Reply

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