Short Tutorial: Tuples, Sets and Dictionaries in Python


Today, we will discuss the concept of different data types such as tuples, sets, and dictionaries in python. In the end of this article, we will cover some commonly asked questions on these concepts in Python.

Tuples

Tuples are similar to the list but immutable (it simply means it can’t be changed).

#Basic Syntax
<Tuple_Name> = tuple(["<Entry String1>", <Entry Number1>, ..............])

 

Sets

It is an unordered collection of unique objects. In simple terms, only unique items are considered as part of the set and repeat items are striking out.  Many operations can be performed such as Union (x|y; available in any set x and set y), Difference (x – y; available in set x not in set y)), Intersection (x&y, available in both sets x and set y) etc.

 

Dictionaries

It is an unordered key-value pair. Keys are unique and immutable objects. The value of the keys can change any time.

If you want to check the value of the name, you can use below commands.

<dictionary_name>.has_key('<key>') or '<key>' in <dictionary_name>

 

Dictionary operations

#To display all keys
<dictionary_name>.keys()
#To display all values
<dictionary_name>.values()
#To display key-value pair
<dictionary_name>.items()
#To get a value of a particular key
<dictionary_name>.get('<key>')
#To add a key-value pair in existing dictionary
<dictionary_name>['<key'] = '<value>'
#To delete a key-value pair in existing dictionary
del <dictionary_name>['<key']

 

To list out all attributes use below command:

 

To get help use below command

>>help(<dictionary_name>.__class__)

Basic Interview  Questions related to Tuples, Sets and Dictionaries in Python

Q. What are the differences between Tuples and Dictionaries in Python?

Ans: 

Tuples: It is a collection of elements in an ordered sequence.

Dictionary: It is a collection of key-value pair. Keys are used to referring value.

Q. What is the difference between Tuples vs List in Python?

Ans: 

Tuples: It is a collection of elements  (or values) in an ordered sequence but immutable (can’t be changed).

List: It is a collection of elements (or values) in an ordered sequence but mutable (can be changed).

Q. Can we create a list of tuples in Python?

Ans: Yes, refer below example:

ExampleListofTuples= [("z", 23), ("r", 2), ("a", 1), ("g", 3)]

Q. Can we append a dictionary with a new key-value pair in Python?

Ans: Yes, refer below example:

StudentRollNoMapping = ({"Rahul":56"}, {"Mohit":93", {"Chitranjan":86})
# Adding a new key value pair (new student and roll number) 
StudentRollNoMapping.update( {'Sanjay' : 89} )

Q. What are dictionary keys in Python?

Ans: Python dictionary is a one to one of the key (it should be unique) and value. Dictionaries are unordered and mutable.

Q. Can we create a dictionary of dictionaries in Python? or What is the nested dictionary in Python?

Ans: Yes, we can create a dictionary of dictionaries which also called nested dictionary in Python. In below example, we have created dictionary student which contains another dictionary student profile information.

#Example
>>>Student= {1: {'name': 'Sameer', 'age': '22', 'sex': 'Male', 'course':'B.Tech'}, 2: {'name': 'Rosie', 'age': '22', 'sex': 'Female', 'course':'B.Sc.'}}
>>>print(Student)

 


Subscribe us to receive more such articles updates in your email.

If you have any questions, feel free to ask in the comments section below. Nothing gives me greater joy than helping my readers!

Disclaimer: This tutorial is for educational purpose only. Individual is solely responsible for any illegal act.

Comments:

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

 
error: