Apontamentos
Apontamentos
String functions
phrase="Giraffe Academy"
print(phrase.lower())
giraffe academy
phrase="Giraffe Academy"
print(phrase.upper())
GIRAFFE ACADEMY
phrase="Giraffe Academy"
print(phrase.isupper())
False
phrase="Giraffe Academy"
print(phrase.upper().isupper())
True
Length function
phrase="Giraffe Academy"
print(len(phrase))
15
phrase="Giraffe Academy"
print(phrase[0])
G
phrase="Giraffe Academy"
#0123
print(phrase[0])
phrase="Giraffe Academy"
#0123
print(phrase[3])
a
Index function
phrase="Giraffe Academy"
print(phrase.index("G"))
0
phrase="Giraffe Academy"
print(phrase.index("Acad"))
8
phrase="Giraffe Academy"
print(phrase.index("z"))
Replace function
phrase="Giraffe Academy"
print(phrase.replace("Giraffe","Elephant"))
Elephant Academy
1.2 Working with numbers
print(3+4.5)
7.5
print(3*4+5)
17
Number functions
Absolute value=abs()
myNum=-5
print(abs(myNum))
5
Power function - E.g. 3^2
print(pow(3,2))
9
print(max(4,6))
6
print(min(4,6))
4
print(round(3.4))
3
print(round(3.5))
4
Import external code in order to have access to other math functions
from math import *
myNum=-5
print(floor(3.2))
3
Ceil function: will round the number up
from math import *
myNum=-5
print(ceil(3.2))
4
Square rout (raiz quadrada
from math import *
myNum=-5
print(sqrt(36))
6
Getting input from users
name=input("Enter your name: ")
print("Hello",name,"!")
Enter your name: Fabio
Hello Fabio !
name=input("Enter your name: ")
print("Hello"+name+"!")
Enter your name: Fabio
HelloFabio!
name=input("Enter your name: ")
print("Hello "+name+" !")
Enter your name: Fabio
Hello Fabio !
name=input("Enter your name: ")
age=input("Enter your age: ")
print("Hello",name,"!","You are",age)
Enter your name: Fabio
Enter your age: 35
Hello Fabio ! You are 35
NOTA: Como não está definido, o python considera por defeito que qualquer valor
introduzido é uma string (texto).
Para podermos introduzir números os código tem que ter isso definido. Ou seja,
temos que converter as strings que obtemos do input do user para números.
Mad libs game: the user needs to insert a random word in a text.
print("Roses are red")
print("Violets are blue")
print("I love you")
Code in python:
color=input("Enter a color: ")
pluralNoun=input("Enter a plural noun: ")
celebrity=input("Enter a celebrity name: ")
print("Roses are",color)
print(pluralNoun,"are blue")
print("I love",celebrity)
Working with lists: when we are dealing with large amount of data
[] – when we want to store a bunch of values inside a list
e.g.
fruit=["apples","bananas","oranges","pineapples"]
To refer to an element inside a list we need to use the index value e.g.
fruit=["apples","bananas","oranges","pineapples"]
0 1 2 3
print(fruit)
fruit=["apples","bananas","oranges","pineapples"]
print(fruit[1])
bananas
We can also use negative index numbers to access elements in the back of the list
fruit=["apples","bananas","oranges","pineapples"]
print(fruit[-3])
bananas
fruit=["apples","bananas","oranges","pineapples"]
print(fruit[-1])
pineapples
with colon after the index
fruit=["apples","bananas","oranges","pineapples","strawberry"]
print(fruit[1:])
['bananas', 'oranges', 'pineapples', 'strawberry']
fruit=["apples","bananas","oranges","pineapples","strawberry"]
print(fruit[1:3])
['bananas', 'oranges']
print(fruit[1:3])
['bananas', 'blueberries']
print(fruit)
['apples', 'bananas', 'oranges', 'pineapples', 'strawberry', 7, 9, 10, 17, 27]
print(fruit)
['apples', 'bananas', 'oranges', 'pineapples', 'strawberry', 'blueberries']
Insert: will allow us to add an element after a given index
fruit=["apples","bananas","oranges","pineapples","strawberry"]
luckyNumbers=[7,9,10,17,27]
fruit.insert(1,"mango")
print(fruit)
['apples', 'mango', 'bananas', 'oranges', 'pineapples', 'strawberry']
Remove: to remove elements
fruit=["apples","bananas","oranges","pineapples","strawberry"]
luckyNumbers=[7,9,10,17,27]
fruit.remove("oranges")
print(fruit)
['apples', 'bananas', 'pineapples', 'strawberry']
Clear: to remove all the elements inside a list
fruit=["apples","bananas","oranges","pineapples","strawberry"]
luckyNumbers=[7,9,10,17,27]
fruit.clear()
print(fruit)
[]
Pop: removes the last element of a list (pops an element out of the list)
fruit=["apples","bananas","oranges","pineapples","strawberry"]
luckyNumbers=[7,9,10,17,27]
fruit.pop()
print(fruit)
['apples', 'bananas', 'oranges', 'pineapples']
Index: will return the index number in a list
fruit=["apples","bananas","oranges","pineapples","strawberry"]
luckyNumbers=[7,9,10,17,27]
print(fruit.index("oranges"))
print(luckyNumbers.index(17))
2
3
If a name is not will the list will return an error
fruit=["apples","bananas","oranges","pineapples","strawberry"]
luckyNumbers=[7,9,10,17,27]
print(fruit.index("pen"))
print(luckyNumbers.index(17))
print(fruit.count("oranges"))
2
sort: will place a list in alphabetical order or numbers in ascending order
fruit=["apples","bananas","oranges","pineapples","oranges","strawberry"]
luckyNumbers=[7,9,10,17,27]
fruit.sort()
print(fruit)
['apples', 'bananas', 'oranges', 'oranges', 'pineapples', 'strawberry']
Will not show if .sort is included in the print
fruit=["apples","bananas","oranges","pineapples","oranges","strawberry"]
luckyNumbers=[7,9,10,17,27]
print(fruit.sort())
None
fruit=["apples","bananas","oranges","pineapples","oranges","strawberry"]
luckyNumbers=[7,9,10,17,27]
fruit.sort()
luckyNumbers.sort()
print(fruit)
print(luckyNumbers)
['apples', 'bananas', 'oranges', 'oranges', 'pineapples', 'strawberry']
[7, 9, 10, 17, 27]
reverse: will reverse the order of a list
fruit=["apples","bananas","oranges","pineapples","oranges","strawberry"]
luckyNumbers=[7,9,10,90,17,27]
fruit.reverse()
luckyNumbers.sort()
print(fruit)
print(luckyNumbers)
['strawberry', 'oranges', 'pineapples', 'oranges', 'bananas', 'apples']
[7, 9, 10, 17, 27, 90]
copy: create another list as a copy of another list
fruit=["apples","bananas","oranges","pineapples","oranges","strawberry"]
luckyNumbers=[7,9,10,90,17,27]
fruit2=fruit.copy()
print(fruit2)
['apples', 'bananas', 'oranges', 'pineapples', 'oranges', 'strawberry']
Tuples
A tuple is a type of data structure which means that is a container where we can
store different values. Is very similar to a list and we can store multiple pieces of
data.
A tuple is immutable which means that the tuple can’t be changed or modified.
Once we create our tuple we cannot modify it. The application for a tuple is an
amount of data that never gone change.
how to create a tuple
coordinates=(4,5)
print(coordinates[0])
4
list of tuples
coordinates=[(4,5),(6,7),(80,90)]
print(coordinates[2])
(80, 90)
Functions
A function is a collection of code that performs a specific task. Helps to organize the
code.
To write a function the first thing is to write the keyword def. When Python sees this
keyword he knows that the programmer wants to use a function.
NOTE: the code inside a function will not be executed by default.
ef sayHi(): #all the code that comes after this line will be inside of our
function
print("hello user") #all the code inside the function needs to be idented
related to the 1st line of code
Process finished with exit code 0
def sayHi():
print("hello user")
sayHi() #we need to call the function
hello user
def sayHi():
print("hello user")
print("Top")
sayHi() #we need to call the function
print("Bottom")
Top
hello user
Bottom