introdução à linguagem python

101
Introdução à linguagem Alex Tercete Matos 6 de junho de 2009

Upload: alex-tercete

Post on 25-Jun-2015

1.840 views

Category:

Technology


3 download

DESCRIPTION

Telas -- bastante baseadas nas do Álvaro (http://alvarojusten.blogspot.com/) -- utilizadas no minicurso de Python realizado na PythOnCampus do dia 6 de junho de 2009, na UVA de Cabo Frio.

TRANSCRIPT

Page 1: Introdução à linguagem Python

Introdução à linguagem PythonAlex Tercete Matos

6 de junho de 2009

Page 2: Introdução à linguagem Python

Antes de começar. . .

PythOnCampus Introdução à linguagem Python – 2 / 28

Page 3: Introdução à linguagem Python

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

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

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

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

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

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

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

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

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

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

Um pouco mais sobre Python

PythOnCampus Introdução à linguagem Python – 6 / 28

Page 14: Introdução à linguagem Python

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Botando a mão na massa!

PythOnCampus Introdução à linguagem Python – 12 / 28

Page 38: Introdução à linguagem Python

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Listas

PythOnCampus Introdução à linguagem Python – 18 / 28

• .append()

• .insert()

• .remove()

• .pop()

• . ount()

• .index()• .sort()• .reverse()

Page 59: Introdução à linguagem Python

Listas

PythOnCampus Introdução à linguagem Python – 18 / 28

• .append()

• .insert()

• .remove()

• .pop()

• . ount()

• .index()• .sort()• .reverse()

Page 60: Introdução à linguagem Python

Listas

PythOnCampus Introdução à linguagem Python – 18 / 28

• .append()

• .insert()

• .remove()

• .pop()

• . ount()

• .index()• .sort()• .reverse()

Page 61: Introdução à linguagem Python

Listas

PythOnCampus Introdução à linguagem Python – 18 / 28

• .append()

• .insert()

• .remove()

• .pop()

• . ount()

• .index()• .sort()• .reverse()

Page 62: Introdução à linguagem Python

Listas

PythOnCampus Introdução à linguagem Python – 18 / 28

• .append()

• .insert()

• .remove()

• .pop()

• . ount()

• .index()• .sort()• .reverse()

Page 63: Introdução à linguagem Python

Listas

PythOnCampus Introdução à linguagem Python – 18 / 28

• .append()

• .insert()

• .remove()

• .pop()

• . ount()

• .index()• .sort()• .reverse()

Page 64: Introdução à linguagem Python

Listas

PythOnCampus Introdução à linguagem Python – 18 / 28

• .append()

• .insert()

• .remove()

• .pop()

• . ount()

• .index()• .sort()• .reverse()

Page 65: Introdução à linguagem Python

Listas

PythOnCampus Introdução à linguagem Python – 18 / 28

• .append()

• .insert()

• .remove()

• .pop()

• . ount()

• .index()• .sort()• .reverse()

Page 66: Introdução à linguagem Python

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Outras Bibliotecas

PythOnCampus Introdução à linguagem Python – 26 / 28

• NumPy

• S iPy

• MatplotLib

• PyODE

• Python-OpenGL• (. . . )

Page 100: Introdução à linguagem Python

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

The Zen of Python

PythOnCampus Introdução à linguagem Python – 28 / 28

1 impor t t h i s