COM102 Presentation 02
COM102 Presentation 02
Programación
Orientada a Objetos
Presentation 01 – Python, Installation, and General Concepts
Python
Functions
Functions
def my_function():
#code
Ingeniería / COM102 3
Functions
my_function()
Functions
my_function("Emil")
my_function("Tobias")
my_function("Linus")
Ingeniería / COM102 5
Functions
• Parameter:
• def my_function(fname):
print("Tu nombre es: " + fname)
• Argument:
• my_function("Emil")
Ingeniería / COM102 6
Python
Collections
Collections
Ingeniería / COM102 8
Lists
The items in the list are sorted, their value can be changed, and duplicate values
are allowed.
• The items in the list are indexed, the first item has index [0], the second item
has index [1], and so on.
Ingeniería / COM102 9
Lists
• To determine how many items a list has, use the len() function:
aList = [”apple", "banana", ”cherry"]
print(len(aList))
• The items in the list are indexed and can be accessed by reference to the index
number:
anotherList = [”red", ”blue", ”green"]
print(anotherList[1])
Ingeniería / COM102 10
Lists
To add an item to the end of the list, use the append() method:
fruits = [”apple", "banana", ”cherry"]
fruits.append(”orange")
print(fruits)
To insert a new list item, without replacing any of the existing values, we can use the
insert() method.
The insert() method inserts an element at the specified index:
• market = [”apple", "banana", ”cherry"]
market.insert(1, ”orange")
print(supermercado)
• To append/extend items from another list to the current list, use the extend() method.
• americana = [”apple", "banana", ”cherry"]
tropical = ["mango", "coco", "papaya"]
americana.extend(tropical)
print(thislist)
Ingeniería / COM102 11
Lists
Ingeniería / COM102 12
Lists
Ingeniería / COM102 13
Lists
Loops in lists
•
• listaFor = ["apple", "banana", "cherry"]
for i in range(len(listaFor)):
print(listaFor[i])
Ingeniería / COM102 14