0% found this document useful (0 votes)
10 views14 pages

COM102 Presentation 02

Uploaded by

Leonardo Peña
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)
10 views14 pages

COM102 Presentation 02

Uploaded by

Leonardo Peña
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/ 14

COM102

Programación
Orientada a Objetos
Presentation 01 – Python, Installation, and General Concepts
Python

Functions
Functions

A function is a block of code that is executed only when it is called.


• Functions are used to perform certain actions, and they are important for
reusing code: define the code once and use it many times.

def my_function():
#code

my_function() it’s the name/identifier of the function


• Inside the function (the body), one adds the code that the function should do
• Inside the parentesys, one adds the parameters needed for the function to work

Ingeniería / COM102 3
Functions

Declared functions do not run immediately.


They are "saved for later use", and will be executed later, when
called.
To call a function, type the name of the function followed by
two parentheses ()

• def my_function():
print("Hello from a function")

my_function()
Functions

Information can be passed to functions as arguments/paremeters.


Arguments are specified after the function name, inside the parentheses. You can
add as many arguments as you want, just separate them with a comma.
The following example shows a function with an argument (fname). When the
function is called, we pass a name, which is used within the function to print the
full name:
• def my_function(fname):
print("Tu nombre es: " + fname)

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")

From a function perspective:


A parameter is the variable that appears in parentheses in the function definition.
• An argument is the value that is sent to the function when it is called.

Ingeniería / COM102 6
Python

Collections
Collections

They are used to store multiple items in a single variable.


There are four types of collection data in the Python programming language:
The list
The tuple
The set
• The dictionary

Ingeniería / COM102 8
Lists

• Lists are created in square brackets:

• myList = [”apple", "banana", ”cherry"]


print(myList)

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])

• To change the value of a specific item, the index number is used:


anotherList[1] = "cyan"

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

To remove an item from a list we can use:


The remove() method removes the specified item.
• list1 = [”apple", "banana", ”cherry"]
list1.remove("banana")
print(list1)
• The pop() method removes the specified index.
• list2 = [”apple", "banana", ”cherry"]
list2.pop(1)
print(list2)
• If you do not specify the index, the pop() method removes the last item.
• list3 = [”apple", "banana", ”cherry"]
list3.pop()
print(list3)

Ingeniería / COM102 12
Lists

To remove an item from a list we can use:


The keyword also deletes the specified index.
list4 = [”apple", "banana", ”cherry"]
del list4[0]
print(list4)

The keyword can also delete the list completely.


• lista5 = [”apple", "banana", ”cherry"]
del lista5
• The clear() method empties the list.
• thisList = [”apple", "banana", ”cherry"]
thisList.clear()
print(thisList)

Ingeniería / COM102 13
Lists

Loops in lists

• listaFor = ["apple", "banana", "cherry"]
for i in range(len(listaFor)):
print(listaFor[i])

• listaWhile = ["apple", "banana", "cherry"]


i = 0
while i < len(listaWhile)):
print(listaWhile[i])
i = i + 1

Ingeniería / COM102 14

You might also like