module3_python
module3_python
Learning Objective:
1. Recognize the key characteristics of each structure. Correctly utilize each structure when appropriate
and access the corresponding data stored on the structure.
3.1: List
List: a mutable data structure that stores elements in an unordered format, like an array.
"""
Elements in list does NOT have to be the same type, but this is uncommon.
In this case, each list could represent the series of information about a p
erson,
but you will need to remember what information is stored at each index. ---
> There is a better option for this purpose - dictionary.
"""
list3 = ["John", "male", 20, False]
Out[ ]: 'hallo'
https://cdn.evg.gov.br/cursos/338_EVG/htmls/modulo03_html01.html 1/10
27/12/2024, 16:38 module3_python
In [ ]: # Remove an element from the list by specifying the element that you want t
o remove
list2.remove('hello')
In [ ]: """
Another way to remove an element: pop()
pop() allows you to identify the position you
"""
list2.append("hello")
list2.pop()
list2
In [ ]: """
Lists could also be sorted.
Method of sorting depends on how the comparable interface is implemented fo
r the objects in the list.
In [ ]: """
Since list is dynamic, meaning that the size of the list grow or shrink as
we insert or remove elements,
we could call len() to find the size of the list at a given time.
"""
size of list1 = 0
size of list2 = 4
https://cdn.evg.gov.br/cursos/338_EVG/htmls/modulo03_html01.html 2/10
27/12/2024, 16:38 module3_python
Out[ ]: 'bye,hallo,hola,olá'
lists = []
lists.append([1,2,3])
lists.append(['a','b','c'])
lists
lists[1]
In [ ]: lists[1][0]
Out[ ]: 'a'
3.2: Tuple
They support all the operations lists supports, except for those that modify the list.
--------------------------------------------------------------------------
-
AttributeError Traceback (most recent call las
t)
<ipython-input-30-89cbd0c10841> in <module>()
1 # ERROR: canot add to a tuple
----> 2 x.append(4)
https://cdn.evg.gov.br/cursos/338_EVG/htmls/modulo03_html01.html 3/10
27/12/2024, 16:38 module3_python
In [ ]: # This is OK because creating a new tuple with x and (4,5,6) added at the e
nd
x + (4,5,6)
Out[ ]: (1, 2, 3, 4, 5, 6)
Out[ ]: (1, 2, 3)
Out[ ]: (1, 2, 3, 1, 2, 3)
Out[ ]: 2
In [ ]: # shorthand for
# (a,b,c) = (1,2,3)
a,b,c = 1,2,3
In [ ]: a
Out[ ]: 1
In [ ]: b
Out[ ]: 2
In [ ]: c
Out[ ]: 3
Out[ ]: [1, 2, 3, 4]
Out[ ]: (1, 2, 3, 4)
https://cdn.evg.gov.br/cursos/338_EVG/htmls/modulo03_html01.html 4/10
27/12/2024, 16:38 module3_python
# "Pack"/associate each element of the tuple with a label. Note the order o
f labels.
first, last, age = person
In [ ]: first
Out[ ]: 'Jane'
In [ ]: last
Out[ ]: 'Doe'
In [ ]: age
Out[ ]: 21
a,b,c = x
--------------------------------------------------------------------------
-
ValueError Traceback (most recent call las
t)
<ipython-input-45-4acb77e9e93d> in <module>()
3 x = (1,2,3,4)
4
----> 5 a,b,c = x
In [ ]: # This is OK!
x = [1,2,3,4]
a,b,c,d = x
In [ ]: a
Out[ ]: 1
In [ ]: b
Out[ ]: 2
In [ ]: c
Out[ ]: 3
In [ ]: d
Out[ ]: 4
https://cdn.evg.gov.br/cursos/338_EVG/htmls/modulo03_html01.html 5/10
27/12/2024, 16:38 module3_python
3.3: Set
Set: a mutable data structure that stores non-duplicated, immutable objects and sorts the elements in
ascending order. Every element in the set is unique.
Out[ ]: set()
ex1
Out[ ]: {1, 2}
Out[ ]: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
d_set
--------------------------------------------------------------------------
-
TypeError Traceback (most recent call las
t)
<ipython-input-55-6b6994b2706f> in <module>()
1 # mutable objects can't go in a set
----> 2 d_set = {[1,2,3]}
3
4 d_set
https://cdn.evg.gov.br/cursos/338_EVG/htmls/modulo03_html01.html 6/10
27/12/2024, 16:38 module3_python
set_of_ages = set(ages)
set_of_ages
list_of_ages
tuple_of_ages
{1,2,3} == {2,1,3}
Out[ ]: True
3.4: Dictionary
Dictionary: a data structure that stores key-value pairs in which the keys MUST be immutatble objects.
Out[ ]: 10
dict2['b']
Out[ ]: 50
https://cdn.evg.gov.br/cursos/338_EVG/htmls/modulo03_html01.html 7/10
27/12/2024, 16:38 module3_python
In [ ]: # What happen if we want to access the value for a non-existing key? (e.g.
'z')
--------------------------------------------------------------------------
-
KeyError Traceback (most recent call las
t)
<ipython-input-63-ddfe9c24c2d2> in <module>()
4 # We might expect an ERROR because this key doesn't exist, so it d
oesn't have a value.
5 # That is a correct assumption.
----> 6 dict2['z']
KeyError: 'z'
dict2['z']
Out[ ]: 999
WHY??
The previous block of code works because we "update" the value of the key first, update = insert (in this
case), and by the time we want to access the value of this key, this key already exists in the dictionary for the
assigned mapping.
dict
https://cdn.evg.gov.br/cursos/338_EVG/htmls/modulo03_html01.html 8/10
27/12/2024, 16:38 module3_python
# Because we could modify the list by inserting new element, sorting elemen
ts, deleting elements, or other ways of modifying it, it CANNOT be a key
--------------------------------------------------------------------------
-
TypeError Traceback (most recent call las
t)
<ipython-input-66-7e50947ec831> in <module>()
9 # Because we could modify the list by inserting new element, sorti
ng elements, deleting elements, or other ways of modifying it, it CANNOT b
e a key
10
---> 11 dict[['a','b', 'c']] = [False, True, False]
In [ ]: # But since tuple is immutatble, we could replace the list with a tuple
dict[('a','b', 'c')] = [False, True, False]
dict
In [ ]: # Or all values
dict.values()
Out[ ]: dict_values(['hello message', ['a', 'b', 'c', 'd', 'e'], False, 800778234
6, [False, True, False]])
https://cdn.evg.gov.br/cursos/338_EVG/htmls/modulo03_html01.html 9/10
27/12/2024, 16:38 module3_python
In [ ]:
https://cdn.evg.gov.br/cursos/338_EVG/htmls/modulo03_html01.html 10/10