Escolar Documentos
Profissional Documentos
Cultura Documentos
" # $%
&
%' ( $ )
!"
#$%&
' !"&()$%&
#
*"
+&!,
) &
"
'&
'&
*"
+&!
$%&
(
( * , ,+
'&
(
(
%&
)
*
"
(
(
--- %& .*
*+
*
* 1
30 !
"
*)
*"
1
"
$%&
+!
$%&
5
2
6
!
30
&
'&
!
(
(
---
*+
&
7
! &"
1 30
4
0
7 !
"
9 2
Josu Labaki
#
/
$%&
&
;< 2$
:
=& 2
labaki@feis.unesp.br
-!(./
# "
0 1.!(&#
*"
+&!
>$
$%& ?
A;
@
$%&
C;
$%&
EF "
# "
. & (
34 "
&
56
>2
>H
A5
>@
>I
E/
# "
"
A>
7& 0#"
#!(& 34 "
&
89
>J
AC
A6
# "
30
" : && : #
() #
AB
&
8;
>
AI
C>
C=
CC
EJ
C@
EH
EA
# "
& &
# "
7)!$@
.0 . &
>/
A J 37
C6
>?
A()=
&
25
37
@>
K
@D
BA
BB
@;
DC
# "
=
<)! ")B) CCC
D;
>
30
DI
GC
GB
E $%&
@
/ *
GI
IA
B
# "
IC
E0 =
&
;2
>
* &
"
E
@
0 #G
CCC
* &
"
>HH
>HA
>HE
;
E /G
/.&
I@
IG
B
# " F
37
&
>HI
555
559
# "
>
0 1.!(&#
& B)
"
)(#
*"
+&!
*"
+&!H
$%&
5
30
+!
&
-! 5
1!
'
! $
'FL55. F %L &
M$
30
*
P
$%&
+!
'$%&
1!
&)
"
$%&
$%&
*)
*
37
M 1
!
30
*)
N
! $%&
"
$%&
=
)
"
* &
O
$%&
'
P
#
!
< 2!
!
+!
!
JR
;
#
%'&
$&
! =
5 &
$%&
3
%!
$%&
2
';2 +!
<,
Q& !
(
(
--- %&
F)
. 8
*
*
$%&
$%&
'&
(
(
--- %& *
8
*+
&
#'
&
%
%
(
%
"
%
#'
-
&
%
+
%
#!(&&.!"
#'
&
"
#(& (
*"
+&!C
L" "
$%&
!
!./.# I
&
&< #0#
=
/5
I
; "
*"
+&! 8C9 I
&
.
=
/5
!
*
"
"
*)
30
*)
$%&
; "
=
/5 !
!
* 37
S%& T
1)
"
&
$%&
#!(& *"
+&! /&0&)0# /#=
/)=
#(& #C
"
"
=
/5
*
"
*
0
)
"
U! .! V (
>>> a=1
>>> b=2
>>> a == b
# == testa se a igual a b
False
>>> a != b # != testa se a diferente de b
True
>>> a <> b # <> tambm testa se a diferente de b
True
>>> a > b # > testa se a maior que b
False
>>> a < b # < testa se a menor que b
True
>>> 2*a >= b # testa se o dobro de a maior ou igual a b
True
8*
$%&
!
"
1
"
$%&
VV
L"
# .J1 .
0112
3456 7 89
!#(# # ( /=
# #
$%&
"
"
&
*)
A!
M*
*)
*
>XA!
>N
!
"
N
!
* .
!
>N
!
$%&
*) !
*)
30 type(x)!
>>> valor1=2
>>> valor1
2
>>> type(valor1)
<type 'int'>
M
*
O!
O
M
*)
"
9N
>N
M
>N "
&3
O.
>N
!
M
J66<
M &
O
9N'
N
+
*)
$%&
0 .!" . & ! K
=
&#" ' 4
&
30
37
37
+! ".!< ' 1 +! =
. "
# !"
) =
#
*)
int! float
str!
(./.&!J .&
!
!
&
# "
>
. & (
34 "
&
".!<
0
$%&
*)
0
$%&
* &
* &
>>> palavra='termodinmica'
>>> palavra
'termodin\xe2mica'
& termodin\xe2mica
*)
! )
"
&
$
$%&
0
0
" !
!
M
&
O
:
"
N$
$%& !
!
K
K
O
K
$%& !
3
O
3
*8
3
3 Y
2 2Z
Y[
10
>>> palavra[2]
'r'
>>> 2*palavra[0]
'tt'
K
K
>A!
>>> palavra[9:12]
'mic'
&
$%&
3
*)
* .
3
=
palavra[9:12]
"
$
0! .
4
/9
>>> palavra[9:]
'mica'
11
3 'H+
I 0
>>> palavra[0:9]
'termodin\xe2'
3!
>>> palavra[:9]
'termodin\xe2'
print palavra[:]9
&
>>> palavra[1:8]
'ermodin'
12
>>> palavra[1:8:2]
'emdn'
0
A'
3
)!
>! C! @
D+ F )
>
G!
>>> palavra[1:8:3]
'eon'
)!
>! E
"
>>> palavra[7:0:-1]
'nidomre'
< 4
1
3
O
3 D
&
"
3 'O
+!
H!
30
>>> palavra[7::-1]
'nidomret'
13
>>> palavra[:4:-2]
'ai\xe2i'
'F
)
"
"
M:N
! 0
\xe2
?+
3 !
"
>>> palavra[::-1]
'acim\xe2nidomret'
)
"
&
:
"
R
!
"*
!
K
MUN
30 N
!
30
Palndromos so palavras ou sentenas que so iguais tanto se lidas de trs para frente quanto no sentido
normal.
14
30
len(x)!
&
*)
$%&
>>> len(palavra)
22
. "
#
;
K
$%&
>>> lista=[1,2,3]
>>> lista
[1, 2, 3]
<
!
*)
15
)
"
>>> lista[0]
1
>>> lista[0]+lista[2]
4
MUN
Z
>!A!C[
JO
UE "
30
Z
E[ '
4
\
+
Z
>!A!C[
>>> lista=lista+[4]
>>> lista
[1, 2, 3, 4]
>>> lista+4
>>> lista=lista+[0,0,0]
>>> lista
[1, 2, 3, 4, 0, 0, 0]
16
*8
"
Z
.A[
!
3
*
&
Z
.>[
!
>>> lista[-1]
0
>>> lista[2:-2]
[3, 4, 0]
&
>>> len(lista)
7
0
=
O
!
"
*8
0
17
>>> lista
['zero', 5, 'zero', 4, 0, 0, 0]
<0
&
"&
<
*)
&
!
)
4
+!
"
*
30
>>> linha1=[1,2,3]
>>> linha2=[0,-1,1]
>>> linha3=[3,3,3]
>>> matriz=[linha1,linha2,linha3]
>>> matriz
18
"
matriz[1]
&
09
>>> matriz[1][2]
1
) =
#
0
"
*)
;
!
O
0
1
>>> tupl=(1,2,3)
>>> tupl
(1, 2, 3)
>>> tupl[0]=0
19
tupl[0]=0
TypeError: object doesn't support item assignment
*)
!
O
&
$%& !
<
=
9>
N
"
*)
>>> a,b=b,a
>>> a
'Deu certo?'
>>> b
0
20
./.&!J .&
"
&
0
0
&
*8
" !
1
&
*
3
"
<
23000,
'denomia\xe7\xe3o':
ACHHH
&
*8
'ilha
\ &
solteira',
30 N $
>>> aurelio['vocao']='turismo'
N\
&
30 N
>>> aurelio
{'popula\xe7\xe3o':
23000,
'voca\xe7\xe3o':
'denomia\xe7\xe3o': 'ilha solteira', 'renda': 1500}
*8
&
'turismo',
21
>>> aurelio['renda']
1500
"
"
^"
&
>>> aurelio['renda']=aurelio['renda']+200
>>> aurelio['renda']
1700
&
"
&
>>> aurelio.keys()
['popula\xe7\xe3o', 'voca\xe7\xe3o', 'denomia\xe7\xe3o', 'renda']
>>> aurelio.has_key('idade')
False
>>> aurelio['idade']=33
>>> aurelio.has_key('idade')
True
3 dicionario.items()
' &
*8 +
>>> aurelio.items()
[('idade', 33), ('popula\xe7\xe3o', 23000), ('voca\xe7\xe3o', 'turismo'),
('denomia\xe7\xe3o', 'ilha solteira'), ('renda', 1700)]
22
# "
>
7& 0#"
#!(&&34 "
&
7& 0#"
#$%&(
".!<
F)
>>> constante=3.14
>>> print 'O valor de pi %f' %constante
O valor de pi 3.14
*
_
M
"
M
30
<
"
1
'C >E+!
!MN
"
23
>>> nome='abacaxi'
>>> caracteristica='amarelo'
>>> print '%s uma fruta' %nome
abacaxi uma fruta
>>> print '%s %s' %(nome,caracteristica)
abacaxi amarelo
"
!
O
&
M
N0
M N
M N
!
M N
M N
" O
"
M *
1N M
O
N
MN
!
<
7#
>
9
"
24
!
"
3.14
C!
!
E
3.1416
C!
3
>A
25
/) & ( 0#!. )=
#$%&( =
. "
#
>>> a=[1,2,3]
>>> a
[1, 2, 3]
?+
!
>>> a.append('poncan')
>>> a
[1, 2, 3, 'poncan']
>>> a.extend([0,0,-3])
>>> a
[1, 2, 3, 'poncan', 0, 0, -3]
F)
3
4
O
!
"
4
>>> b=[1,2,3]
>>> b.append([1,2,3])
>>> b
[1, 2, 3,[1, 2, 3]]
26
3 O
>>> a.insert(0,'start')
>>> a
['start',1,2,3,'poncan',0,0,-3]
&"
JO
/
remove(0)
@
*
H
N
>>> a.remove(0)
>>> a
['start', 1, 2, 3, 'poncan', 0, -3
>>> a.remove('poncan')
>>> a
['start', 1, 2, 3, 0, -3]
;
:
:
O
26
>>> a.pop(0)
'start'
>>> a
[1, 2, 3, 0, -3]
27
`
"
$
4
*
'
O+
MCN
M N
!
>>> a.count(3)
1
`
"
" !
K
30
*
>>>
Mohs=['Talco',
F)
&
"
'Gipsita',
'Calcita',
'Fluorita',
'Apatita',
'Cor\xedndon',
'Top\xe1zio',
'Quartzo',
'Ortocl\xe1sio',
28
# "
>
" : && : #
() #
&
!B)#!"
&CCC
<
* 1!
! 0
M-& N
@ 2
'L+ <
O
*8
M*N
* 30
&
M*N
>
>>> b=0
>>> while b < 5:
print b
b=b+1
0
1
2
3
4
*
)
&
1
@
$%&
29
! *
=
/5
* "
&
*
F
&
... b < 5
<
O
8
< 6
)
"
&
print b
-&
b=b+1!
=
/5
< 6
* 1
$
O!
0
2&
* "
0 &"
F)
O!
>>> b=1
>>> while b < 5:
print '%i dlares valem %.2f reais' %(b,b*2.98)
b=b+1
0
"
"
>!
&
"
N
> a@
*!
!
&
Voc tambm pode encontrar na literatura de Python o termo endentao, que significa a mesma coisa. No
entanto, indentao mais utilizado, embora seja um neologismo (j incorporado por alguns dicionrios da
lngua portuguesa).
30
! *
65U '
=
/5 +
!
b+=1!
b*=2
b+=n
b=b+n!
b*=n
b=b*n
!"#(# ( (#(&
F)
"
$%&
30
$!
26
0!
30
$
30
"
!
!
! *
M1N
O
!
F)
$!
2
6
"
31
AC
30
23232323
" !
$!
2
6
*)
37
32
30
*)
2
+6
K
CCC
=
.K
CCC
= CCC
<
* 1!
M1N
M8
7
2
>HHb N
7
2 0!
M1N 0
M1N 0
M8
M8
N
!
N
!
N
!
>HHb N
Hb N
33
30
M8
"
30
&
Mais de 100 C
F
O
&
"
"
'M>
CH! )
"
N
+
>>> b = 1
>>> while b < 4:
if b==1:
print '%i dlar vale %.2f reais' %(b,b*2.98)
else:
print '%i dlares valem %.2f reais' %(b,b*2.98)
b+=1
34
7&
$%&
#
8
M
'
"
Joo
Rafael
Douglas
>>> for x in a:
print '%s tem %i letras' %(x,len(x))
35
M$
N . M1N
"
'
+ ^
M` 0 N
'S
`0 T
+ 2
x
!
0 ?$
! M1N M` 0 N
len(x)
1
;
30
30
# 6!
# :!
>>> range(1,11,2)
[1, 3, 5, 7, 9]
!
O
>!
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1,11)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> for elefante in range(5):
print elefante**2
0
1
4
9
36
16
&
2
A6 $
*
!
'*O
N
&
*
'
30
& C+
*
' + <
0 &"
2
36
0
1
2
4
3
$%&
0 Joo
1 Rafael
2 Douglas
37
<
)
"
37
30
30
#
*
1 0 0
0 1 0
0 0 1
&
print '\n'
'\n'*k
,
<
&
*
!
&
&
#
*
&
" !
"
&
38
combustvel
asa
motor
&
"
)
"
:03
c
8 c
!
&
O
orcamento[teste123]
"
&
!
"
orcamento['combustvel']!
&
39
B (
dicionario.items()
2
&
9D
%E(
#M
*
F
*
* 1
M_N
?
Z
A!C!E!@!B!
8
0
( #
C
2
4
'
)!
! .>[
_
XX H+!
Nmero no primo
&"
#L 2
& !
.
8
*
O
#
!$
*
37
#
37
40
>>> b=10
>>> while 1:
b=b-1
print b
if b<7: break
9
8
7
6
30
while
1:
F
.
"
&
"
loop1:
blablabla
blebleble
loop2:
bliblibli
blobloblo
break2 # Cancela somente o loop2
break1 # Est alinhado com o loop1, portanto cancela somente loop1.
41
#"
#0 !"
&(
&
<
37
"
"
<
&
"
"
"
>>> lst=[]
>>> for nome in range(3):
lst.append(int(raw_input('Digite um valor: ')))
Digite um valor: 33
Digite um valor: -4
Digite um valor: a
30
'
9+
0
'%
%
0
"
ST
3
?;
42
>>> lst=[]
>>> for i in range(3):
while 1:
try:
lst.append(int(raw_input('Digite um valor: ')))
break
except:
print 'Digite somente nmeros!!!'
Digite um valor: 2
Digite um valor: a
Digite somente nmeros!!!
Digite um valor: r
Digite somente nmeros!!!
Digite um valor: t
Digite somente nmeros!!!
Digite um valor: 33
Digite um valor: e
Digite somente nmeros!!!
Digite um valor: w
Digite somente nmeros!!!
Digite um valor: -4.99
>>> lst
[2, 33, -4]
43
"*L
O
)
&
'
30 !
* ?+
E/
"
L
&
0
<
$!
&
4
!
30
M 1
".
2
6 2
8
N
!
&
"
O!
1
*
& ! M*
,N
!
8
?<0
1
&
"
!
"
O
0 &"
0
0 &
2
36!
"
1]
O
K
! )
"
30
"
30
"
O
8 !
!
0
44
-0.333333333333
-0.5
-1.0
No possvel dividir 1 por 0
1.0
0.5
:F
+ %
+ #
"
(#
>(
C
>(
C
! H <
> '
! H CCCCCCCCCCCCCCCC> 2
30
> H+
C!
! *
45
>>> 1+2
3
>>> 1+2.0
3.0
>>> 2*9
18
>>> 2*9.
18.0
>>> 1/3.
0.33333333333333331
# "
& &
.0 . &
$%&
1
;
"
]
30
=
/5 !
=
/5 !
"
$
N.!(&N
! "
=
/5 !
65U<
' 0
0
)
7.= I
!
"
#! #
&
1
$
9+!
0
46
. !
"
"
)! I
18
)!
&()=!
J@
F
/O
"
\
4
B 'BX>UAUC+
# perfeitos.py
n = int(raw_input('Digite o nmero a ser testado: '))
teste = 0
for i in range(1,n):
if n % i == 0:
teste=teste+i
if teste == n:
print n, ' um nmero perfeito'
else:
print n, 'no um nmero perfeito'
F
D
:H
"
=
*
30
0H ;
"
30
!
$!
0
!
*
47
!
!
"
3H ,!
2*
0
)
"
'
Z
>!A!C!E!
!
'>! + '
! .>[
! 0
?+
0 *
2
: 6
0
D
IH2
AH
K
.
*
#
"
5H
&
30 !
K
!
8H=
" 1
'
)!
N
!
N )
4
+
D
JH2 0 ! '
KH
N 0
4
'
J@
"
+!
>>>
Digite o nmero a ser testado: 6
6 um nmero perfeito
48
\)
"
J@
>>>
Digite o nmero a ser testado: 7
7 no um nmero perfeito
&
J@
'while 1:!
&
+!
while 1:
o cdigo aqui (indentado corretamente)
#
H&
G @D4G
(#
'
C
(
C
&
&:;;;;
# "
3
&
59 .
%
+
'
49
/
*
O;
" 1
=
/5 !
&
30
&
0
.
"
*
"
&
raw_input(Pressione ENTER)
! )
"
"
$!
<
30
"
! )
"
"
=
/5 !
& M
* 0
=
/5 N
3
1#$%&L
&
1'
9+!
"
O 30
$%&
#!/usr/bin/python
&
/usr/bin/python
<
$%&
"
1>H H
!
30
?
1!
$
!
)
"
$%&
50
# "
>
7)!$@
A()=
&
K
.!.!(&K
)!$@
!
*
37
"
"
O !
&
0
30 !
37
! *
&
1
37
$%&
def nome_da_funcao(parametro1,parametro2,...):
operacoes sobre os n parametros
)
$
"
"
30
>>> f(3)
9
51
30 !
>>> elevar(9,9)
387420489
L"
37
* &
def nome_da_funo(*nome1):
procedimentos...
1
*
>>> impressora(1,2,3)
(1, 2, 3)
52
"
>! A
30
* &
! )
"
>>> somatoria(1,2,3,4,5)
15
30
"
0
30 !
>>> somatoria(1,2,3,4,'teste')
53
=
c
30
%
(
'
L
'
37
#
#
&
'
9 ?
&
37
>>> testa_primo(28)
Nmero no primo
>>> testa_primo(7)
Nmero primo
54
<0 &"
30
'C+
30
:
30
>>> elevar(f(3),4)
6561
F
30
!
"
30
>>> user('Mamute')
Meu nome Mamute
>>> user()
Meu nome Labaki
>>> cadastro=[]
>>> def add(x,y='Casado',z=3):
cadastro.append([x,y,z])
>>> add('Mozart','Solteiro','sem filhos')
>>> cadastro
55
'sem
filhos'],
['Beethoven',
'Casado',
3],
'Casado',
3],
O!
>>> add('Salieri',z=4)
>>> cadastro
[['Mozart',
'Solteiro',
'sem
filhos'],
['Beethoven',
"
O
37
30
37
!
20
&
O
.
56
4
>>> print f.__doc__
Esta funo retorna o
quadrado de um nmero
7)!$@
&3
BOP!/.#
K
! &"
30
<
*
*
30
"
30
* 1!
#
%E(
map(funo1, seqncia1)
>>> elementos=[1,2,3,4,5,6]
>>> map(quadrado,elementos)
[1, 4, 9, 16, 25, 36]
*
=
37
57
>>> map(cambio,orcamento.items())
combustvel R$ 200
combustvel US$ 66.6666666667
asa R$ 1200
asa US$ 400.0
motor R$ 4500
motor US$ 1500.0
30
)
"
orcamento.items()
>>> orcamento.items()
[('combust\xedvel', 200), ('asa', 1200), ('motor', 4500)]
58
30
<
30
"
! ('combust\xedvel', 200)
30
('combust\xedvel',
componente[0]!
200)[0]!
8 c
!
c
AHHc
componente[1]
30
##
30
30
%E(
30 .
2
#
>>> reduce(soma,[1,2,3])
6
Z
>!A!C['
#
30
'
>UA X C
#
6
>+
A+
'
C+
CUC X B
?
#
G
C
%
&
9
9
59
>>> elementos=[4,3,2,1]
>>> reduce(eleva,elementos)
4096
3#
E & !"
3#
QQ E & !"
)=
"
#(&
.0 . & #
&
EVVC
BE
<)!(& #
&
BE
BEVVA
EHIB
/ . & #
&
EHIB
>
EHIBVV>
EHIB
)=
"
#(&
;
$%&
EHIB
* &
37
!
8
37
60
4
+!
K
'
K
Z
>!A!C[
* 1!
+!
Z
H! >! A! C! E[
!
*
&
37
*
30
"
&)
&
>>> valores=((4,5,6),(7,8,9),(5,6,-7))
>>> [a+b+c for (a,b,c) in valores]
[15, 24, 4]
E X @UB.D
37
61
<
>DB! >IG[
30
A!
Z
>CA! >@E!
30
"
!
30
"
) !1
.(
K
K /"
"
5"
???
"
)
"
)
&
"
30
37
30
4
! '1+ X >(
1 0
"
30
)
'
1XH+!
4
62
37
$%&
<
37
0
!
def nome_da_funo(argumento_1,argumento_2,...,argumento_n):
return parmetro
<
30 )
"
30
&
'4
>>> f(3)
9
30
!
##
!
'
>>> lista=[]
>>> def f(x):
lista.append(x)
print x
return x**2
63
>>> f(2)
2
4
>>> lista
[2]
A
30
30
##
* 1
30
>>> 2*f(9)
9
162
>>> lista
[2, 9]
'I+
IVVA
"
*
30 # '
>BA
##
! G> X
GI+
A!
30
\
R
&
30
!
N
30
<
30
30
64
>>> h(7)
343
>>> 2*h(7)
343
8 !)
"
[None,
30
None,
"
None]!
@G!
2
6
<
"
*
1
&"
30
##
1
".
#
-e
F
##
,
%
&
.#
%E(
#
&
##
65
A()=
&
7 !
"
37
R
M
"
O
0
37
!
8
37
"
30
65U< /
< =
/5 ! "
7.= I
37
"
)
0
&
&!
N R .!(&N!
.
"
*
'
&
#circulos.py
def perimetro(r):
"""Esta funcao calcula o perimetro de
Sem muito rigor, o path uma relao de todos os diretrios em que Python vai procurar pelo seu mdulo.
Se voc quiser, pode salvar seu mdulo na pasta Python23, em C:, que certamente estar no path. No entanto,
evite salvar todos os seus mdulo l, para no sobrecarregar este diretrio.
66
def area(r):
"""Esta funcao calcula a area de
um circulo de raio r."""
try:
r=float(r)
return 3.14*(r**2)
except:
print 'O argumento da funcao deve ser um numero.'
def potencia(x,y=2):
"""potencia(x,y) eleva x a potencia y.
Default: y=2."""
try:
return x**y
except:
print 'Argumentos invalidos'
def nada():
"""Esta funcao nao faz nada.
67
%&
J@
1
)
9 $
=
/5 '
&
)! I *"
+&! + =
=
+
)
37
>>> potencia(2,3)
30
"
37
2
)
(
$%& AC
&!
30
O.
68
30
>>> circulos.potencia(2,3)
8
>>> print circulos.potencia.__doc__
potencia(x,y) eleva x a potencia y.
Default: y=2.
>>> circulos.perimetro(33.)
207.24000000000001
>>> circulos.outrafuncao(1,2)
30
69
37
!
! *
O
=
/5
37
>>> nada()
37
70
$%&
&
*
0 M
N
!
3
3
!
!
$%&
)
" 1
&
O)
"
9 'N
$%& 9+
<
37
1
*
! C >E
0#"
+
71
3.1415926535897931
>>> from math import pi
>>> pi
3.1415926535897931
37
"
0.0
>>> log10(2)
0.3010299956639812
>>> pow(2,4)
16.0
>>> print pow.__doc__
pow(x,y)
72
&"
* &
1 !
<
30
3 &
(
(
%&
4
(
1&
30
)
"
$%&
<
0
8
30
0
&
"
& /
BB
"
&
/& &
7 !
!
*)
<
)
"
&
30
&
<=
&3#.
&
3).=
"
.! !
&
?R
?
'
+ =
&/#.
37 !
!
O
)
"
$%&
F)
>>> x=1000
>>> def f(x):
return x**2
>>> x
1000
>>> f(9)
73
81
>>> x
1000
+
30 #
&
* !
#2
:6!
+
& "
:
&
'
+
30 +!
&
:;;;
<
1 X >HHH
:+VVA /
*
!
1
1 X
* '
&3
+
'
"
:
O
>HHH
>>> type(12)
<type 'int'>
>>> def type(m):
print 'Digitastes',m
>>> type(12)
Digitastes 12
>>> def tipologia(x):
def type(y):
print 'Voc digitou',y
type(x)
74
>>> tipologia(12)
Voc digitou 12
>>> type(12)
Digitastes 12
8&
*)
*)
30
'
30
O !
+!
!
30
30
30
0
M/
N
!
*
*
*
*
4
0
30 !
*
<
30 +
30 \
# *
&
*
F)
30 !
/
'
0
.M
!
* cc 0
>>> area(.7)
28.274333882308138
>>> def volume(inc):
75
return (pi*(t+inc)**3)/3.
>>> volume(.4)
)
"
!
!
30
1
*
.
\
30
"
>>> volume(.4)
1.0471975511965976
>>> t
76
"
".
30
*
>>> volume(0.4)
1.0471975511965976
>>> t
0.59999999999999998
"
*
)
" 1
37
!
O
&
0
37
!
&
30
77
'
30
1N
+!
>>> sin('lhe')
D-lhe ajuda!
>>> from math import *
>>> cos(pi)
-1.0
>>> sin('me')
30
"
1
&
30
&
78
# "
>
=
<)! ")B) CCC
#!. )=
#$%&( # B).1&
30
".
30
30
open(endereo/nome_do_arquivo.extenso,modo_de_abertura)
*
*
#
'M N
+]
'M N
+]
N
'M-N
9 $
* &
!
30
abertura.close()
* &
30
* &
&
open('c:/tabela.txt','w').close()!
>>> abertura=open('c:/tabela.txt','w')
79
S).(#(&
)
" 0
&
&
! "
1 2
"
9
F" \
&
G
!
1 $
>>> abertura.close()
<0
&.
* &
*)
$%&
abertura.close()!
30
1
O !
*.
*
"
&
"
"
&
! 0
!
!
O
.
>>> abrindo=open('c:/tabela.txt','a')
$
$
)
" 1
*
26!
30
80
)
"
!
*
print!
&
/
*
37
$!
GH
%
+
L
'
&9 ^ *
&
'\n'
30
'
QB (
9 .
'
>>> abrindo.close()
>>> vamosdigitar=open('c:/tabela.txt','a')
>>> for i in range(100):
vamosdigitar.write('%i dlares valem %f reais\n' %(i,i*2.98))
>>> vamosdigitar.close()
&
S*
1T
>>> abertura=open('c:/tabela.txt','r')
81
L"
&
&
. *
*
''!
>>> abertura.readline()
'Tabela de d\xf3lares0'
>>> abertura.readline()
'0 dlares valem 0.00 reais'
>>> abertura.close()
&
!
)
"
6
* &
>>> w=open('c:/tabela.txt','r')
>>> lst=w.readlines()
>>> print lst[0]
Tabela de dlares
+!
30
1 !
"
82
!
1
30
/ 2!
/!
A()=
&
"
#/.&!#=
30
" ?
*
37
30
>>> import os
>>> atalho=os.startfile
>>> atalho('c:/tabela.txt')
<
&!
' +
&
37
O 30
&
2
*8
0
1
"
'
os.startfile('c:/tabela.txt')
+
+!
" 1
1
/ 2!
!
"
&
>>> import os
>>> a=os.system
83
>>> a('c:/tabela.txt')
0
'
9+
/ 1
"
(
$%& AC!
1
&
<
(
$%& AC!
"]
%
%
M<
"
(
$%& .<
]
1
"
<0
O
<
"
30
3
%
84
'
4 +
$
M
&
"
M
N
!
N
!
#instalar.py
import os
comando=os.system
comando('copy a:\meumodulo.py c:\Python23')
NOME=raw_input('Seu nome: ')
# Linha 4
NOME='Python-'+NOME
comando('md c:\''+NOME)
# Linha 6
*
M
30
& B
/ 2
<
M$%& .<
N
] M<
G &
"
4
!
!
&
*
&
! )
" "
"
&
( ?
L
30
<
%
>%
(
85
.0
#!(&0
&
30
&
!
30
`
"
!
>>> salve=time.localtime()
>>> print 'Hoje ',salve[2],'do',salve[1],'de',salve[0]
Hoje 20 do 5 de 2004
`
"
&!
* &
,
1
30
"
!
$
&
86
0
K
&
>
87
>>> a
['Woiski', 'Barata', 'Labaki', 'Bestinha', 'Osvaldo', 'Fred', 'Thiago']
>>> sorteio(a)
'Fred'
>>> sorteio(a)
'Ricardo'
>>> sorteio(a)
'Labaki'
&)
' Z
H!>[
+
F)
* &
0 5 3 9 4
88
*"
+&!
)"
&S
L 3&! #0.<&
L"
30
1
&
* 1
&
! "
/
/
&&= I
)! / . "
CCC!
Line
0,0
10,0
10,10
0,10
0,0
$
*
!
2
&
\
*
&
&
&
8*
<
&
&
M
&
)!
&
&
'
+
;
&
89
rectangle
0,0
10,10
"
<
< 6
6
M>H!>HN ^
&
:
`
"
&
@H
* &
M
"
"
]
8
& $%&
)
"
*
0
:
;
37
* 1
#retangulos.py
import math
raiz=math.sqrt
A=float(raw_input('Area inicial: '))
w=open('c:/nosso_script.scr','w')
for i in range(1,51):
a=raiz(A)
w.write('rectangle\n')
pontoi=str(-a/2)+','+str(-a/2)+'\n'
pontof=str(a/2)+','+str(a/2)+'\n'
90
w.write(pontoi)
w.write(pontof)
A=2*A
w.write('Zoom\n')
w.write('Extents\n')
w.close()
IH
5HM N
"
e
!
"
"
8H
/! M
&
D
JH
(
A
30
8
%
& 'S
h T
+ J3
&
=
/5
"
' +
"
AH!
'-2.2360679775,-2.2360679775\n'
D
KH
0 ' (
A! (
A+
:;H
')
"
&+
D
::H
:0H/
:
"
&
&
1
*
"
91
:3
:IH <
& !
&
O 30
# "
U ./#:0#
"
'
"&
"
! 0
?
! Beep(freqncia,durao)!
winsound!
#
%E(
O!
CD
CADBD!
30
?
$
30
$
26
!
"
"
2
<
92
'
'
"
2
I69
2
: A6
+
S#=!(#
* &
2 6!
!
!
93
2
+ 6
'
X>!
3.
XA!
>>> calendar.monthrange(2004,5)
(5, 31)
4 !
"
2
+ 6!
+ $
*
0
! 8
>>> calendar.prmonth(1922,2)
February 1922
Mo Tu We Th Fr Sa Su
9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28
94
E0 =
&2
# "
!
"
*
L"
&
"
".
56C5C
&
#3#=
+#!(& /&0 K
)!$@
&
4
8
"
!
0!
"
0!
*
*
37 !
"
def testa_par(x):
if x % 2 == 0:
print x,' um nmero par'
else:
print x,' um nmero mpar'
def testa_primo(valor):
teste=0
for individuo in range(2,valor):
if valor % individuo == 0:
No perca tempo digitando estes exemplos: eles esto disponveis para download em http://labaki.tk
95
teste=teste+1
if teste <> 0:
print valor,'no primo'
else:
print valor,' primo'
def testa_perfeito(gretagarbo):
verifica=1
for qualquerum in range(1,gretagarbo):
if gretagarbo % qualquerum == 0:
verifica=verifica+qualquerum
if verifica == gretagarbo:
print gretagarbo,' perfeito'
else:
print gretagarbo,'no perfeito'
def procura_divisores(n):
lista_de_divisores=[]
for i in range(1,n):
if n % i == 0:
lista_de_divisores.append(i)
if len(lista_de_divisores)==0:
print n,'No tem divisores'
else:
print 'Divisores de',n,':'
for i in lista_de_divisores:
96
print i,
def soma_algarismos(n):
n=str(n)
while len(n)>1:
m=0
for i in n:
m=m+int(i)
n=str(m)
print n
"
30 soma_algarismos
&
30 !
K
&
'while
'
>
1+!
30 +
'
while 1:
#
&
len(n)
'
97
56C8C /#"
/+V0#.=
#3#=
+#!(&/&0 ".!<
*
&
M$
"
*
0
.
N
!
"
&
3
"
&
.
3
*
.
)
"
&
;
+
1
3
"
*
3
30
try:
w=open('c:/mailcollection.txt','a')
except:
w=open('c:/mailcollection.txt','w')
lista=raw_input('Mail: ')
lista=lista.split()
for i in range(len(lista)):
lista[i]=lista[i].replace('<','').replace('>','').replace(',','')
mail=[]
for i in range(len(lista)):
if lista[i].__contains__('@'):
mail.append(lista[i])
for i in range(len(mail)):
mail[i]=mail[i]+','
print 'Enderecos capturados:\n'
98
for i in mail:
print i,
w.write(i)
w.close()
&
'
*)
J- ! 6 !
+!
&
65U
65U
M
NF
)
"
0
"
"!
<
"
+!
'
+!
O2
0
&
2
$6!
$
3
>>> 'araraquara'.split('a')
['', 'r', 'r', 'qu', 'r', '']
ST
99
`
"
&
S
*T
2 6
ST
& >>
&"
: !!
!!2
:6
56C9C
S#( .# (
=
. "
#
O
'
>>!
>A!
"
+
100
&
&
<
for i
'
"
&
O '
O+
for j
30 9+
&
&
for i
"
for i+ `
"
0
30
0
'
"
*
30
4
print '\n'
0
"
#
*
,
#
*
&
for w
101
&
w!
O' 1
&
:+
for
1
56C>C
./.&!J .&
&!
102
if morse[i][j]=='.':
Beep(500,50*vel)
sleep(0.1*vel)
elif morse[i][j]=='-':
Beep(500,150*vel)
sleep(0.1*vel)
else:
sleep(0.3*vel)
sleep(0.3*vel)
* ?
O
"
"
*
!
30
"
"
P ,$
#
"
* 1
'
"
M N
" +
\ &
"
M N
"
Z
ST
[
!
MC M '
\ &
'.+
& ! sleep(0.3*vel)!
for j!
)
'+
30 !
@HH LO
; 31
)!
1
"
"
for j
&8
*
&8
'
3 +
"
"
&
$%& !
"
^!
89
103
56C2C ! N"
&! # + &!C
1 !(& 0 # B).1&
30
30 !
8 !
>H
!
37
*3
30 !
&
30
"
30
<
U>
x k +1 = x k
<-
=
!
30
<O
.6 &
30 !
.6 &
f ( xk )
f ' ( xn )
t0=time()
104
!
30
f='x-(('+funcao+')/('+derivada+'))'
30
<-
.6 &
!)
"
w=open('c:/newton-raphson.txt','w')
w.write(' Newton-Raphson\n\n')
if localtime()[3]<=12:
w.write(' Bom dia!\n\n')
elif 12<localtime()[3]<18:
w.write(' Boa tarde!\n\n')
else:
w.write(' Boa noite!\n\n')
&
8 !
"
* !
"
8
2
6
3 C
&
105
30
&
.
*
+;
MF
* &
.
\
'\n\n' '
&
x=x0
for p in range(10):
w.write('Iterao '+str(p+1)+':\n')
x=eval(f)
w.write(str(x)+'\n')
w.write('\n')
*
<-
.6 &
30
>H
37
2
#6
30
&
1 F)
&
&
'
M1N
1
9+
>>> string='2+a+b'
>>> a=1
>>> b=5
>>> eval(string)
8
+
.
1>H
!
2
.
106
'\n'
O
"
&
2
"
2
6
! time()-t0
"
<0 &"
2
6!
!
*
"
&
w.close()
startfile('c:/newton-raphson.txt')
J3
"
! )
"
1
P
@
-
30 $
*
O
0
/ 2
)
AHHH!
&
37
/
3
t=open('c:/Arquivos de Programas/ACAD2000/graficos.scr','w')
`
"
30
30
107
def arange(xi,xf,i):
a=[]
while xi<=xf:
a.append(xi)
xi=xi+i
return a
S
1
AH!
&
t.write('color\n')
t.write('20\n')
0
&
1
"
& "
!
+; /
"
2
+ #2
+66!
g
+
9
t.write('line\n')
funcao=funcao.replace('x','elemento')
for elemento in arange(x,x0,0.1):
t.write(str(elemento)+','+str(eval(funcao))+'\n')
t.write('\n')
t.write('Zoom\n')
t.write('Extents\n')
t.close()
108
/ "
&&= I
)! / . "
56C?C
04
&<).!+& .0 = ( 0 0A .#
4
4
K
$
`
$
`
$
`
&
)
"
!
&
. 1
H
H
E
HE
D
HED
HE@C
&)
import random
r=random.random
print 'Memoria\n'
print 'Nao joque usando IDLE\n'
print 'Digite todos os numeros que forem'
print 'informados pelo programa'
*)
&N
109
escolha=int(10*r())
print escolha
a=str(escolha)
user=str(int(raw_input('Tente: ')))
*
3
" !
while user==a:
escolha=int(10*r())
print escolha
a=a+str(escolha)
user=str(int(raw_input('Tente: ')))
print '\n'*100
&
\
=
/5 !
&
"
>HH
30
"
4
0
&
4
\
)!
110
"
"
/ 2
"
&
=
/5 !
"
* "
# "
E /G
/.&
J"
1
*
'A!
*
!
+
30
O
'1+!
'>!
+?$
!
"
& )!
1
&
"
)
"
E!
4
1
*
'
E *
J3
)
&
*
)
&
" @C+
111
"
3!
1
M5
N
&
O
O
!
&
,
.* ,
0
0
*
AHH
M
5 8
5 8
O
N
!
".
x, y = 5, 3
print x
print y
def one():
x = 1
print x
two(x)
print x
def two(y):
print y
y = 2
print y
z = one
y = z()
print x
print y
112
0 #G
CCC
! $%&
*)
30
*"
^
$%&
SL
&
)
"
!
'
30
*)
'
+ $
*)
9+
$%& !
9
R
37 !
*
1
30
&
0
<
"
#
!
0
<
"
!
30
*)
30
!
"
30
"
"
9
Sua participao muito importante para a constante
melhoria deste material. Ficarei muito honrado em
conhecer suas opinies, sugestes, crticas ou dvidas
sobre ele. Para isso, posso ser encontrado em
labaki@feis.unesp.br.
Agradeo a todos que j colaboraram, em especial ao Prof.
E. R. Woiski, Ph.D, pelas observaes e a toda a lista
python-brasil por diversas mensagens que ajudaram a
melhorar muitos pontos ao longo do texto.
J. Labaki
labaki@feis.unesp.br
http://labaki.tk
" (&/)0 !"
& &(
(. ".3)G
(&=
.1 0 !" :( ( B) 0#!"
.(& & / W(."
& (&#)"
&C
113