Python
Data Structures
Session II
Norms
Take Notes! Do It With Me! Participate!
Agenda
1. Data structures and its types
2. Lists
3. List Comprehension
4. Tuples
5. Tuple Unpacking
6. Dictionaries
7. Sets
What are Data structures?
Data structures!
● Data structures are “containers” that
organize and group data according to
type.
● The data structures differ based on
mutability and order.
Types of Data Structures!
The non-primitive data structures in
Python include
● List
● Tuples
● Dictionary
● Set
Each of the data structures is unique in
its own way.
What are Lists?
Lists!
● In Python Lists are used to store
multiple items in a single variable.
● Since they are stored in a
sequential order, a special
number based on their position in
the list called index is assigned to
them and they are accessed using
these index values.
Lists!
● These indexes start from zero and
are assigned in an increasing
order i.e. from zero to n – 1 where
n is number of items in that lists.
● These lists can store data of
multiple data types, e.g. Integer,
String, Float etc.
Lists!
● Created using square brackets.
● List is mutable, meaning that we can
change, add, and remove items in a
list after it has been created.
● Since they are indexed, lists can
have items with the same value.
Can you give a real life example of Lists?
How to use lists in python?
Lists!
Create a list and practise.
Lists!
Access items of a list.
Lists!
Change value of an item in a list.
Lists!
Add and insert items in a list.
Lists!
Remove specific item from a list.
List Comprehension!
● List comprehension offers a shorter
syntax when you want to create a new
list based on the values of an existing
list.
● Eg: Based on a list of fruits, you want a
new list, containing only the fruits with
the letter "a" in the name.
List Comprehension!
With and Without list comprehension:
Have you understood the use of
List comprehension?
Activity!
1. Add Papaya to the list.
2. Replace Apple with Grapes.
What is a tuple?
Tuples!
● Used to store multiple items in a
single variable.
● Is one of 4 built-in data types in
Python.
● We can store collections of data
using tuples.
● They are written with round brackets.
Well, Tuples are very similar to Lists!!
Can you give a real life example of tuples?
How to use a tuple?
Tuples!
Create a Tuple and practise.
Tuples!
Access items of a tuple.
Tuples!
● Once a tuple is created, you cannot change its values. Tuples are unchangeable,
or immutable as it also is called.
● But there is a workaround. You can convert the tuple into a list, change the list,
and convert the list back into a tuple.
Unpacking a Tuple!
● When we create a tuple, we normally assign values to it. This is called
"packing" a tuple.
● But, in Python, we are also allowed to extract the values back into
variables. This is called "unpacking".
Activity!
● Create a new tuple with a sting, an integer
and a boolean in it.
● Convert a tuple into a list.
● Unpack the tuple.
What is a dictionary in
python?
Dictionaries!
● Dictionary in Python is an unordered
collection of data values.
● Used to store data values in a
key:value pair manner, which, unlike
other data types, can hold only a single
valued data as the value in the
key:value pair.
Dictionaries!
● A single dictionary element can hold
multiple key:value pairs.
● Dictionaries are written with curly
brackets, and have keys and values.
● Dictionaries are changeable/ mutable.
Can you give a real life example of
dictionary?
How to use dictionaries in
python?
Dictionaries!
Create and print a dictionary
Dictionaries!
Access items in a dictionary.
Dictionaries!
Change values of items in dictionary.
Dictionaries!
Add items to a dictionary.
Dictionaries!
Remove items from a dictionary.
Activity!
● Print a dictionary, which has key- value pairs
of capital cities of India.
● Remove the capital city of Telangana from
the dictionary.
● Replace any of the capital city with
Hyderabad.
What is a Set?
Sets!
● Set is one of 4 built-in data types in
Python used to store collections of data.
● Used to store multiple items in a single
variable.
● A set is mutable & unordered.
● Sets cannot have two items with the same
value.
Can you give a real life example of Sets?
How to use Sets in
python?
Sets!
Create a set and practise.
Sets!
● You cannot access items in a set by
referring to an index or a key.
● But you can loop through the set items
using a for loop, or ask if a specified
value is present in a set, by using the in
keyword.
Sets!
Sets!
To add, use add command or update command.
Sets!
To remove an item in a set, use the remove(), or the discard() method.
Have you understood about Sets?
Activity!
● Prepare a set using contact numbers of your
family members.
● Now replace your contact number with an
alternative number.
What we learnt today!
● All about Data structures
○ Lists
○ Tuples
○ Dictionaries
○ Sets
Time for Feedback!
1. Click on the feedback form link.
2. Enter your personal details.
3. Rate the session based on
a. Overall Experience
b. Trainer
c. Content
4. Then click on Submit…!
Doubts?
Key Takeaways
What is your one key takeaway from the session?