Sesion 3 Data Collections
Sesion 3 Data Collections
Cert if icación
Pyt hon
TABLE OF CONTENTS
2. Lists are sequences – they can be iterated, and the order of the elements is
established.
5. The number of elements contained in the list can be determined by the len()
function. For example, the following snippet prints 3 to the screen:
6. Any of the list's elements can be accessed using indexing. List elements
are indexed by integer numbers starting from zero. Therefore, the first list
element's index is 0 while the last element's index is equal to the list length
minus 1. Using indices that are not integers raises the TypeError exception.
For example, the following snippet prints a b c 0 1 2 to the screen: GC
8. An attempt to access a non-existent list element (when the index goes out of
the permissible range) raises the IndexError exception.
9. A slice is a means by which the programmer can create a new list using a
part of the already existing list.
the_list[from:to:step]
and selects those elements whose indices start at from, don't exceed to, and
change with step. For example, the following snippet prints [5, 6] to the screen:
print((1,2,3,4,5,6)[4:6])
1 2 3 4 5 6 Values
0 1 2 3 4 5 6 Index
12. Slices – like indices – can take negative values. For example, the following
snippet prints [1,2] to the screen:
the_list = [0, 1, 2, 3]
0 1 2 3 Values
print(the_list[-3:-1])
-4 -3 -2 -1 0 Index
1. If any of the slice's indices exceeds the allowable range, no exception is raised, and
the non-existent elements are not taken into consideration. Therefore, it is possible that
the resulting slice is an empty list.
2. Assigning a list to a list does not copy elements. Such an assignment results in a
situation when more than one name identifies the same data aggregate. For example, the
following snippet prints True to the screen:
list_a = [1]
list_b = list_a
list_b[0] = 0
print(list_a[0] == list_b[0])
As the slice is a copy of the source list, the following snippet prints False to the screen:
list_a = [1]
list_b = list_a[:]
list_b[0] = 0
print(list_a[0] == list_b[0])
1. Append
2. Insert
3. Del
4. In
5. Not
6. Iterated
7. Comprehension
the_list = [1,2]
the_list.append(3)
print(the_list)
4. The .insert(at_index, element) method can be used to insert the element at the
at_index of the existing list. For example, the following snippet outputs [1,2,3] to the
screen:
the_list = [2,3]
the_list.insert(0, 1)
print(the_list)
5. The del instruction can be used to remove any of the existing list elements. For
example, the following snippet prints [] to the screen:
the_list = [1]
del the_list[0]
print(the_list)
6. The in and not in operators can check whether any value is contained inside the list or
not. For example, the following snippet prints True False to the screen:
the_list = [1,2,3]
for element in the_list:
print(element, end=' ')
8. List comprehension allows the programmer to construct lists in a compact way. For
example, the following snippet prints ["apple", "banana", "mango"] to the screen:
txt = "apple#banana#cherry#orange"
x = txt.split("#")
print(x)
10. The in and not in operators can be applied to strings to check if any string
is a part of another string. An empty string is considered a part of any string,
including an empty one.
Tuples
1. A tuple, like a list, is a data aggregate that contains a certain number
(including zero) of elements of any type. Tuples, like lists, are sequences, but
they are immutable. You're not allowed to change any of the tuple elements,
or add a new element, or remove an existing element. Attempting to break this
rule will raise the TypeError exception.
2. Tuples can be initialized with tuple literals. For example, these assignments
instantiate three tuples – one empty, one one-element, and one two-element:
empty_tuple = () ó tuple()
one_element_tuple = 1,
two_element_tuple = (1, 2.5)
two_element_tuple = 1, 2.5 # the same effect as above
3. The number of elements contained in the tuple can be determined by the len()
function. For example, the following snippet prints 4 to the screen:
Note the inner pair of parentheses – they cannot be omitted, as it will cause
the tuple to be replaced with four independent values and will cause an error.
4. Any of the tuple's elements can be accessed using indexing, which works in the same
manner as in lists, including slicing.
6. If any of the slice's indices exceeds the permissible range, no exception is raised, and
the non-existent elements are not taken into consideration. Therefore, the resulting slice
may be an empty tuple. For example, the following snippet outputs () to the screen:
print((1,2,3)[4:5]) 1 2 3 Values
0 1 2 3 4 5 6 Index
7. The in and not in operators can check whether or not any value is contained inside the
tuple.
8. Tuples can be iterated through (traversed) by the for loop, like lists.
tuple1=(1, 2.5)
tuple2=(3, 4.5)
join = tuple1+tuple2
print(join)
tuple1=(1, 2.5)
multi = tuple1*3
print(multi)
Dict ionaries
1. A dictionary is a data aggregate that gathers pairs of values. The first
element in each pair is called the key, and the second one is called the value.
Both keys and values can be of any type.
2. Dictionaries are mutable but are not sequences – the order of pairs is
imposed by the order in which the keys are entered into the dictionary.
empty_dictionary = {}
phone_directory = {'Emergency': 911, 'Speaking Clock': 767}
• Los diccionarios se declaran con { }
• Los elementos se separan con ,
• Las definiciones de indican con :
4. Accessing a dictionary's value requires the use of its key. For example, the following
line outputs 911 to the screen:
print(phone_directory['Emergency'])
5. An attempt to access an element whose key is absent in the dictionary raises the
KeyError exception.
6. The in and not in operators can be used to check whether a certain key exists in the
dictionary. For example, the following line prints True False to the screen:
7. The len() function returns the number of pairs contained in the directory. For example,
the following line outputs 0 to the screen:
print(len(empty_directory))
8. Changing a value of the existing key is done by an assignment. For example, the
following snippet outputs False to the screen:
9. Adding a new pair to the dictionary resembles a regular assignment. For example, the
following snippet outputs 2 to the screen:
10. Removing a pair from a dictionary is done with the del instruction. For example, the
following snippet outputs 0 to the screen:
currencies = {'USD': 'United States dollar'}
del currencies['USD']
print(len(currencies))
11. When iterated through by the for loop, the dictionary displays only its keys. For
example, the following snippet outputs A B to the screen:
12. The .keys() method returns a list of keys contained in the dictionary. For example, the
following snippet outputs A B to the screen:
14. The .items() method returns a list of two-element tuples, each filled with key:value
pairs. For example, the following snippet outputs ('A', 'Alpha') ('B', 'Bravo') to the screen: