Escolar Documentos
Profissional Documentos
Cultura Documentos
Repetições
•
Repetições
Seria tão legal repetir várias
vezes as mesmas linhas de
código, mas isso é um sonho...
Imprimindo de 1 a 3
• Forma simples
print (1)
print (2)
print (3)
x=1
x <= 3 ?
n =1
soma= O
while n <= 10:
tabuada= tabuada+ 1
O loop FOR
Executa um bloco de código para cada item de
uma seqüência
for <variable> in <sequence>:
• A seqüência pode ser uma seqüência de <indented code block >
caracteres, uma lista, … <non-indented code block>
• O Bloco de código deve ser identado
word = 'top'
Introduction to Computing Using Python
>>>
2
0
1
>>>
3
6
4
12
2
4
10
8
>>>
3
5
14
12
>>>
Introduction to Computing Using Python
Exercício
a) 0, 1, 2, 3, 4, 5, 6, 7, 8 , 9, 10
b) 1, 2, 3, 4, 5, 6, 7, 8, 9
c) 0, 2, 4, 6, 8
d) 1, 3, 5, 7, 9
>>> hyp(3,4)
5.0
>>>
import math
def hyp(a, b):
res = math.sqrt(a**2 + b**2)
return res
Introduction to Computing Using Python
Exercício
>>> hello('Julie')
Welcome, Julie, to the world of Python.
>>>
def hello(name):
line = 'Welcome, ' + name + ', to the world of Python.'
Python.’
print(line)
Introduction to Computing Using Python
Exercício
def rng(lst):
res = max(lst) - min(lst)
return res
Introduction to Computing Using Python
Comentários e docstrings
Comentários f(x)
f(x)
>>> help(f)
Docstring Help on function f in module __main__:
olhar
Uma variável não existe antes de ser atribuída
a b c d
3 [1, 2, 3]
'three' >>> a
3.3 Traceback (most recent call last):
File "<pyshell#66>", line 1, in
<module>
a
NameError: name 'a' is not defined
>>> a = 3
<variable> = <expression>
>>> b = 2 + 1.3
>>> c = 'three'
1. <expression> é avaliada e seu valor >>> d = [1, 2] + [3]
a b c d
>>> a
3
3 [1, 7,
2, 3] >>> a = 6
'three' >>> a
3.3 6
6 >>> d
[1, 2, 3]
>>> d[1] = 7
O objeto (3) referido pela variável a não muda; >>> d
em vez disso, a refere-se a um novo objeto (6) [1, 7, 3]
Os inteiros são imutáveis
Atribuição e mutabilidade
a b c d >>>
>>> aa
6
>>>
>>> bb
9 3.3
>>> b = a
>>> b
6
3 [1, 7, 9]
3] >>> a = 9
'three' >>> b
3.3 6
>>> c = d
6 >>> c
[1, 7, 3]
>>> d[2] = 9
ac e bd referem-se ao mesmo objeto inteiro
de lista >>> c
[1, 7, 9]
aAagora se refere
lista que a umanovo
c se refere objeto d
alterações; (9); b aindaao
refere-se se mesmo
refere ao objeto
objeto deantigo (6)modo
lista, de
Como
que eleos muda
inteiros são imutáveis, uma alteração em um não afeta o valor de b
Como as listas são mutáveis, uma alteração para d afeta c
Introduction to Computing Using Python
Trocando valores
a b tmp queremos: a b
Temos:
3 6 3 6
>>> a
3
>>> b
6
>>> tmp
a,
b =b a== b,
ba
>>> b = a
>>> a = tmp
Introduction to Computing Using Python
a x
3 5
>>> a = 3
>>> g(a)
def g(x):
>>>
x=5
lst l
[1,2,3]
[5,2,3]
>>> lst = [1,2,3]
>>> h(lst)
def h(l):
>>>
l[0] = 5
AVariável l dentro
função h h () refere-se
() modificou o valor ao
de objeto lst refere-se
lst no shell a
interativo.
Isto é porque lst e l referem-se a um objeto mutável.
Como se executamos l = lst
Introduction to Computing Using Python
Exercício
def swapFS(lst):
if len(lst) > 1:
lst[0], lst[1] = lst[1], lst[0]
“A vida é como
andar de bicicleta.
Para manter o
equilíbrio, é preciso
se manter em
movimento”.
Einstein.
,
fi> Copvrlght California Institute of Technolo91J. All rights reserved.
Commercial use or modification of this material ts prohibited.