Data STR - Notes
Data STR - Notes
List in Python:
3.1.1 Defining lists,
accessing values in list,
deleting values in list,
updating lists.
3.1.2 Basic List Operations
3.1.3 Built - in List functions
List in Python:
�The most basic data structure in Python is the sequence.
�Each element of a sequence is assigned a number – its position or index.
�The first index is zero, the second index is one, and so forth.
�Python has six built-in types of sequences(strings, Unicode strings, lists, tuples, buffers, and
xrange objects.) , but the most common ones are lists and tuples.
�There are certain things you can do with all sequence types. These operations include
indexing, slicing, adding, multiplying, and checking for membership.
� In addition, Python has built-in functions for finding the length of a sequence and for finding
its largest and smallest elements.
Tuple in Python:
3.2.1 Defining Tuples,
accessing values in Tuples,
deleting values in Tuples,
updating Tuples.
3.2.2 Basic Tuple Operations
3.2.3 Built - in Tuple functions
# Difference between List & Tuple
Tuple in Python:
�Tuple with only single element:
�Note: When a tuple has only one element, we must put a comma after the element, otherwise
Python will not treat it as a tuple.
�# a tuple with single data item
my_data = (99,)
If we do not put comma after 99 in the above example then
python will treat my_data as an int variable rather than a tuple.
Sets in Python:
3.2.1 Defining Sets,
accessing values in Sets,
deleting values in Sets,
updating Sets.
3.2.2 Basic Set Operations
3.2.3 Built - in Set functions
# Difference between List, Tuple and set
# Advantages/features of sets
Sets in Python:
�The set in python can be defined as the unordered collection of various items enclosed within
the curly braces i.e. { }.
�The elements of the set can not be duplicate.
�The Set is mutable but elements of set must be immutable.
�However, the set itself is mutable. We can add or remove items from it.
�Sets can be used to perform mathematical set operations like union, intersection, symmetric
difference etc.
�Unlike other collections in python, there is no index attached to the elements of the set, i.e.,
we cannot directly access any element of the set by the index.
�However, we can print them all together or we can get the list of elements by looping through
the set.
Sets in Python:
�Mathematically a set is a collection of items not in any particular order.
�A Python set is similar to this mathematical definition with below additional conditions.
�The elements in the set cannot be duplicates.
�The elements in the set are immutable(cannot be modified) but the set as a whole is mutable.
�There is no index attached to any element in a python set.
�So they do not support any indexing or slicing operation.
print("sorry")
#O/P
1,yes
2,3,4,sorry
e.g.3
l1 = ["eat","sleep","repeat"]
obj1 = enumerate(l1)
print ("Return type:",type(obj1))
print (list(enumerate(l1,2)))
Output
Return type: <class 'enumerate'>
[(2, 'eat'), (3, 'sleep'), (4, 'repeat')]
#Advantages of Sets:
�Because sets cannot have multiple occurrences of the
same element, it makes sets highly useful to efficiently
remove duplicate values from a list or tuple and to
perform common math operations like unions and
intersections.
�E.g.
>>> l=[2,3,4,5,2,3,4,5]
>>> s=set() #empty set creation.
>>> s=set(l)
>>> s
{2, 3, 4, 5}
Dictionary in Python:
3.2.1 Defining Dictionary,
accessing values in Dictionary,
deleting values in Dictionary,
updating Dictionary.
3.2.2 Basic Dictionary Operations
3.2.3 Built - in Dictionary functions
# Difference between List, Tuple, set and Dictionary
# Advantages/features of Dictionary
Dictionary in Python:
�A real-life dictionary holds words and their meanings. As you can imagine, likewise, a Python
dictionary holds key-value pairs.
�Dictionary in Python is an unordered collection of data values, used to store data values like a
map, which unlike other Data Types that hold only single value as an element.
�Dictionary holds key:value pair.
�Key value is provided in the dictionary to make it more optimized.
�Dictionaries are optimized to retrieve values when the key is known.
�Note – Keys in a dictionary doesn‟t allows Polymorphism.
Dictionary in Python:
�Each key is separated from its value by a colon (:), the items are separated by commas, and the
whole thing is enclosed in curly braces.
�An empty dictionary without any items is written with just two curly braces, like this: {}.
�Keys are unique within a dictionary while values may not be.
�The values of a dictionary can be of any type, but the keysmust be of an immutable data type
such as strings,numbers, or tuples.
�Note – Dictionary keys are case sensitive, same name but
different cases of Key will be treated distinctly.
Dictionary in Python:
Accessing Elements in Dictionary
�While indexing is used with other container types to
access values, dictionary uses keys. Key can be used
either inside square brackets or with the get() method.
�The difference while using get() is that it
returns None instead of KeyError, if the key is not
found.
�E.g.
>>> my_dict={1:34,2:56,3:89,5:67}
>>> my_dict.get(3)
89
>>> my_dict.get(4)
Dictionary in Python:
Accessing Elements in Dictionary
Example:
>>> my_dict={1:34,2:56,3:89,5:67}
>>> my_dict
{1: 34, 2: 56, 3: 89, 5: 67}
>>> my_dict[2]
56
>>> my_dict[4]
Traceback (most recent call last):
File "<pyshell>", line 1, in <module>
KeyError: 4
Dictionary in Python:
Add/Update Elements in Dictionary
�Dictionaries are mutable. We can add new items or change the
value of existing items using assignment operator.
� If the key is already present, value gets updated, else a new
key: value pair is added to the dictionary.
� In Python Dictionary, Addition of elements can be done in
multiple ways. One value at a time can be added to a Dictionary
by defining value along with the key e.g. Dict[Key] = ‘Value’.
�Updating an existing value in a Dictionary can be done by using
the built-in update() method.
�Note- While adding a value, if the key value already exists, the
value gets updated otherwise a new Key with the value is added
to the Dictionary.
Dictionary in Python:
Add/Update Elements in Dictionary
Example:
>>> my_dict={1:34,2:56,3:89,5:67}
>>> my_dict
{1: 34, 2: 56, 3: 89, 5: 67}
>>> my_dict[4]=222 #Add new key:value
>>> my_dict
{1: 34, 2: 56, 3: 89, 5: 67, 4: 222}
>>> my_dict[3]=48 #Update existing value
>>> my_dict
{1: 34, 2: 56, 3: 48, 5: 67, 4: 222}
Dictionary in Python:
Add/Update Elements in Dictionary
Example:
�>>> d={1:23,2:45,5:{4,5,6},8:89}
�>>> d
�{1: 23, 2: 45, 5: {4, 5, 6}, 8: 89}
�>>> d[5]
�{4, 5, 6}
�>>> type(d[5])
�<class 'set'>
�>>> d={1:23,2:45,5:{4:55,5:66,6:77},8:89}
�>>> d
�{1: 23, 2: 45, 5: {4: 55, 5: 66, 6: 77}, 8: 89}
�>>> type(d[5])
�<class 'dict'>
Dictionary in Python:
Add/Update Elements in Dictionary
Example:
�>>> d={1:23,2:45,5:{4,5,6},8:89}
�>>> d
�{1: 23, 2: 45, 5: {4, 5, 6}, 8: 89}
�>>> d[5]
�{4, 5, 6}
�>>> type(d[5])
�<class 'set'>
�>>> d={1:23,2:45,5:{4:55,5:66,6:77},8:89}
�>>> d
�{1: 23, 2: 45, 5: {4: 55, 5: 66, 6: 77}, 8: 89}
�>>> type(d[5])
�<class 'dict'>
Dictionary in Python:
Accessing Elements from Dictionary
Example:
�>>> d={1:23,2:45,5:{4:{12:14,13:15},5:66,6:77},8:89}
�>>> d
�{1: 23, 2: 45, 5: {4: {12: 14, 13: 15}, 5: 66, 6: 77}, 8: 89}
�>>> d[1]
�23
�>>> d[5][5]
�66
�>>> d[5][4][12]
�14
Dictionary in Python:
Delete Elements from Dictionary
� In Python Dictionary, deletion of keys can be done by
using the del keyword.
�Using del keyword, specific values from a dictionary as
well as whole dictionary can be deleted.
� Items in a Nested dictionary can also be deleted by using
del keyword and providing specific nested key and
particular key to be deleted from that nested Dictionary.
�Note- del will delete the entire dictionary and hence
printing it after deletion will raise an Error.
Dictionary in Python:
Delete Elements from Dictionary
Example:
>>>d={1:23,2:45,5:{4:{12:14,13:15},5:66,6:77},8:89}
>>>del d[2]
>>> d
{1: 23, 5: {4: {12: 14, 13: 15}, 5: 66, 6: 77}, 8: 89}
>>> del d[5][5]
>>> d
{1: 23, 5: {4: {12: 14, 13: 15}, 6: 77}, 8: 89}
>>> del d
>>> d
Traceback (most recent call last):
File "<pyshell>", line 1, in <module>
NameError: name 'd' is not defined
Dictionary in Python:
Built-in Functions
>>> Dictionary1
{1: 5, 2: 'b', 3: 5, 4: None, 5: 5, 7: 5}
>>> any(Dictionary1)
True
>>> all(Dictionary1)
True
>>> max(Dictionary1)
7
>>> min(Dictionary1)
1
>>> len(Dictionary1)
6
>>> sorted(Dictionary1)
[1, 2, 3, 4, 5, 7]