0% found this document useful (0 votes)
3 views10 pages

Apontamentos

The document provides an overview of basic Python programming concepts, including strings, numbers, booleans, and lists. It covers how to store and manipulate data, perform calculations, and create functions, along with examples of user input and list operations. Additionally, it introduces tuples and their immutability, emphasizing the importance of functions in organizing code.

Uploaded by

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

Apontamentos

The document provides an overview of basic Python programming concepts, including strings, numbers, booleans, and lists. It covers how to store and manipulate data, perform calculations, and create functions, along with examples of user input and list operations. Additionally, it introduces tuples and their immutability, emphasizing the importance of functions in organizing code.

Uploaded by

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

PYTHON

String = plain text. To store a string, we need quotation marks.


name="Tom"

Numbers: To store a number we don’t need to have quotation marks


age=50
age=50.458

Boolean values = True or False values.

1.1 Working with strings


print("Giraffe Academy")
create a new line
print("Giraffe\nAcademy")
Giraffe
Academy
print("Giraffe\"Academy")
Giraffe"Academy
1.2 Storing strings into variables
phrase="Giraffe Academy"
print(phrase)
Giraffe Academy
Concatenation
phrase="Giraffe Academy"
print(phrase +" is cool")
Giraffe Academy is cool

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 *

floor function: will remove the decimal point


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

Building a basic calculator


num1 = input("Enter a number: ")
num2 = input("Enter another number: ")
result = num1 * num2
print(result)

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.

int() = integer = the user can only insert whole numbers


num1 = input("Enter a number: ")
num2 = input("Enter another number: ")
result = int(num1)*int(num2)
print(result)
num1 = input("Enter a number: ")
num2 = input("Enter another number: ")
result = int(num1)*int(num2)
print(result)

float() = floating = the user can insert decimal or whole numbers


num1 = input("Enter a number: ")
num2 = input("Enter another number: ")
result = float(num1)*float(num2)
print(result)

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

Let’s ask the user to insert their own words e.g.:


print("Roses are {color}")
print("{plural noun} are blue")
print("I love {celebrity}")

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

Modify values inside a list


fruit=["apples","bananas","oranges","pineapples","strawberry"]
fruit[2]="blueberries"

print(fruit[1:3])
['bananas', 'blueberries']

List with functions


Extend function: will allow us to take a list and append another list onto the end of
it.
fruit=["apples","bananas","oranges","pineapples","strawberry"]
luckyNumbers=[7,9,10,17,27]
fruit.extend(luckyNumbers)

print(fruit)
['apples', 'bananas', 'oranges', 'pineapples', 'strawberry', 7, 9, 10, 17, 27]

Append: will allow us to add another element in the end of a list


fruit=["apples","bananas","oranges","pineapples","strawberry"]
luckyNumbers=[7,9,10,17,27]
fruit.append("blueberries")

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

count: how many times a value will show up inside a list


fruit=["apples","bananas","oranges","pineapples","oranges","strawberry"]
luckyNumbers=[7,9,10,17,27]

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy