Introdução à linguagem Python
-
Upload
alex-tercete -
Category
Technology
-
view
1.833 -
download
3
description
Transcript of Introdução à linguagem Python
![Page 1: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/1.jpg)
Introdução à linguagem PythonAlex Tercete Matos
6 de junho de 2009
![Page 2: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/2.jpg)
Antes de começar. . .
PythOnCampus Introdução à linguagem Python – 2 / 28
![Page 3: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/3.jpg)
Quem sou eu?
PythOnCampus Introdução à linguagem Python – 3 / 28
• CEFET/RJ
− Engenharia de Controle e Automação
• Chemtech
− Desenvolvimento de software
• Software Livre
− GNU/Linux− Python− etc.
![Page 4: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/4.jpg)
Quem sou eu?
PythOnCampus Introdução à linguagem Python – 3 / 28
• CEFET/RJ
− Engenharia de Controle e Automação
• Chemtech
− Desenvolvimento de software
• Software Livre
− GNU/Linux− Python− etc.
![Page 5: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/5.jpg)
Quem sou eu?
PythOnCampus Introdução à linguagem Python – 3 / 28
• CEFET/RJ
− Engenharia de Controle e Automação
• Chemtech
− Desenvolvimento de software
• Software Livre
− GNU/Linux− Python− etc.
![Page 6: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/6.jpg)
Quem são vocês?
PythOnCampus Introdução à linguagem Python – 4 / 28
• Nome
• Faz o que?
• Programa em qual(is) linguagem(ns)?
• Já conhece ou ouviu falar de Python?
• Por que está aqui?
![Page 7: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/7.jpg)
O que é Python?
PythOnCampus Introdução à linguagem Python – 5 / 28
• Linguagem interpretada
• Software livre
• Multiplataforma
• Procedural, OO e funcional
• VHLL: Very High Level Language
• Baterias incluídas!
− Extensa biblioteca padrão
![Page 8: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/8.jpg)
O que é Python?
PythOnCampus Introdução à linguagem Python – 5 / 28
• Linguagem interpretada
• Software livre
• Multiplataforma
• Procedural, OO e funcional
• VHLL: Very High Level Language
• Baterias incluídas!
− Extensa biblioteca padrão
![Page 9: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/9.jpg)
O que é Python?
PythOnCampus Introdução à linguagem Python – 5 / 28
• Linguagem interpretada
• Software livre
• Multiplataforma
• Procedural, OO e funcional
• VHLL: Very High Level Language
• Baterias incluídas!
− Extensa biblioteca padrão
![Page 10: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/10.jpg)
O que é Python?
PythOnCampus Introdução à linguagem Python – 5 / 28
• Linguagem interpretada
• Software livre
• Multiplataforma
• Procedural, OO e funcional
• VHLL: Very High Level Language
• Baterias incluídas!
− Extensa biblioteca padrão
![Page 11: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/11.jpg)
O que é Python?
PythOnCampus Introdução à linguagem Python – 5 / 28
• Linguagem interpretada
• Software livre
• Multiplataforma
• Procedural, OO e funcional
• VHLL: Very High Level Language
• Baterias incluídas!
− Extensa biblioteca padrão
![Page 12: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/12.jpg)
O que é Python?
PythOnCampus Introdução à linguagem Python – 5 / 28
• Linguagem interpretada
• Software livre
• Multiplataforma
• Procedural, OO e funcional
• VHLL: Very High Level Language
• Baterias incluídas!
− Extensa biblioteca padrão
![Page 13: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/13.jpg)
Um pouco mais sobre Python
PythOnCampus Introdução à linguagem Python – 6 / 28
![Page 14: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/14.jpg)
História
PythOnCampus Introdução à linguagem Python – 7 / 28
• Criada por Guido van Rossumem 1989
• Nome baseado no seriado in-glês Monty Python’s Flying
Circus
• Versão 2.0 lançada em 2000
• Versão 3.0 lançada em 2008
− Quebra de compatibilidade
![Page 15: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/15.jpg)
História
PythOnCampus Introdução à linguagem Python – 7 / 28
• Criada por Guido van Rossumem 1989
• Nome baseado no seriado in-glês Monty Python’s Flying
Circus
• Versão 2.0 lançada em 2000
• Versão 3.0 lançada em 2008
− Quebra de compatibilidade
![Page 16: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/16.jpg)
História
PythOnCampus Introdução à linguagem Python – 7 / 28
• Criada por Guido van Rossumem 1989
• Nome baseado no seriado in-glês Monty Python’s Flying
Circus
• Versão 2.0 lançada em 2000
• Versão 3.0 lançada em 2008
− Quebra de compatibilidade
![Page 17: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/17.jpg)
História
PythOnCampus Introdução à linguagem Python – 7 / 28
• Criada por Guido van Rossumem 1989
• Nome baseado no seriado in-glês Monty Python’s Flying
Circus
• Versão 2.0 lançada em 2000
• Versão 3.0 lançada em 2008
− Quebra de compatibilidade
![Page 18: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/18.jpg)
Características
PythOnCampus Introdução à linguagem Python – 8 / 28
• Sintaxe elegante, simples e clara
• Orientada a objetos (porém procedural e funcional,também)
• Tipagem dinâmica
• Tipagem forte
• Estruturas de dados de alto nível
− tuplas, listas e dicionários
• Blocos de código são delimitados por endentação (?)
![Page 19: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/19.jpg)
Características
PythOnCampus Introdução à linguagem Python – 8 / 28
• Sintaxe elegante, simples e clara
• Orientada a objetos (porém procedural e funcional,também)
• Tipagem dinâmica
• Tipagem forte
• Estruturas de dados de alto nível
− tuplas, listas e dicionários
• Blocos de código são delimitados por endentação (?)
![Page 20: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/20.jpg)
Características
PythOnCampus Introdução à linguagem Python – 8 / 28
• Sintaxe elegante, simples e clara
• Orientada a objetos (porém procedural e funcional,também)
• Tipagem dinâmica
• Tipagem forte
• Estruturas de dados de alto nível
− tuplas, listas e dicionários
• Blocos de código são delimitados por endentação (?)
![Page 21: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/21.jpg)
Características
PythOnCampus Introdução à linguagem Python – 8 / 28
• Sintaxe elegante, simples e clara
• Orientada a objetos (porém procedural e funcional,também)
• Tipagem dinâmica
• Tipagem forte
• Estruturas de dados de alto nível
− tuplas, listas e dicionários
• Blocos de código são delimitados por endentação (?)
![Page 22: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/22.jpg)
Características
PythOnCampus Introdução à linguagem Python – 8 / 28
• Sintaxe elegante, simples e clara
• Orientada a objetos (porém procedural e funcional,também)
• Tipagem dinâmica
• Tipagem forte
• Estruturas de dados de alto nível
− tuplas, listas e dicionários
• Blocos de código são delimitados por endentação (?)
![Page 23: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/23.jpg)
Características
PythOnCampus Introdução à linguagem Python – 8 / 28
• Sintaxe elegante, simples e clara
• Orientada a objetos (porém procedural e funcional,também)
• Tipagem dinâmica
• Tipagem forte
• Estruturas de dados de alto nível
− tuplas, listas e dicionários
• Blocos de código são delimitados por endentação (?)
![Page 24: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/24.jpg)
Características
PythOnCampus Introdução à linguagem Python – 8 / 28
• Sintaxe elegante, simples e clara
• Orientada a objetos (porém procedural e funcional,também)
• Tipagem dinâmica
• Tipagem forte
• Estruturas de dados de alto nível
− tuplas, listas e dicionários
• Blocos de código são delimitados por endentação (?)
![Page 25: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/25.jpg)
Vantagens
PythOnCampus Introdução à linguagem Python – 9 / 28
• Fácil de aprender
• Poderosa
• Linguagem dinâmica: quase tudo acontece em runtime
• Code → Test
• Facilidade de criação de módulos
− A comunidade pode facilmente criar e compartilharfuncionalidades
• Interpretador interativo: shell
![Page 26: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/26.jpg)
Vantagens
PythOnCampus Introdução à linguagem Python – 9 / 28
• Fácil de aprender
• Poderosa
• Linguagem dinâmica: quase tudo acontece em runtime
• Code → Test
• Facilidade de criação de módulos
− A comunidade pode facilmente criar e compartilharfuncionalidades
• Interpretador interativo: shell
![Page 27: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/27.jpg)
Vantagens
PythOnCampus Introdução à linguagem Python – 9 / 28
• Fácil de aprender
• Poderosa
• Linguagem dinâmica: quase tudo acontece em runtime
• Code → Test
• Facilidade de criação de módulos
− A comunidade pode facilmente criar e compartilharfuncionalidades
• Interpretador interativo: shell
![Page 28: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/28.jpg)
Vantagens
PythOnCampus Introdução à linguagem Python – 9 / 28
• Fácil de aprender
• Poderosa
• Linguagem dinâmica: quase tudo acontece em runtime
• Code → Test
• Facilidade de criação de módulos
− A comunidade pode facilmente criar e compartilharfuncionalidades
• Interpretador interativo: shell
![Page 29: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/29.jpg)
Vantagens
PythOnCampus Introdução à linguagem Python – 9 / 28
• Fácil de aprender
• Poderosa
• Linguagem dinâmica: quase tudo acontece em runtime
• Code → Test
• Facilidade de criação de módulos
− A comunidade pode facilmente criar e compartilharfuncionalidades
• Interpretador interativo: shell
![Page 30: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/30.jpg)
Vantagens
PythOnCampus Introdução à linguagem Python – 9 / 28
• Fácil de aprender
• Poderosa
• Linguagem dinâmica: quase tudo acontece em runtime
• Code → Test
• Facilidade de criação de módulos
− A comunidade pode facilmente criar e compartilharfuncionalidades
• Interpretador interativo: shell
![Page 31: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/31.jpg)
Quem usa?
PythOnCampus Introdução à linguagem Python – 10 / 28
• Empresas:
− Google, Yahoo!
− NASA, CERN
• Software:
− Trac
− GIMP, Blender, Inkscape
− BitTorrent
− Gnome
![Page 32: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/32.jpg)
Quem usa?
PythOnCampus Introdução à linguagem Python – 10 / 28
• Empresas:
− Google, Yahoo!
− NASA, CERN
• Software:
− Trac
− GIMP, Blender, Inkscape
− BitTorrent
− Gnome
![Page 33: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/33.jpg)
Onde roda?
PythOnCampus Introdução à linguagem Python – 11 / 28
• UNIX-likes
− GNU/Linux: padrão na grande maioria
− Ma OS X: padrão
• Windows: tem que instalar
• Nokia Series 60
• (. . . )
![Page 34: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/34.jpg)
Onde roda?
PythOnCampus Introdução à linguagem Python – 11 / 28
• UNIX-likes
− GNU/Linux: padrão na grande maioria
− Ma OS X: padrão
• Windows: tem que instalar
• Nokia Series 60
• (. . . )
![Page 35: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/35.jpg)
Onde roda?
PythOnCampus Introdução à linguagem Python – 11 / 28
• UNIX-likes
− GNU/Linux: padrão na grande maioria
− Ma OS X: padrão
• Windows: tem que instalar
• Nokia Series 60
• (. . . )
![Page 36: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/36.jpg)
Onde roda?
PythOnCampus Introdução à linguagem Python – 11 / 28
• UNIX-likes
− GNU/Linux: padrão na grande maioria
− Ma OS X: padrão
• Windows: tem que instalar
• Nokia Series 60
• (. . . )
![Page 37: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/37.jpg)
Botando a mão na massa!
PythOnCampus Introdução à linguagem Python – 12 / 28
![Page 38: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/38.jpg)
Conceitos básicos
PythOnCampus Introdução à linguagem Python – 13 / 28
• Variáveis
• Números
− Inteiros
− De ponto flutuante
− Complexos
• Strings
− ', """ e \− Concatenação
− Slices
![Page 39: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/39.jpg)
Conceitos básicos
PythOnCampus Introdução à linguagem Python – 13 / 28
• Variáveis
• Números
− Inteiros
− De ponto flutuante
− Complexos
• Strings
− ', """ e \− Concatenação
− Slices
![Page 40: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/40.jpg)
Conceitos básicos
PythOnCampus Introdução à linguagem Python – 13 / 28
• Variáveis
• Números
− Inteiros
− De ponto flutuante
− Complexos
• Strings
− ', """ e \− Concatenação
− Slices
![Page 41: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/41.jpg)
Mais conceitos básicos
PythOnCampus Introdução à linguagem Python – 14 / 28
• Listas
1 l i s t a = [ 'Alex' , 22 , 8 1 . 3 , [ 8 , 'CEFET' ] ]
− Acesso: lista[n℄, lista[n:℄, lista[:n℄,lista[n:m℄
− Mutáveis: lista[n℄ = valor− len(lista), lista.append()
![Page 42: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/42.jpg)
Mais conceitos básicos
PythOnCampus Introdução à linguagem Python – 14 / 28
• Listas
1 l i s t a = [ 'Alex' , 22 , 8 1 . 3 , [ 8 , 'CEFET' ] ]
− Acesso: lista[n℄, lista[n:℄, lista[:n℄,lista[n:m℄
− Mutáveis: lista[n℄ = valor− len(lista), lista.append()
![Page 43: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/43.jpg)
Mais conceitos básicos
PythOnCampus Introdução à linguagem Python – 14 / 28
• Listas
1 l i s t a = [ 'Alex' , 22 , 8 1 . 3 , [ 8 , 'CEFET' ] ]
− Acesso: lista[n℄, lista[n:℄, lista[:n℄,lista[n:m℄
− Mutáveis: lista[n℄ = valor− len(lista), lista.append()
![Page 44: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/44.jpg)
Mais conceitos básicos
PythOnCampus Introdução à linguagem Python – 14 / 28
• Listas
1 l i s t a = [ 'Alex' , 22 , 8 1 . 3 , [ 8 , 'CEFET' ] ]
− Acesso: lista[n℄, lista[n:℄, lista[:n℄,lista[n:m℄
− Mutáveis: lista[n℄ = valor− len(lista), lista.append()
![Page 45: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/45.jpg)
Estruturas condicionais
PythOnCampus Introdução à linguagem Python – 15 / 28
1 i f expressao1 :2 comando13 comando24 . . .
valor1 if expressao else valor2
![Page 46: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/46.jpg)
Estruturas condicionais
PythOnCampus Introdução à linguagem Python – 15 / 28
1 i f expressao1 :2 comando13 comando24 . . .5 e l i f expressao2 :6 comando37 . . .
valor1 if expressao else valor2
![Page 47: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/47.jpg)
Estruturas condicionais
PythOnCampus Introdução à linguagem Python – 15 / 28
1 i f expressao1 :2 comando13 comando24 . . .5 e l i f expressao2 :6 comando37 . . .8 e lse :9 comandoN
10 . . .
valor1 if expressao else valor2
![Page 48: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/48.jpg)
Estruturas condicionais
PythOnCampus Introdução à linguagem Python – 15 / 28
1 i f expressao1 :2 comando13 comando24 . . .5 e l i f expressao2 :6 comando37 . . .8 e lse :9 comandoN
10 . . .
• valor1 if expressao else valor2
![Page 49: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/49.jpg)
Estruturas de repetição
PythOnCampus Introdução à linguagem Python – 16 / 28
1 f o r i i n i t e r a v e l :2 comando13 comando2
list range() di t
break ontinue pass for... else
![Page 50: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/50.jpg)
Estruturas de repetição
PythOnCampus Introdução à linguagem Python – 16 / 28
1 f o r i i n i t e r a v e l :2 comando13 comando2
• iterável → list, range(), di t, etc.
break ontinue pass for... else
![Page 51: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/51.jpg)
Estruturas de repetição
PythOnCampus Introdução à linguagem Python – 16 / 28
1 f o r i i n i t e r a v e l :2 comando13 comando2
• iterável → list, range(), di t, etc.
1 whi le expressao :2 comando3
break ontinue pass for... else
![Page 52: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/52.jpg)
Estruturas de repetição
PythOnCampus Introdução à linguagem Python – 16 / 28
1 f o r i i n i t e r a v e l :2 comando13 comando2
• iterável → list, range(), di t, etc.
1 whi le expressao :2 comando3
• break, ontinue, pass, for... else
![Page 53: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/53.jpg)
Funções
PythOnCampus Introdução à linguagem Python – 17 / 28
1 def f uncao ( param1 , param2 , . . . ) :2 comando13 . . .4 [ r e t u r n v a l o r ]
• *args
• **kwargs → param1=valor, param2=valor, . . .
• lambda: funções anônimas
• docstrings
![Page 54: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/54.jpg)
Funções
PythOnCampus Introdução à linguagem Python – 17 / 28
1 def f uncao ( param1 , param2 , . . . ) :2 comando13 . . .4 [ r e t u r n v a l o r ]
• *args
• **kwargs → param1=valor, param2=valor, . . .
• lambda: funções anônimas
• docstrings
![Page 55: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/55.jpg)
Funções
PythOnCampus Introdução à linguagem Python – 17 / 28
1 def f uncao ( param1 , param2 , . . . ) :2 comando13 . . .4 [ r e t u r n v a l o r ]
• *args
• **kwargs → param1=valor, param2=valor, . . .
• lambda: funções anônimas
• docstrings
![Page 56: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/56.jpg)
Funções
PythOnCampus Introdução à linguagem Python – 17 / 28
1 def f uncao ( param1 , param2 , . . . ) :2 comando13 . . .4 [ r e t u r n v a l o r ]
• *args
• **kwargs → param1=valor, param2=valor, . . .
• lambda: funções anônimas
• docstrings
![Page 57: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/57.jpg)
Funções
PythOnCampus Introdução à linguagem Python – 17 / 28
1 def f uncao ( param1 , param2 , . . . ) :2 comando13 . . .4 [ r e t u r n v a l o r ]
• *args
• **kwargs → param1=valor, param2=valor, . . .
• lambda: funções anônimas
• docstrings
![Page 58: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/58.jpg)
Listas
PythOnCampus Introdução à linguagem Python – 18 / 28
• .append()
• .insert()
• .remove()
• .pop()
• . ount()
• .index()• .sort()• .reverse()
![Page 59: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/59.jpg)
Listas
PythOnCampus Introdução à linguagem Python – 18 / 28
• .append()
• .insert()
• .remove()
• .pop()
• . ount()
• .index()• .sort()• .reverse()
![Page 60: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/60.jpg)
Listas
PythOnCampus Introdução à linguagem Python – 18 / 28
• .append()
• .insert()
• .remove()
• .pop()
• . ount()
• .index()• .sort()• .reverse()
![Page 61: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/61.jpg)
Listas
PythOnCampus Introdução à linguagem Python – 18 / 28
• .append()
• .insert()
• .remove()
• .pop()
• . ount()
• .index()• .sort()• .reverse()
![Page 62: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/62.jpg)
Listas
PythOnCampus Introdução à linguagem Python – 18 / 28
• .append()
• .insert()
• .remove()
• .pop()
• . ount()
• .index()• .sort()• .reverse()
![Page 63: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/63.jpg)
Listas
PythOnCampus Introdução à linguagem Python – 18 / 28
• .append()
• .insert()
• .remove()
• .pop()
• . ount()
• .index()• .sort()• .reverse()
![Page 64: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/64.jpg)
Listas
PythOnCampus Introdução à linguagem Python – 18 / 28
• .append()
• .insert()
• .remove()
• .pop()
• . ount()
• .index()• .sort()• .reverse()
![Page 65: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/65.jpg)
Listas
PythOnCampus Introdução à linguagem Python – 18 / 28
• .append()
• .insert()
• .remove()
• .pop()
• . ount()
• .index()• .sort()• .reverse()
![Page 66: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/66.jpg)
Tuplas
PythOnCampus Introdução à linguagem Python – 19 / 28
1 t u p l a = ( 2 2 , 'Alex' , 9 . 6 )
• Bem mais rápidas
• Imutáveis:
1 >>> tu p l a [ 0 ] = 232 Traceback ( most r e ce n t c a l l l a s t ) :3 F i l e "<stdin >" , l i n e 1 , i n <module>4 TypeError : 'tuple' o b j e c t does not suppo r t
i tem ass ignment
![Page 67: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/67.jpg)
Tuplas
PythOnCampus Introdução à linguagem Python – 19 / 28
1 t u p l a = ( 2 2 , 'Alex' , 9 . 6 )
• Bem mais rápidas
• Imutáveis:
1 >>> tu p l a [ 0 ] = 232 Traceback ( most r e ce n t c a l l l a s t ) :3 F i l e "<stdin >" , l i n e 1 , i n <module>4 TypeError : 'tuple' o b j e c t does not suppo r t
i tem ass ignment
![Page 68: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/68.jpg)
Tuplas
PythOnCampus Introdução à linguagem Python – 19 / 28
1 t u p l a = ( 2 2 , 'Alex' , 9 . 6 )
• Bem mais rápidas
• Imutáveis:
1 >>> tu p l a [ 0 ] = 232 Traceback ( most r e ce n t c a l l l a s t ) :3 F i l e "<stdin >" , l i n e 1 , i n <module>4 TypeError : 'tuple' o b j e c t does not suppo r t
i tem ass ignment
![Page 69: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/69.jpg)
Dicionários
PythOnCampus Introdução à linguagem Python – 20 / 28
1 d i c = {'Alex' : 22 , 5 : 'anos' , ( 1 , 2 ) : 'agua' ,'posi ao' : ( 3 , 4 ) }
• di [ have℄
• have in di → True ou False• Não acessa na ordem!
1 f o r c , v i n d i c . i t e r i t e m s ( ) :2 comando13 . . .
![Page 70: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/70.jpg)
Dicionários
PythOnCampus Introdução à linguagem Python – 20 / 28
1 d i c = {'Alex' : 22 , 5 : 'anos' , ( 1 , 2 ) : 'agua' ,'posi ao' : ( 3 , 4 ) }
• di [ have℄
• have in di → True ou False• Não acessa na ordem!
1 f o r c , v i n d i c . i t e r i t e m s ( ) :2 comando13 . . .
![Page 71: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/71.jpg)
Dicionários
PythOnCampus Introdução à linguagem Python – 20 / 28
1 d i c = {'Alex' : 22 , 5 : 'anos' , ( 1 , 2 ) : 'agua' ,'posi ao' : ( 3 , 4 ) }
• di [ have℄
• have in di → True ou False• Não acessa na ordem!
1 f o r c , v i n d i c . i t e r i t e m s ( ) :2 comando13 . . .
![Page 72: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/72.jpg)
Dicionários
PythOnCampus Introdução à linguagem Python – 20 / 28
1 d i c = {'Alex' : 22 , 5 : 'anos' , ( 1 , 2 ) : 'agua' ,'posi ao' : ( 3 , 4 ) }
• di [ have℄
• have in di → True ou False• Não acessa na ordem!
1 f o r c , v i n d i c . i t e r i t e m s ( ) :2 comando13 . . .
![Page 73: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/73.jpg)
Programação Funcional
PythOnCampus Introdução à linguagem Python – 21 / 28
1 >>> l i s t a = [ 1 , 2 , 3 , 4 ]
![Page 74: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/74.jpg)
Programação Funcional
PythOnCampus Introdução à linguagem Python – 21 / 28
1 >>> l i s t a = [ 1 , 2 , 3 , 4 ]2 >>> f i l t e r ( lambda x : x % 2 == 0 , l i s t a )
![Page 75: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/75.jpg)
Programação Funcional
PythOnCampus Introdução à linguagem Python – 21 / 28
1 >>> l i s t a = [ 1 , 2 , 3 , 4 ]2 >>> f i l t e r ( lambda x : x % 2 == 0 , l i s t a )3 [ 2 , 4 ]
![Page 76: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/76.jpg)
Programação Funcional
PythOnCampus Introdução à linguagem Python – 21 / 28
1 >>> l i s t a = [ 1 , 2 , 3 , 4 ]2 >>> f i l t e r ( lambda x : x % 2 == 0 , l i s t a )3 [ 2 , 4 ]4 >>> map ( lambda x : x ∗ x , l i s t a )
![Page 77: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/77.jpg)
Programação Funcional
PythOnCampus Introdução à linguagem Python – 21 / 28
1 >>> l i s t a = [ 1 , 2 , 3 , 4 ]2 >>> f i l t e r ( lambda x : x % 2 == 0 , l i s t a )3 [ 2 , 4 ]4 >>> map ( lambda x : x ∗ x , l i s t a )5 [ 1 , 4 , 9 , 1 6 ]
![Page 78: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/78.jpg)
Programação Funcional
PythOnCampus Introdução à linguagem Python – 21 / 28
1 >>> l i s t a = [ 1 , 2 , 3 , 4 ]2 >>> f i l t e r ( lambda x : x % 2 == 0 , l i s t a )3 [ 2 , 4 ]4 >>> map ( lambda x : x ∗ x , l i s t a )5 [ 1 , 4 , 9 , 1 6 ]6 >>> reduce ( lambda x , y : x + y , l i s t a )
![Page 79: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/79.jpg)
Programação Funcional
PythOnCampus Introdução à linguagem Python – 21 / 28
1 >>> l i s t a = [ 1 , 2 , 3 , 4 ]2 >>> f i l t e r ( lambda x : x % 2 == 0 , l i s t a )3 [ 2 , 4 ]4 >>> map ( lambda x : x ∗ x , l i s t a )5 [ 1 , 4 , 9 , 1 6 ]6 >>> reduce ( lambda x , y : x + y , l i s t a )7 10
![Page 80: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/80.jpg)
Programação Funcional
PythOnCampus Introdução à linguagem Python – 21 / 28
1 >>> l i s t a = [ 1 , 2 , 3 , 4 ]2 >>> f i l t e r ( lambda x : x % 2 == 0 , l i s t a )3 [ 2 , 4 ]4 >>> map ( lambda x : x ∗ x , l i s t a )5 [ 1 , 4 , 9 , 1 6 ]6 >>> reduce ( lambda x , y : x + y , l i s t a )7 108 >>> z i p ( l i s t a , [ 1 , 4 , 9 ] )
![Page 81: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/81.jpg)
Programação Funcional
PythOnCampus Introdução à linguagem Python – 21 / 28
1 >>> l i s t a = [ 1 , 2 , 3 , 4 ]2 >>> f i l t e r ( lambda x : x % 2 == 0 , l i s t a )3 [ 2 , 4 ]4 >>> map ( lambda x : x ∗ x , l i s t a )5 [ 1 , 4 , 9 , 1 6 ]6 >>> reduce ( lambda x , y : x + y , l i s t a )7 108 >>> z i p ( l i s t a , [ 1 , 4 , 9 ] )9 [ ( 1 , 1 ) , ( 2 , 4 ) , ( 3 , 9 ) ]
![Page 82: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/82.jpg)
Programação Funcional
PythOnCampus Introdução à linguagem Python – 21 / 28
1 >>> l i s t a = [ 1 , 2 , 3 , 4 ]2 >>> f i l t e r ( lambda x : x % 2 == 0 , l i s t a )3 [ 2 , 4 ]4 >>> map ( lambda x : x ∗ x , l i s t a )5 [ 1 , 4 , 9 , 1 6 ]6 >>> reduce ( lambda x , y : x + y , l i s t a )7 108 >>> z i p ( l i s t a , [ 1 , 4 , 9 ] )9 [ ( 1 , 1 ) , ( 2 , 4 ) , ( 3 , 9 ) ]
• List comprehension
![Page 83: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/83.jpg)
Módulos
PythOnCampus Introdução à linguagem Python – 22 / 28
• arquivo.py
• import arquivo
• arquivo.X, arquivo.f()• from arquivo import X, f• from arquivo import *
1 i f __name__ == '__main__' :2 p r i n t 'Sou um programa!'
3 e lse :4 p r i n t 'Sou um modulo!'
![Page 84: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/84.jpg)
Módulos
PythOnCampus Introdução à linguagem Python – 22 / 28
• arquivo.py
• import arquivo
• arquivo.X, arquivo.f()• from arquivo import X, f• from arquivo import *
1 i f __name__ == '__main__' :2 p r i n t 'Sou um programa!'
3 e lse :4 p r i n t 'Sou um modulo!'
![Page 85: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/85.jpg)
Módulos
PythOnCampus Introdução à linguagem Python – 22 / 28
• arquivo.py
• import arquivo
• arquivo.X, arquivo.f()• from arquivo import X, f• from arquivo import *
1 i f __name__ == '__main__' :2 p r i n t 'Sou um programa!'
3 e lse :4 p r i n t 'Sou um modulo!'
![Page 86: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/86.jpg)
Módulos
PythOnCampus Introdução à linguagem Python – 22 / 28
• arquivo.py
• import arquivo
• arquivo.X, arquivo.f()• from arquivo import X, f• from arquivo import *
1 i f __name__ == '__main__' :2 p r i n t 'Sou um programa!'
3 e lse :4 p r i n t 'Sou um modulo!'
![Page 87: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/87.jpg)
Módulos
PythOnCampus Introdução à linguagem Python – 22 / 28
• arquivo.py
• import arquivo
• arquivo.X, arquivo.f()• from arquivo import X, f• from arquivo import *
1 i f __name__ == '__main__' :2 p r i n t 'Sou um programa!'
3 e lse :4 p r i n t 'Sou um modulo!'
![Page 88: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/88.jpg)
Módulos
PythOnCampus Introdução à linguagem Python – 22 / 28
• arquivo.py
• import arquivo
• arquivo.X, arquivo.f()• from arquivo import X, f• from arquivo import *
1 i f __name__ == '__main__' :2 p r i n t 'Sou um programa!'
3 e lse :4 p r i n t 'Sou um modulo!'
![Page 89: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/89.jpg)
Arquivos
PythOnCampus Introdução à linguagem Python – 23 / 28
1 >>> arq = open ( 'teste.txt' , 'w' )
'Teste de es rita\n''teste.txt''Teste de es rita\n'
![Page 90: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/90.jpg)
Arquivos
PythOnCampus Introdução à linguagem Python – 23 / 28
1 >>> arq = open ( 'teste.txt' , 'w' )2 >>> arq . w r i t e ( 'Teste de es rita\n' )3 >>> arq . c l o s e ( )
'teste.txt''Teste de es rita\n'
![Page 91: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/91.jpg)
Arquivos
PythOnCampus Introdução à linguagem Python – 23 / 28
1 >>> arq = open ( 'teste.txt' , 'w' )2 >>> arq . w r i t e ( 'Teste de es rita\n' )3 >>> arq . c l o s e ( )4 >>> arq = open ( 'teste.txt' )
'Teste de es rita\n'
![Page 92: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/92.jpg)
Arquivos
PythOnCampus Introdução à linguagem Python – 23 / 28
1 >>> arq = open ( 'teste.txt' , 'w' )2 >>> arq . w r i t e ( 'Teste de es rita\n' )3 >>> arq . c l o s e ( )4 >>> arq = open ( 'teste.txt' )5 >>> arq . r e a d l i n e s ( )6 [ 'Teste de es rita\n' ]
![Page 93: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/93.jpg)
Arquivos
PythOnCampus Introdução à linguagem Python – 23 / 28
1 >>> arq = open ( 'teste.txt' , 'w' )2 >>> arq . w r i t e ( 'Teste de es rita\n' )3 >>> arq . c l o s e ( )4 >>> arq = open ( 'teste.txt' )5 >>> arq . r e a d l i n e s ( )6 [ 'Teste de es rita\n' ]
1 f o r l i n h a i n arq :2 p r i n t l i n h a
![Page 94: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/94.jpg)
Classes
PythOnCampus Introdução à linguagem Python – 24 / 28
1 c las s NomeDaClasse :2 " " " Essa c l a s s e nao f a z muita co i s a " " "
3
4 s e l f . a t r i b u t o = v a l o r5 def metodo ( s e l f , param1 , param2 , . . . ) :6 comando1
• __init__()• Sobrecarga de operadores
• Herança múltipla
![Page 95: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/95.jpg)
Classes
PythOnCampus Introdução à linguagem Python – 24 / 28
1 c las s NomeDaClasse :2 " " " Essa c l a s s e nao f a z muita co i s a " " "
3
4 s e l f . a t r i b u t o = v a l o r5 def metodo ( s e l f , param1 , param2 , . . . ) :6 comando1
• __init__()• Sobrecarga de operadores
• Herança múltipla
![Page 96: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/96.jpg)
Classes
PythOnCampus Introdução à linguagem Python – 24 / 28
1 c las s NomeDaClasse :2 " " " Essa c l a s s e nao f a z muita co i s a " " "
3
4 s e l f . a t r i b u t o = v a l o r5 def metodo ( s e l f , param1 , param2 , . . . ) :6 comando1
• __init__()• Sobrecarga de operadores
• Herança múltipla
![Page 97: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/97.jpg)
Classes
PythOnCampus Introdução à linguagem Python – 24 / 28
1 c las s NomeDaClasse :2 " " " Essa c l a s s e nao f a z muita co i s a " " "
3
4 s e l f . a t r i b u t o = v a l o r5 def metodo ( s e l f , param1 , param2 , . . . ) :6 comando1
• __init__()• Sobrecarga de operadores
• Herança múltipla
![Page 98: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/98.jpg)
Baterias incluídas
PythOnCampus Introdução à linguagem Python – 25 / 28
• sys, os, re, string, zlib
• so ket, urllib, httplib, imaplib• time, datetime, math, random• stru t, pi kle, Pi kle, threading• logging, de imal• (. . . )
![Page 99: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/99.jpg)
Outras Bibliotecas
PythOnCampus Introdução à linguagem Python – 26 / 28
• NumPy
• S iPy
• MatplotLib
• PyODE
• Python-OpenGL• (. . . )
![Page 100: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/100.jpg)
Aplicações
PythOnCampus Introdução à linguagem Python – 27 / 28
• Scripts
• Computação Gráfica
• Web
• Jogos
• Aplicações Desktop
• (. . . )
![Page 101: Introdução à linguagem Python](https://reader034.fdocument.pub/reader034/viewer/2022042614/558b62ced8b42ae1628b462e/html5/thumbnails/101.jpg)
The Zen of Python
PythOnCampus Introdução à linguagem Python – 28 / 28
1 impor t t h i s