Decision Making Tools Lecture No - 07
Decision Making Tools Lecture No - 07
Lecture No: 07
TUPLES
● The sequence of values stored in a tuple can be of any type, and they are
indexed by integers.
Tuple1 = tuple("Hello")
print(Tuple1)
print("\nFirst element of Tuple: ")
print(Tuple1[0])
# Tuple unpacking
Tuple1 = ("Hello", "World", "!")
● Concatenation of tuples is done always from the end of the original tuple.
Note- Only the same data types can be combined with concatenation, an error
arises if a list and a tuple are combined.
Concatenation of Tuples
Tuple1 = (0, 1, 2, 3)
Tuple2 = ('String', 'in', 'Tuple')
● Tuples are immutable and hence they do not allow deletion of a part of it.
The entire tuple gets deleted by the use of del() method.
Tuple1 = (0, 1, 2, 3, 4)
print(Tuple1)
del Tuple1
print(Tuple1)
Tuple Methods
● Python has two built-in methods that you can use on tuples.
Tuple vs Lists
DICTIONARY
Dictionary
Note – Dictionary keys are case sensitive, the same name but different
cases of Key will be treated distinctly.
Cont’d
Dict = {}
print("Empty Dictionary: ")
print(Dict)
# Creating a Dictionary
# with dict() method
Dict = dict({1: 'Lists', 2: 'Sets', 3: 'Tuples', 4:
'Dictionary'})
print("\nDictionary with the use of dict(): ")
print(Dict)
● In order to access the items of a dictionary refer to its key name. Key
can be used inside square brackets.
print(Dict[1])
dict2 = dict1.copy()
print(dict2)
dict1.clear()
print(dict1)
print(dict2.get(1))
print(dict2.items())
print(dict2.keys())
dict2.pop(4)
print(dict2)
dict2.popitem()
print(dict2)
dict2.update({3: "C++"})
print(dict2)
print(dict2.values())
SETS
Python Sets
* Note: Set items are unchangeable, but you can remove items and add new
items.
Python Collection (Arrays)
● Elements can be added to the Set by using the built-in add() function.
● Only one element at a time can be added to the set by using add() method.
● Loops are used to add multiple elements at a time with the use of add()
method.
set1 = set()
print("Initial blank Set: ")
print(set1)