0% found this document useful (0 votes)
4 views29 pages

Decision Making Tools Lecture No - 07

Uploaded by

atif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views29 pages

Decision Making Tools Lecture No - 07

Uploaded by

atif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Decision Making Tools

Lecture No: 07
TUPLES

● Tuple is a collection of Python 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’.

● 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.


TUPLE
CREATING EMPTY TUPLE
Accessing

● Tuples are immutable, and usually, they contain a sequence of


heterogeneous elements that are accessed via unpacking or indexing.
● Lists are mutable, and their elements are usually homogeneous and are
accessed by iterating over the list.
TUPLE LENGTH

Tuple1 = tuple("Hello")
print(Tuple1)
print("\nFirst element of Tuple: ")
print(Tuple1[0])

# Tuple unpacking
Tuple1 = ("Hello", "World", "!")

# This line unpack


# values of Tuple1
a, b, c = Tuple1
print("\nValues after unpacking: ")
print(a)
print(b)
print(c)
Concatenation of Tuples

● Concatenation of tuple is the process of joining two or more Tuples.

● Concatenation is done by the use of ‘+’ operator.

● Concatenation of tuples is done always from the end of the original tuple.

● Other arithmetic operations do not apply on Tuples.

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')

Tuple3 = Tuple1 + Tuple2

# Printing first Tuple


print("Tuple 1: ")
print(Tuple1)

# Printing Second Tuple


print("\nTuple2: ")
print(Tuple2)

# Printing Final Tuple


print("\nTuples after Concatenation: ")
print(Tuple3)
Deleting a 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

● Dictionary in Python is a collection of keys values, used to store


data values like a map, which, unlike other data types which hold only
a single value as an element.

● Dictionary holds key:value pair. Key-Value is provided in the


dictionary to make it more optimized.
Creating a Dictionary

● In Python, a dictionary can be created by placing a sequence of


elements within curly {} braces, separated by ‘comma’.
● Dictionary holds pairs of values, one being the Key and the other
corresponding pair element being its Key:value.
● Values in a dictionary can be of any data type and can be
duplicated, whereas keys can’t be repeated and must be immutable.

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)

# Creating a Dictionary with Mixed keys


Dict = {'Name': 'Data Type', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict)
Accessing elements of a Dictionary

● In order to access the items of a dictionary refer to its key name. Key
can be used inside square brackets.

print("Accessing a element using key:")

print(Dict[1])

What will be the expected output?


Dictionary Method

● clear() – Remove all the elements from the dictionary


● copy() – Returns a copy of the dictionary
● get() – Returns the value of specified key
● items() – Returns a list containing a tuple for each key value
pair
● keys() – Returns a list containing dictionary’s keys
● pop() – Remove the element with specified key
● popitem() – Removes the last inserted key-value pair
● update() – Updates dictionary with specified key-value pairs
● values() – Returns a list of all the values of dictionary
dict1 = {1: "Python", 2: "Java", 3: "Ruby", 4:
"C++"}

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

● In Python, a Set is an unordered collection of data types that is iterable,


mutable and has no duplicate elements.
● The order of elements in a set is undefined though it may consist of various
elements.
● The major advantage of using a set is that it has a highly optimized
method for checking whether a specific element is contained in the set.

* Note: Set items are unchangeable, but you can remove items and add new
items.
Python Collection (Arrays)

There are four collection data types in the Python programming


language:
● List is a collection which is ordered and changeable. Allows
duplicate members.
● Tuple is a collection which is ordered and unchangeable.
Allows duplicate members.
● Set is a collection which is unordered, unchangeable*, and
unindexed. No duplicate members.
● Dictionary is a collection which is ordered** and changeable.
No duplicate members.
Creating a Set

● Sets can be created by using the built-in set() function with an


iterable object or a sequence by placing the sequence inside curly
braces, separated by a ‘comma’.
● A set contains only unique elements but at the time of set
creation, multiple duplicate values can also be passed.
● Order of elements in a set is undefined and is unchangeable.
● Type of elements in a set need not be the same, various mixed-up
data type values can also be passed to the set.
Example

set1 = set([1, 2, 4, 4, 3, 3, 3, 6, 5])


print("\nSet with the use of Numbers: ")
print(set1)

# Creating a Set with


# a mixed type of values
# (Having numbers and strings)
set1 = set([1, 2, 'Python', 4, 'For', 6, 'All'])
print("\nSet with the use of Mixed Values")
print(set1)
Add()

● 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)

# Adding element and tuple to


the Set
set1.add(8)
Things to learn by yourself:
set1.add(9)
set1.add((6, 7))
● update()
print("\nSet after Addition of ● remove()
Three elements: ") ● discard()
print(set1) ● clear()
● pop()
Accessing a set
● Set items cannot be accessed by referring to an index, since sets are
unordered the items has no index.
● 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.

set1 = set(["All", "is", "well"])


print("\nInitial set")
print(set1)

# Accessing element using for loop


print("\nElements of set: ")
for i in set1:
print(i, end=' ')

# Checking the element using in keyword


print("is" in set1)
● The union() method returns a new set with all items from both sets
(set1.union(set2))
● The update() method inserts the items in set2 into set1
● The intersection_update() method will keep only the items that are
present in both sets
● The intersection() method will return a new set, that only contains
the items that are present in both sets
● The symmetric_difference_update() method will keep only the
elements that are NOT present in both sets.
● The symmetric_difference() method will return a new set, that
contains only the elements that are NOT present in both sets.

You might also like