Upload
truongkien
View
216
Download
0
Embed Size (px)
Citation preview
Aula 2 Prática: Tarefa 7
Usando o módulo Turtle, desenhe cada um dos triângulos cujos vértices estão especificados abaixo:
Triângulo 1: (-217, 35), (-79, 107), (-83, 29)
Triângulo 2: (-120, -65), (-46, 67), (96, 69)
Triângulo 3: (-68, -67), (-39, -139), (119, -59)
Triângulo 4: (48, -24), (133, 92), (250, -50)
Dica: turtle.goto(x, y) move a tartaruga para a posição (x, y) da tela, onde (0,0) corresponde ao centro da tela.
Aula 2 Prática: Tarefa 7
import turtle
def triangulo(x0, y0, x1, y1, x2, y2):'''Desenha um triangulo dados os vertices(x0, y0), (x1, y1), (x2, y2).'''turtle.up()turtle.goto(x0, y0)turtle.down()turtle.goto(x1, y1)turtle.goto(x2, y2)turtle.goto(x0, y0)
triangulo(-217, 35, -79, 107, -83, 29)triangulo(-120, -65, -46, 67, 96, 69)triangulo(-68, -67, -39, -139, 119, -59)triangulo(48, -24, 133, 92, 150, -50)
turtle.done()
Tarefa 8
Para cada triângulo da tarefa anterior, escreva no centro de cada aresta o comprimento desta (arredondando para duas casas decimais). Por exemplo, para o Triângulo 1: (-217, 35), (-79, 107), (-83, 29), teremos
Dica: turtle.write(x) escreve o valor de x na posição atual da tartaruga.
Aula 2 Prática: Tarefa 7
def aresta(x0, y0, x1, y1):''' Escreve no centro de uma aresta o seu comprimento.'''
x = .5*(x0 + x1)y = .5*(y0 + y1)turtle.up()turtle.goto(x, y)turtle.down()d = ((x0-x1)**2 + (y0-y1)**2)**0.5turtle.write(round(d, 2))
Aula 2 Prática: Tarefa 7
def triangulo(x0, y0, x1, y1, x2, y2):'''Desenha um triangulo dados os vertices(x0, y0), (x1, y1), (x2, y2).'''turtle.up()turtle.goto(x0, y0)turtle.down()turtle.goto(x1, y1)turtle.goto(x2, y2)turtle.goto(x0, y0)
aresta(x0, y0, x1, y1)aresta(x1, y1, x2, y2)aresta(x2, y2, x0, y0)
Tuplas - sequências de valores
>>> (2, 4)
(2, 4)
>>> ('pedra', 'papel', 'tesoura')
('pedra', 'papel', 'tesoura')
>>> (2.3, 'aranha', 10)
(2.3, 'aranha', 10)
>>> ('pao', 'siri', 'ketchup', 'pao')
('pao', 'siri', 'ketchup', 'pao')
Tupla unitária
>>> (3,)
(3,)
>>> (3)
3
>>> (1000.,)
(1000.0,)
>>> ('forever alone',)
('forever alone',)
Tupla - omitir parênteses
Se não causar ambiguidade, podemos omitir os parênteses.>>> 3, 4, 5(3, 4, 5)>>> 'Bob', 'Patrick', 'Sandy'('Bob', 'Patrick', 'Sandy')>>> 1, 3.1, 'samambaia', 'umbigo'(1, 3.1, 'samambaia', 'umbigo')>>> p = 3.1, 4.2>>> q = 1.5, 8.3>>> p(3.1, 4.2)>>> q(1.5, 8.3)
Concatenação
>>> (3, 1) + (4, 2)
(3, 1, 4, 2)
>>> p = 'bola', 'tatu'
>>> q = 42, 'chilique'
>>> p + q
('bola', 'tatu', 42, 'chilique')
Multiplicação por inteiro
>>> 4*(1, 2)(1, 2, 1, 2, 1, 2, 1, 2)>>> a = 'Homer', 'Marge'>>> 3*a('Homer', 'Marge', 'Homer', 'Marge', 'Homer', 'Marge')>>> b = 100>>> 5*b500>>> b = 100,>>> 5*b(100, 100, 100, 100, 100)
Atribuição múltipla
>>> p = 'pipoca', 'refrigerante'>>> a, b = p>>> a'pipoca'>>> b'refrigerante'>>> q = 200, 351, 8>>> x, y, z = q>>> x200>>> y351>>> z8
Indexação
>>> a = 130, 422, 99>>> a[0]130>>> a[1]422>>> a[2]99>>> s = 'Homer', 'Marge', 'Lisa', 'Bart', 'Maggie'>>> s[0]'Homer'>>> s[3]'Bart'>>> s[5]IndexError: tuple index out of range
Aula 2 Prática: Tarefa 7
import turtle
def triangulo(x0, y0, x1, y1, x2, y2):'''Desenha um triangulo dados os vertices(x0, y0), (x1, y1), (x2, y2).'''turtle.up()turtle.goto(x0, y0)turtle.down()turtle.goto(x1, y1)turtle.goto(x2, y2)turtle.goto(x0, y0)
triangulo(-217, 35, -79, 107, -83, 29)triangulo(-120, -65, -46, 67, 96, 69)triangulo(-68, -67, -39, -139, 119, -59)triangulo(48, -24, 133, 92, 150, -50)
turtle.done()
Aula 2 Prática: Tarefa 7
import turtle
def triangulo(p, q, r):'''Desenha um triangulo dados os verticesp, q, r.'''turtle.up()turtle.goto(p[0], p[1])turtle.down()turtle.goto(q[0], q[1])turtle.goto(r[0], r[1])turtle.goto(p[0], p[1])
triangulo((-217, 35), (-79, 107), (-83, 29))triangulo((-120, -65), (-46, 67), (96, 69))triangulo((-68, -67), (-39, -139), (119, -59))triangulo((48, -24), (133, 92), (150, -50))
turtle.done()
Aula 2 Prática: Tarefa 7
import turtle
def triangulo(p, q, r):'''Desenha um triangulo dados os verticesp, q, r.'''turtle.up()turtle.goto(p)turtle.down()turtle.goto(q)turtle.goto(r)turtle.goto(p)
triangulo((-217, 35), (-79, 107), (-83, 29))triangulo((-120, -65), (-46, 67), (96, 69))triangulo((-68, -67), (-39, -139), (119, -59))triangulo((48, -24), (133, 92), (150, -50))
turtle.done()
Aula 2 Prática: Tarefa 7
...
P = ((-217, 35), (-79, 107), (-83, 29), (-120, -65), (-46, 67), (96, 69),(-68, -67), (-39, -139), (119, -59),(48, -24), (133, 92), (150, -50))
triangulo(P[0], P[1], P[2])triangulo(P[3], P[4], P[5])triangulo(P[6], P[7], P[8])triangulo(P[9], P[10], P[11])triangulo(P[0], P[4], P[10])
turtle.done()
Indexação com valores negativos
>>> a = 130, 422, 99>>> a[-1]99>>> a[-2]422>>> a[-3]130>>> a[-4]IndexError: tuple index out of range>>> s = 'Homer', 'Marge', 'Lisa', 'Bart', 'Maggie'>>> s[-1]'Maggie'>>> s[-5]'Homer'>>> s[-7]IndexError: tuple index out of range
Fatiamento
>>> t = 'a', 'hue', 9, 2, 'chaves', 'chiquinha', 'quico'>>> t[2:5](9, 2, 'chaves')>>> t[4:6]('chaves', 'chiquinha')>>> t[4:7]('chaves', 'chiquinha', 'quico')>>> t[4:8]('chaves', 'chiquinha', 'quico')>>> t[4:8000]('chaves', 'chiquinha', 'quico')
Fatiamento
>>> t = 'a', 'hue', 9, 2, 'chaves', 'chiquinha', 'quico'>>> t[:5]('a', 'hue', 9, 2, 'chaves')>>> t[3:](2, 'chaves', 'chiquinha', 'quico')>>> t[:]('a', 'hue', 9, 2, 'chaves', 'chiquinha', 'quico')>>> t[-4:-1](2, 'chaves', 'chiquinha')>>> t[3:1]()>>> t[-5:5](9, 2, 'chaves')
Fatiamento estendido
>>> t = 'a', 'hue', 9, 2, 'chaves', 'chiquinha', 'quico'>>> t[1:6]('hue', 9, 2, 'chaves', 'chiquinha')>>> t[1:6:2]('hue', 2, 'chiquinha')>>> t[2::3](9, 'chiquinha')>>> t[-6:-2:4]('hue',)>>> t[:-2:4]('a', 'chaves')
Número de elementos
>>> a = 'Goku', 'Gohan', 'Goten' 'Vegeta', 'Piccolo', 'Trunks'
>>> len(a)
6
>>> b = 'pao', 'siri', 'ketchup', 'pao'
>>> len(b)
4
Contagem
>>> a = 4, 9, 1, 4, 2, 4, 9, 8, 1, 3, 5, 2, 8, 4, 3, 5, 4, 2, 1>>> a.count(4)5>>> a.count(1)3>>> a.count(10)0>>> b = 'pao', 'siri', 'ketchup', 'pao'>>> b.count('pao')2>>> b.count('batata')0
Índice de um elemento
>>> x = 'Homer', 'Marge', 'Maggie', 'Bart', 'lisa'
>>> x.index('Homer')
0
>>> x.index('Bart')
3
>>> x.index('Lisa')
ValueError: tuple.index(x): x not in tuple
Tuplas são imutáveis
>>> a = 3, 1, 2
>>> a[0] = 10
TypeError: 'tuple' object does not support item assignment
Listas
>>> [2, 4]
[2, 4]
>>> ['pedra', 'papel', 'tesoura']
['pedra', 'papel', 'tesoura']
>>> [2.3, 'aranha', 10]
[2.3, 'aranha', 10]
>>> ['pao', 'siri', 'ketchup', 'pao']
['pao', 'siri', 'ketchup', 'pao']
Lista unitária
>>> [3,]
[3]
>>> [3]
[3]
>>> [1000.]
[1000.0]
>>> ['forever alone']
['forever alone']
Concatenação de listas
>>> [3, 1] + [4, 2]
[3, 1, 4, 2]
>>> p = ['bola', 'tatu']
>>> q = [42, 'chilique']
>>> p + q
['bola', 'tatu', 42, 'chilique']
Multiplicação por inteiro
>>> 4*[1, 2]
[1, 2, 1, 2, 1, 2, 1, 2]
>>> a = ['Homer', 'Marge']
>>> 3*a
['Homer', 'Marge', 'Homer', 'Marge', 'Homer', 'Marge']
>>> b = [100]
>>> 5*b
[100, 100, 100, 100, 100]
Atribuição múltipla
>>> p = ['pipoca', 'refrigerante']>>> a, b = p>>> a'pipoca'>>> b'refrigerante'>>> q = [200, 351, 8]>>> x, y, z = q>>> x200>>> y351>>> z8
Indexação
>>> a = [130, 422, 99]>>> a[0]130>>> a[1]422>>> a[2]99>>> s = ['Homer', 'Marge', 'Lisa', 'Bart', 'Maggie']>>> s[0]'Homer'>>> s[3]'Bart'>>> s[5]IndexError: list index out of range
Indexação com valores negativos
>>> a = [130, 422, 99]>>> a[-1]99>>> a[-2]422>>> a[-3]130>>> a[-4]IndexError: list index out of range>>> s = ['Homer', 'Marge', 'Lisa', 'Bart', 'Maggie']>>> s[-1]'Maggie'>>> s[-5]'Homer'>>> s[-7]IndexError: list index out of range
Fatiamento
>>> t = ['a', 'hue', 9, 2, 'chaves', 'chiquinha', 'quico']>>> t[2:5][9, 2, 'chaves']>>> t[4:6]['chaves', 'chiquinha']>>> t[4:7]['chaves', 'chiquinha', 'quico']>>> t[4:8]['chaves', 'chiquinha', 'quico']>>> t[4:8000]['chaves', 'chiquinha', 'quico']
Fatiamento
>>> t = ['a', 'hue', 9, 2, 'chaves', 'chiquinha', 'quico']>>> t[:5]['a', 'hue', 9, 2, 'chaves']>>> t[3:][2, 'chaves', 'chiquinha', 'quico']>>> t[:]['a', 'hue', 9, 2, 'chaves', 'chiquinha', 'quico']>>> t[-4:-1][2, 'chaves', 'chiquinha']>>> t[3:1][]>>> t[-5:5][9, 2, 'chaves']
Fatiamento estendido
>>> t = ['a', 'hue', 9, 2, 'chaves', 'chiquinha', 'quico']>>> t[1:6]['hue', 9, 2, 'chaves', 'chiquinha']>>> t[1:6:2]['hue', 2, 'chiquinha']>>> t[2::3][9, 'chiquinha']>>> t[-6:-2:4]['hue']>>> t[:-2:4]['a', 'chaves']
Número de elementos
>>> a = ['Goku', 'Gohan', 'Goten', 'Vegeta', 'Piccolo', 'Trunks']
>>> len(a)
5
>>> b = ['pao', 'siri', 'ketchup', 'pao']
>>> len(b)
4
Contagem
>>> a = [4, 9, 1, 4, 2, 4, 9, 8, 1, 3, 5, 2, 8, 4, 3, 5, 4, 2, 1]>>> a.count(4)5>>> a.count(1)3>>> a.count(10)0>>> b = ['pao', 'siri', 'ketchup', 'pao']>>> b.count('pao')2>>> b.count('batata')0
Índice de um elemento
>>> x = ['Homer', 'Marge', 'Maggie', 'Bart', 'lisa']
>>> x.index('Homer')
0
>>> x.index('Bart')
3
>>> x.index('Lisa')
ValueError: 'Lisa' is not in list
Alterando valores numa lista
>>> x = ['Chaves', 'Chiquinha', 'Nhonho', 'Seu Barriga']
>>> x[3]
'Seu Barriga'
>>> x[3] = 'Quico'
>>> x
['Chaves', 'Chiquinha', 'Nhonho', 'Quico']
>>> x[2] = 'Popis'
>>> x
['Chaves', 'Chiquinha', 'Popis', 'Quico']
Adicionando valores numa lista
>>> x = ['Chaves', 'Chiquinha', 'Nhonho', 'Seu Barriga']>>> x.append('Quico')>>> x['Chaves', 'Chiquinha', 'Nhonho', 'Seu Barriga', 'Quico']>>> x.insert(2, 'Seu Madruga')>>> x['Chaves', 'Chiquinha', 'Seu Madruga', 'Nhonho', 'Seu Barriga', 'Quico']>>> x.insert(-2, 'Jaiminho')>>> x['Chaves', 'Chiquinha', 'Seu Madruga', 'Nhonho', 'Jaiminho', 'Seu Barriga', 'Quico']
Adicionando valores numa lista
>>> a = ['Homer', 'Marge']>>> b = ['Bart', 'Lisa', 'Maggie']>>> a.extend(b)>>> a['Homer', 'Marge', 'Bart', 'Lisa', 'Maggie']>>> x = [2, 4]>>> y = [1, 9, 10]>>> x.append(y)>>> x[2, 4, [1, 9, 10]]
Removendo valores de uma lista
>>> stark = ['Ned', 'Robb', 'Arya', 'Bran', 'Rickon', 'Jon Snow']>>> stark.remove('Ned')>>> stark['Robb', 'Arya', 'Bran', 'Rickon', 'Jon Snow']>>> del stark[0]>>> stark['Arya', 'Bran', 'Rickon', 'Jon Snow']>>> a = [10, 94, 17, 39, 13, 23]>>> del a[1:4]>>> a[10, 13, 23]
Removendo valores de uma lista
>>> stark = ['Ned', 'Robb', 'Arya', 'Brand', 'Rickon', 'Jon Snow']>>> stark.pop()'Jon Snow'>>> stark['Ned', 'Robb', 'Arya', 'Brand', 'Rickon']>>> stark.pop(0)'Ned'>>> stark['Robb', 'Arya', 'Brand', 'Rickon']
Separando palavras
>>> frase = 'Em todos estes anos nesta industria vital'
>>> frase.split()
['Em', 'todos', 'estes', 'anos', 'nesta', 'industria', 'vital']
>>> personagens = 'Cosmo, Wanda, Timmy Turner, Denzel Crocker'
>>> personagens.split(', ')
['Cosmo', 'Wanda', 'Timmy Turner', 'Denzel Crocker']
Unindo palavras
>>> palavras = ['Em', 'todos', 'estes', 'anos', 'nesta', 'industria', 'vital']
>>> separador = ' '
>>> separador.join(palavras)
'Em todos estes anos nesta industria vital'
>>> p = ['Cosmo', 'Wanda', 'Timmy Turner', 'Denzel Crocker']
>>> ', '.join(p)
'Cosmo, Wanda, Timmy Turner, Denzel Crocker'
Partição
>>> frase = 'A vinganca nunca eh plena, mata a alma e envenena.'>>> frase.partition('plena')('A vinganca nunca eh ', 'plena', ', mata a alma e envenena.')>>> frase.partition('envenena.')('A vinganca nunca eh plena, mata a alma e ', 'envenena.', '')>>> frase.partition('biscoito')('A vinganca nunca eh plena, mata a alma e envenena.', '', '')