0% found this document useful (0 votes)
2 views19 pages

Tuples and Dictionaries

The document provides an overview of tuples and dictionaries in Python, highlighting their characteristics and functionalities. Tuples are immutable, can contain various data types, and support operations like indexing, slicing, and concatenation, while dictionaries allow for key-value pairs with non-integer keys and support operations like adding, deleting, and iterating over elements. The document also outlines built-in functions available for both data structures, emphasizing their differences and use cases.

Uploaded by

dazesoraa
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)
2 views19 pages

Tuples and Dictionaries

The document provides an overview of tuples and dictionaries in Python, highlighting their characteristics and functionalities. Tuples are immutable, can contain various data types, and support operations like indexing, slicing, and concatenation, while dictionaries allow for key-value pairs with non-integer keys and support operations like adding, deleting, and iterating over elements. The document also outlines built-in functions available for both data structures, emphasizing their differences and use cases.

Uploaded by

dazesoraa
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/ 19

Tuples

• Tuples are very similar to lists, but they are immutable (i.e.,
unchangeable)

• Tuples are written with round brackets as follows:


t1 = (1, 2, 3)
t2 = (“a”, “b”, “c”, ”d”)
t3 = (200, “A”, [4, 5], 3.2)
print(t1)
print(t2)
print(t3)
Tuples
• Like lists, tuples can:
• Contain any and different types of elements
• Contain duplicate elements (e.g., (1, 1, 2))
• Be indexed exactly in the same way (i.e., using the [] brackets)
• Be sliced exactly in the same way (i.e., using the [::] notation)
• Be concatenated (e.g., t = (1, 2, 3) + (“a”, “b”, “c”))
• Be repeated (e.g., t = (“a”, “b”) * 10)
• Be nested (e.g., t = ((1, 2), (3, 4), ((“a”, “b”, ”c”), 3.4))
• Be passed to a function, but will result in pass-by-value and not pass-by-
reference outcome since it is immutable
• Be iterated over
Examples
This will print the elements of t1 in
t1 = ("a", "b", "c") a reversed order, but will not change
print(t1[::-1]) t1 itself since it is immutable
t2 = ("a", "b", "c")
t3 = t1 + t2 This will concatenate t1 and t2 and
print(t3) assign the result to t3 (again, t1 and
t3 = t3 * 4 t2 will be unchanged since they are
print(t3) immutable)
This will repeat t3 four times and
for i in t3: assign the result to t3. Hence,
print(i, end = " ") t3 will be overwritten (i.e., NOT
changed in place- because it is
print() immutable-, but redefined with a
new value)
Examples
This is an example of nesting, where
t4 = ((1, 2, 3), ("a", "b", "c")) a matrix with 2 rows and
for j in t4: 3 columns is created. The first row
for k in j: includes the elements 1, 2, and 3.
print(k,end = " ") The second row includes the elements
print() “a”, “b”, and “c”.

This outer loop iterates over each


element in t4; that is, it gets first the
This inner loop iterates over each element (1, 2, 3) and second the
element read by the outer loop; element (“a”, “b”, “c”)
that is, it first iterates over the
elements of the element (1, 2, 3),
and second it iterates over the
elements of the element (“a”, “b”,
Examples
This change on t remains local to the function
def func1(t):
since a value of t was passed and not a
t=t*2
reference to it
t = (1, 2, 3)
print(t) This will output (1, 2, 3)
func1(t)
print(t) This will also output (1, 2, 3) since
tuples are immutable, hence, will
always exhibit a passed-by-value
behavior
Using Functions with Tuples
• You can also use functions with tuples
The count(x) function returns the number of
t1 = (1, 2, 3, 1, 5, 1) elements with the specified value x (e.g., x is 1
print(t1.count(1)) in this example)
print(t1.index(1))
Output: The index(x) function returns the index of the
3 first element with the specified value x (e.g., x
0 is 1 in this example)

In fact, Python has only these two built-in functions that can be used
on tuples
Towards Dictionaries
• Lists and tuples hold elements with only integer indices

45 “Coding” 4.5 7 89
0 1 2 3 4
Integer
Indices

• So in essence, each element has an index (or a key) which can only be
an integer, and a value which can be of any type (e.g., in the above
list/tuple, the first element has key 0 and value 45)
• What if we want to store elements with non-integer indices (or keys)?
Dictionaries
• In Python, you can use a dictionary to store elements with keys of any
types (not necessarily only integers like lists and tuples) and values of
any types as well
45 “Coding” 4.5 7 89
“NUM” 1000 2000 3.4 “XXX”

keys of different types Values of different types

• The above dictionary can be defined in Python as follows:


dic = {"NUM":45, 1000:"coding", 2000:4.5, 3.4:7, "XXX":89}
key value
Each element is a key:value pair, and elements are separated by commas
Dictionaries
• In summary, dictionaries:
• Can contain any and different types of elements (i.e., keys and values)
• Can contain only unique keys but duplicate values

dic2 = {"a":1, "a":2, "b":2}


Output: {'a': 2, 'b': 2}
print(dic2)

The element “a”:2 will override the element “a”:1


because only ONE element can have key “a”

• Can be indexed but only through keys (i.e., dic2[“a”] will return 1 but dic2[0]
will return an error since there is no element with key 0 in dic2 above)
Dictionaries
• In summary, dictionaries:
• CANNOT be concatenated
• CANNOT be repeated
• Can be nested (e.g., d = {"first":{1:1}, "second":{2:"a"}}
• Can be passed to a function and will result in a pass-by-reference and not
pass-by-value behavior since it is immutable (like lists)
def func1(d):
d["first"] = [1, 2, 3]
Output:
dic = {"first":{1:1}, "second": {'first': {1: 1}, 'second': {2: 'a'}}
{2:"a"}} {'first': [1, 2, 3], 'second': {2: 'a'}}
print(dic)
func1(dic)
print(dic)
Dictionaries
• In summary, dictionaries:
• Can be iterated over
dic = {"first": 1, "second": 2, "third": 3}
for i in dic:
print(i)

first ONLY the keys will be returned.


Output: second
third How to get the values?
Dictionaries
• In summary, dictionaries:
• Can be iterated over
dic = {"first": 1, "second": 2, "third": 3}
for i in dic:
print(dic[i])

1
Output: 2 Values can be accessed via indexing!
3
Adding Elements to a Dictionary
• How to add elements to a dictionary?
• By indexing the dictionary via a key and assigning a corresponding value
dic = {"first": 1, "second": 2, "third": 3}
print(dic)
dic["fourth"] = 4
print(dic)

{'first': 1, 'second': 2, 'third': 3}


Output:
{'first': 1, 'second': 2, 'third': 3, 'fourth': 4}
Adding Elements to a Dictionary
• How to add elements to a dictionary?
• By indexing the dictionary via a key and assigning a corresponding value
dic = {"first": 1, "second": 2, "third": 3}
print(dic)
dic[”second"] = 4 If the key already exists,
print(dic) the value will be overridden

{'first': 1, 'second': 2, 'third': 3}


Output:
{'first': 1, 'second’: 4, 'third': 3}
Deleting Elements to a Dictionary
• How to delete elements in a dictionary?
• By using del

dic = {"first": 1, "second": 2, "third": 3} Output:


print(dic)
dic["fourth"] = 4 {'first': 1, 'second': 2, 'third': 3}
print(dic) {'first': 1, 'second': 2, 'third': 3, 'fourth': 4}
del dic["first"] {'second': 2, 'third': 3, 'fourth': 4}
print(dic)
Deleting Elements to a Dictionary
• How to delete elements in a dictionary?
• Or by using the function pop(key)

dic = {"first": 1, "second": 2, "third": 3} Output:


print(dic)
dic["fourth"] = 4 {'first': 1, 'second': 2, 'third': 3}
print(dic) {'first': 1, 'second': 2, 'third': 3, 'fourth': 4}
dic.pop(“first”) {'second': 2, 'third': 3, 'fourth': 4}
print(dic)
Dictionary Functions
• Many other functions can also be used with dictionaries

Function Description
dic.clear() Removes all the elements from dictionary dic
dic.copy() Returns a copy of dictionary dic
dic.items() Returns a list containing a tuple for each key-value pair in
dictionary dic
dic.get(k) Returns the value of the specified key k from dictionary dic
dic.keys() Returns a list containing all the keys of dictionary dic
dic.pop(k) Removes the element with the specified key k from
dictionary dic
Dictionary Functions
• Many other functions can also be used with dictionaries

Function Description
dic.popitem() Removes the last inserted key-value pair in dictionary dic
dic.values() Returns a list of all the values in dictionary dic
Next Lecture…
• Practice on Tuples and Dictionaries

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy