0% found this document useful (0 votes)
6 views

Sesion 3 Data Collections

The document provides an overview of key Python concepts including lists, tuples, dictionaries, strings, and functions. It describes how to initialize, access, modify, and iterate over each of these data types. Control flow statements like conditionals and loops are also mentioned.

Uploaded by

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

Sesion 3 Data Collections

The document provides an overview of key Python concepts including lists, tuples, dictionaries, strings, and functions. It describes how to initialize, access, modify, and iterate over each of these data types. Control flow statements like conditionals and loops are also mentioned.

Uploaded by

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

PITCH DECK

Cert if icación
Pyt hon
TABLE OF CONTENTS

01. 02. 03. 04.

Comput er Dat a Collect ions –


Cont rol Flow – Funct ions and
Programming and Tuples,
Condit ional Blocks Except ions
Pyt hon Dict ionaries, List s,
and Loops
Fundament als and St rings
List s
1. A list is a data aggregate that contains a certain number (including zero) of
elements of any type.

2. Lists are sequences – they can be iterated, and the order of the elements is
established.

3. Lists are mutable – their contents may be changed.


4. Lists can be initialized with list literals. For example, these two assignments
instantiate two lists – the former is empty, while the latter contains three
elements:

empty_list = [] • Las listas se declaran con [ ]


• Los elementos se separan con , (comas)
three_elements = [1, 'two', False]

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:

print(len(['a', 'b', 'c'])

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.

10. The most general slice looks as follows:

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

8. Strings like list


3. The .append(element) method can be used to append an element to the end of an
existing list. For example, the following snippet outputs [1,2,3] to the screen:

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, 'a']


print('a' in the_list, 1 not in the_list)
7. Lists can be iterated through (traversed) by the for loop, which allows the programmer
to scan all their elements without the use of explicit indexing. For example, the following
snippet prints 1 2 3 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:

fruits = ["apple", "banana", "cherry", "kiwi", "mango"]


newlist = [x for x in fruits if "a" in x]
print(newlist)
St rings like list
9. Strings, like lists, are sequences, and in many contexts they behave like
lists, especially when they are indexed and sliced or are arguments of the len()
function.

txt = "welcome to the jungle"


x = txt.split()
print(x)

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:

print(len((1, 2.2, '3', True)))

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.

5. An attempt to access a non-existent tuple element raises the IndexError exception.

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.

9. The + operator joins tuples together.

tuple1=(1, 2.5)
tuple2=(3, 4.5)
join = tuple1+tuple2
print(join)

10. The * operator multiplies tuples, just like lists.

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.

3. Dictionaries can be initialized with dictionary literals. For example, these


assignments instantiate two dictionaries – one empty and one containing two
key:value pairs:

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:

print('Emergency' in phone_directory, 'White House' in phone_directory)

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:

attendance = {'Bob': True}


attendance['Bob'] = False
print(attendance['Bob'])

9. Adding a new pair to the dictionary resembles a regular assignment. For example, the
following snippet outputs 2 to the screen:

domains = {'au': 'Australia'}


domains['at'] = 'Austria'
print(len(domains))

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:

phonetic = {'A': 'Alpha', 'B': 'Bravo'}


for key in phonetic:
print(key, end=' ')

12. The .keys() method returns a list of keys contained in the dictionary. For example, the
following snippet outputs A B to the screen:

phonetic = {'A': 'Alpha', 'B': 'Bravo'}


for key in phonetic.keys():
print(key, end=' ')
13. The .values() method returns a list of values contained in the dictionary. For example,
the following snippet outputs Alpha Bravo to the screen:

phonetic = {'A': 'Alpha', 'B': 'Bravo'}


for value in phonetic.values():
print(value, end=' ')

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:

phonetic = {'A': 'Alpha', 'B': 'Bravo'}


for item in phonetic.items():
print(item, end=' ')
THANK YOU!

You might also like