Advanced Data
Structures and
Dictionaries
Tuples in Python
Python Tuple is a collection of Python Programming objects much
like a list. The sequence of values stored in a tuple can be of any type,
and they are indexed by integers. Values of a tuple are syntactically
separated by ‘commas‘. Although it is not necessary, it is more
common to define a tuple by closing the sequence of values in
parentheses. This helps in understanding the Python tuples more
easily.
What is Immutable in Tuples?
Tuples are immutable. Some Characteristics of Tuples in Python:
• Like Lists, tuples are ordered and we can access their elements using
their index values
• We cannot update items to a tuple once it is created.
• Tuples cannot be appended or extended.
• We cannot remove items from a tuple once it is created.
What is Immutable in Tuples?
Accessing Values in Python Tuples
Tuples in Python provide two ways by which we can access the
elements of a tuple.
• Python Access Tuple using a Positive Index
• Access Tuple using Negative Index
Different ways of creating Tuple
• Using round brackets
• Without Brackets
• Tuple Constructor
• Empty Tuple
• Single Element Tuple
• Using Tuple Packing
Different ways of creating Tuple –
Using round brackets
Different ways of creating Tuple –
Without brackets
Different ways of creating Tuple –
Tuple Constructor
Different ways of creating Tuple –
Empty Tuple
Different ways of creating Tuple –
Single Tuple
Different ways of creating Tuple –
Using Tuple Packing
Different Operations Related to
Tuples
• Traversing
• Concatenation
• Nesting
• Repetition
• Slicing
• Deleting
• Finding the length
• Multiple Data Types with tuples
• Tuples in a Loop
Different Operations Related to
Tuples - Traversing
Different Operations Related to
Tuples – Concatenation
Different Operations Related to
Tuples – Nesting
Different Operations Related to
Tuples – Repetition
Different Operations Related to
Tuples – Slicing
Different Operations Related to
Tuples – Deleting
In this example, we are deleting a tuple using DEL keyword. The
output will be in the form of error because after deleting the tuple, it
will give a NameError.
Note: Remove individual tuple elements is not possible, but we can
delete the whole Tuple using Del keyword.
Different Operations Related to
Tuples – Finding the Length
Different Operations Related to Tuples –
Multiple Data Types with Tuple
Tuples in Python are heterogeneous in nature. This means tuples
support elements with multiple datatypes.
Different Operations Related to
Tuples – Tuples in Loop
Built-in functions in Tuple
Dictionaries in Python
A Python dictionary is a data structure that stores the value
in key: value pairs. Values in a dictionary can be of any data type and
can be duplicated, whereas keys can’t be repeated and must
be immutable.
Example: Here, The data is stored in key:value pairs in dictionaries,
which makes it easier to find values.
How to create Dictionary
A dictionary can be created by placing a sequence of elements within
curly {} braces, separated by a ‘comma’.
Accessing Dictionary items
Adding and Updating Dictionary
Items
Removing Dictionary Items
We can remove items from dictionary using the following methods:
del: Removes an item by key.
pop(): Removes an item by key and returns its value.
clear(): Empties the dictionary.
popitem(): Removes and returns the last key-value pair.
Removing Dictionary Items
Iterating through a Dictionary
Nested Dictionaries
Thank You