Post on 08-Apr-2018
transcript
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 1/40
I n t r o d u c c i ó n c o n c e p t u a l y p r á c t i c a a l l e n g u a j e d e
p r o g r a m a c i ó n P y t h o n
R o b e r t o B o n v a l l e t
< r b o n v a l l @ a l u m n o s . i n f . u t f s m . c l >
D e p a r t a m e n t o d e I n f o r m á t i c a
U n i v e r s i d a d T é c n i c a F e d e r i c o S a n t a M a r í a
2 0 d e j u n i o d e 2 0 0 6
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 2/40
A g e n d a
1 . B l a b l a i n t r o d u c t o r i o ( e n c u r s o )
2 . E j e m p l o s h o l a m u n d í s t i c o s
3 . T u t o r i a l d e l l e n g u a j e
4 . E j e m p l o s s e n c i l l o s
5 . B l a b l a l o s ó c o
6 . M á s e j e m p l o s
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 3/40
Q u é e s P y t h o n ( 1 / 2 )
D e n i c i ó n m a r k e t e r a d e l F A Q d e h t t p : / / p y t h o n . o r g : P y t h o n i s
a n i n t e r p r e t e d , i n t e r a c t i v e , o b j e c t - o r i e n t e d
p r o g r a m m i n g l a n g u a g e . I t i n c o r p o r a t e s m o d u l e s ,
e x c e p t i o n s , d y n a m i c t y p i n g , v e r y h i g h l e v e l d y n a m i c
d a t a t y p e s , a n d c l a s s e s . P y t h o n c o m b i n e s r e m a r k a b l e
p o w e r w i t h v e r y c l e a r s y n t a x . I t h a s i n t e r f a c e s t o
m a n y s y s t e m c a l l s a n d l i b r a r i e s , a s w e l l a s t o v a r i o u s
w i n d o w s y s t e m s , a n d i s e x t e n s i b l e i n C o r C + + . I t i s
a l s o u s a b l e a s a n e x t e n s i o n l a n g u a g e f o r a p p l i c a t i o n s
t h a t n e e d a p r o g r a m m a b l e i n t e r f a c e . F i n a l l y , P y t h o n
i s p o r t a b l e : i t r u n s o n m a n y U n i x v a r i a n t s , o n t h e
M a c , a n d o n P C s u n d e r M S - D O S , W i n d o w s ,
W i n d o w s N T , a n d O S / 2 .
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 4/40
Q u é e s P y t h o n ( 2 / 2 )
C a r a c t e r í s t i c a s f u n d a m e n t a l e s :
I n t e r p r e t a d o
b y t e c o m p i l e d
M u l t i p a r a d i g m a ( ? )
M u l t i p l a t a f o r m a
S i s t e m a d e t i p o s :
v e r i c a c i ó n d i n á m i c a ( d u c k t y p i n g )
s t r o n g t y p e d
A d m i n i s t r a c i ó n a u t o m á t i c a d e m e m o r i a
M a n e j o d e e x c e p c i o n e s
T i p o s d e d a t o s d e a l t o n i v e l
I n t é r p r e t e i n t e r a c t i v o > > >
C a p a c i d a d d e i n t r o s p e c c i ó n
T o d o e s u n o b j e t o
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 5/40
E j e m p l o s h o l a m u n d í s t i c o s
E s c r i b i r h o l a m u n d o
E s c r i b i r l a l e t r a d e U n e l e f a n t e s e b a l a n c e a b a . . .
E s c r i b i r l a l e t r a d e 9 9 b o t t l e s o f b e e r o n t h e w a l l
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 6/40
V a r i a b l e s , a s i g n a c i o n e s y o p e r a c i o n e s
N o m b r e s d e v a r i a b l e s
A s i g n a c i o n e s
O p e r a c i o n e s e l e m e n t a l e s
> > >
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 7/40
S t r i n g s
S t r i n g s s o n i n m u t a b l e s
O p e r a c i o n e s r e t o r n a n u n n u e v o s t r i n g
C o n c a t e n a t i o n
S l i c i n g
S t r i n g f o r m a t t i n g
M é t o d o s e s p e c í c o s
> > >
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 8/40
E s t r u c t u r a s d e d a t o s
T u p l a s
( 1 , 2 , 3 )
t u p l e ( )
L i s t a s
[ 1 , 2 , 3 ]
l i s t ( )
D i c c i o n a r i o s
{ ' a ' : 1 , ' b ' : 2 , 1 0 0 : 3 }
d i c t ( )
C o n j u n t o s
s e t ( )
> > >
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 9/40
S e n t e n c i a d e c o n t r o l i f
s i n t a x i s :
i f c o n d i t i o n :
d o s o m e t h i n g ( )
d o o t h e r t h i n g ( )
e l i f a n o t h e r c o n d i t i o n :
b l a h b l a h ( )
e l s e :
e n o u g h ( )
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 10/40
S e n t e n c i a d e c o n t r o l w h i l e
i t e r a m i e n t r a s s e c u m p l a u n a c o n d i c i ó n
b r e a k , c o n t i n u e
s i n t a x i s :
w h i l e c o n d i t i o n :
d o s o m e t h i n g ( )
y o u k n o w w h a t ( )
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 11/40
S e n t e n c i a d e c o n t r o l f o r
i t e r a s o b r e u n o b j e t o i t e r a b l e
b r e a k , c o n t i n u e
s i n t a x i s :
f o r v a r i n i t e r a b l e :
d o s o m e t h i n g ( v a r )
d o s o m e t h i n g e l s e ( )
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 12/40
E j e m p l o s i d i o m á t i c o s d e u s o d e f o r ( 1 / 3 )
f o r i i n r a n g e ( 1 0 ) :
p r i n t i ∗∗ 2
b i g n u m b e r s = [ 1 5 4 , 1 2 3 , 4 3 , 1 6 1 , 3 7 ]
f o r n , v a l i n e n u m e r a t e ( b i g n u m b e r s ) :
i f v a l < 1 0 0 :
b i g n u m b e r s [ n ] + = 1 0 0
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 13/40
E j e m p l o s i d i o m á t i c o s d e u s o d e f o r ( 2 / 3 )
v a r s = { }
f o r l i n e i n f i l e ( " c h a l l a " ) :
i f l i n e . s t a r t s w i t h ( " # " ) :
c o n t i n u e
k e y , v a l u e = l i n e . s p l i t ( )
v a r s [ k e y ] = v a l u e
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 14/40
E j e m p l o s i d i o m á t i c o s d e u s o d e f o r ( 3 / 3 )
c a p i t a l e s = {
' C h i l e ' : ' S a n t i a g o ' ,
' P e r u ' : ' L i m a ' ,
' B o l i v i a ' : ' L a P a z ' ,
' A r g e n t i n a ' : ' B u e n o s A i r e s '
}
f o r p a i s i n s o r t e d ( c a p i t a l e s ) :
p r i n t " L a c a p i t a l d e % s " \
" e s % s " % ( p a i s , c a p i t a l e s [ p a i s ] )
f o r p a i s , c i u d a d i n c a p i t a l e s . i t e m s ( ) :
p r i n t " L a c a p i t a l d e % s " \
" e s % s " % ( p a i s , c i u d a d )
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 15/40
D e c l a r a c i ó n d e f u n c i o n e s c o n d e f ( 1 / 3 )
d e f i s p r i m e ( n ) :
" D e t e r m i n a s i e l a r g u m e n t o e s p r i m o "
f o r d i n r a n g e ( s q r t ( n ) ) :
i f n % d = = 0 :
r e t u r n F a l s e
r e t u r n T r u e
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 16/40
D e c l a r a c i ó n d e f u n c i o n e s c o n d e f ( 2 / 3 )
d e f c o u n t l e t t e r s ( m e s s a g e , t o c o u n t = ' a e i o u ' ) :
" " " C u e n t a e n e l m e n s a j e l a s o c u r r e n c i a s
d e l a s l e t r a s d a d a s . P o r d e f e c t o , c u e n t a
l a s v o c a l e s . " " "
n = 0
f o r l e t t e r i n m e s s a g e :
i f l e t t e r i n t o c o u n t :
n + = 1
r e t u r n n
a = c o u n t l e t t e r s ( " H o l a m u n d o " )
b = c o u n t l e t t e r s ( " H o l a m u n d o " , " m " )
c = c o u n t l e t t e r s ( m e s s a g e = " H o l a m u n d o " ,
t o c o u n t = " o " )
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 17/40
D e c l a r a c i ó n d e f u n c i o n e s c o n d e f ( 3 / 3 )
d e f s h o w ( t i m e s , ∗ a r g s , ∗∗ k w a r g s ) :
f o r i i n r a n g e ( t i m e s ) :
f o r a r g i n a r g s :
p r i n t a r g
f o r k e y i n k w a r g s :
p r i n t k e y , k w a r g s [ k e y ]
s h o w ( 5 , ' b l a h ' , ' y u h u ' , 3 4 , a = 2 . 1 , b = ' x x x ' )
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 18/40
E x c e p c i o n e s t r y / e x c e p t ( 1 / 3 )
a = f l o a t ( r a w _ i n p u t ( ) )
b = f l o a t ( r a w _ i n p u t ( ) )
t r y :
p r i n t " E l c u o c i e n t e e s % f " % ( a / b )
e x c e p t Z e r o D i v i s i o n E r r o r :
p r i n t " N o s e p u e d e h a c e r d i v i s i o n p o r c e r o "
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 19/40
E x c e p c i o n e s t r y / e x c e p t ( 2 / 3 )
t r y :
f = f i l e ( " / e t c / s h a d o w " , " r w + " )
e x c e p t I O E r r o r :
f = f i l e ( " ~ / m y s h a d o w " , " w " )
f i n a l l y :
f . c l o s e ( )
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 20/40
E x c e p c i o n e s t r y / e x c e p t ( 3 / 3 )
t r y :
s = s o c k e t ( A F _ I N T E R , S O C K _ S T R E A M )
d e s t = ( ' f w . i n f . u t f s m . c l ' , 9 9 9 9 )
s . c o n n e c t ( d e s t )
s . s e n d ( ' H o l a f w ' )
e x c e p t :
e r r o r _ l o g ( " E r r o r a l c r e a r e l s o c k e t " )
e l s e :
s . c l o s e ( )
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 21/40
L a p e r e z o s a p a l a b r a r e s e r v a d a p a s s
L a s e n t e n c i a p a s s n o h a c e n a d a
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 22/40
D e c l a r a c i ó n d e c l a s e s c o n c l a s s ( 1 / 4 )
c l a s s P e r s o n a :
d e f _ _ i n i t _ _ ( s e l f , n o m b r e , r u t , e d a d ) :
s e l f . n o m b r e = n o m b r e
s e l f . r u t = r u t
s e l f . e d a d = i n t ( e d a d )
s e l f . v i v o = T r u e
d e f k i l l ( s e l f ) :
s e l f . v i v o = F a l s e
d e f i s a l i v e ( s e l f ) :
r e t u r n s e l f . v i v o
r o b e r t o = P e r s o n a ( ' R o b e r t o ' , ' 1 5 2 7 4 0 0 0 − k ' , 2 3 )
i f r o b e r t o . i s a l i v e ( ) :
r o b e r t o . k i l l ( )
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 23/40
D e c l a r a c i ó n d e c l a s e s c o n c l a s s ( 2 / 4 )
i n s t a n c i a . m e t o d o ( a r g u m e n t o s ) e s e q u i v a l e n t e a
c l a s e . m e t o d o ( i n s t a n c i a , a r g u m e n t o s )
L a r m a d e l o s m é t o d o s i n c l u y e a l a i n s t a n c i a c o m o p r i m e r
p a r á m e t r o
E l c o n s t r u c t o r s i e m p r e s e l l a m a _ _ i n i t _ _
L a c l a s e e s u n o b j e t o p o r s í m i s m a , y s i r v e c o m o p l a n t i l l a p a r a
l a c r e a c i ó n d e n u e v a s i n s t a n c i a s .
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 24/40
D e c l a r a c i ó n d e c l a s e s c o n c l a s s ( 3 / 4 )
c l a s s I n t e g e r S t a c k ( l i s t ) :
d e f _ _ i n i t _ _ ( s e l f , ∗ a r g s ) :
l i s t . _ _ i n i t _ _ ( s e l f )
d e f i s e m p t y ( s e l f ) :
r e t u r n ( l e n ( s e l f ) = = 0 )
d e f p o p ( s e l f ) :
i f n o t s e l f . i s e m p t y ( ) :
r e t u r n l i s t . p o p ( s e l f )
r a i s e R u n t i m e E r r o r , " S t a c k i s e m p t y "
d e f p u s h ( s e l f , v a l u e ) :
i f n o t i s i n s t a n c e ( v a l u e , i n t ) :
r a i s e V a l u e E r r o r , \
" V a l u e m u s t b e i n t "
s e l f . a p p e n d ( v a l u e )
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 25/40
D e c l a r a c i ó n d e c l a s e s c o n c l a s s ( 4 / 4 )
c l a s s L i b r o :
p a s s
t a o c p = L i b r o ( )
t a o c p . t i t l e = " T h e A r t O f C o m p u t e r P r o g r a m m i n g "
t a o c p . v o l u m e = 1
t a o c p . a u t h o r = " D o n a l d K n u t h "
t a o c p . e d i t i o n = 2
t a o c p . y e a r = 1 9 7 3
t a o c p . i s b n = " 0 − 2 0 1 − 0 3 8 0 9 − 9 "
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 26/40
F u n c i o n e s g e n e r a d o r a s
d e f f i b o n a c c i ( n = 1 0 ) :
" G e n e r a t e s t h e f i r s t n F i b o n a c c i n u m b e r s "
c u r r e n t = 0
n e x t = 1
f o r i i n r a n g e ( n ) :
y i e l d c u r r e n t
c u r r e n t , n e x t = n e x t , n e x t + c u r r e n t
f o r i i n f i b o n a c c i ( ) :
p r i n t i
L a f u n c i ó n p r o d u c e u n a s e c u e n c i a , u n í t e m a l a v e z
y i e l d r e t o r n a u n v a l o r y s u s p e n d e l a e j e c u c i ó n
L a s i g u i e n t e v e z q u e l a f u n c i ó n e s l l a m a d a , s e r e t o m a l a
e j e c u c i ó n e n e l p u n t o e n q u e q u e d ó s u s p e n d i d a .
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 27/40
C o m p r e n s i o n e s d e l i s t a
C r e a c i ó n d e l i s t a s u s a n d o l a n o t a c i ó n m a t e m á t i c a p a r a l o s
c o n j u n t o s :
S = {x
2
: x ∈ R ∧ 3 | x }
S = [ x ∗ x f o r x i n r a n g e ( 1 0 ) i f x % 3 = = 0 ]
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 28/40
E x p r e s i o n e s g e n e r a d o r a s ( 1 / 2 )
G e n e r a l i z a c i ó n d e l a s c o m p r e n s i o n e s d e l i s t a
R e t o r n a n u n i t e r a b l e
f r o m m a t h i m p o r t s q r t
a = ( ( 1 , 3 ,
−1 , 4 ) ,
( − 2 , − 5 , 6 , 2 ) ,
( 0 , 8 , 2 , 3 ) )
n o r m _ f r o b e n i u s = s q r t ( s u m ( a b s ( x ) ∗∗ 2
f o r r o w i n a f o r x i n r o w ) )
n o r m _ i n f = m a x ( s u m ( r o w ) f o r r o w i n a )
n o r m _ 1 = m a x ( s u m ( c o l ) f o r c o l i n z i p ( ∗
a ) )
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 29/40
E x p r e s i o n e s g e n e r a d o r a s ( 2 / 2 )
v = ( 5 , 4 , 2 , − 1 , 3 )
w = ( − 2 , 1 , 5 , − 7 , 4 )
i n n e r _ p r o d u c t = s u m ( x
∗y
f o r ( x , y ) i n z i p ( v , w ) )
n o r m _ e u c l _ v = s q r t ( s u m ( x ∗∗ 2 f o r x i n v ) )
n o r m a l i z e d _ v = t u p l e ( x / n o r m _ e u c l _ v
f o r x i n v )
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 30/40
D e c o r a d o r e s d e f u n c i o n e s ( 1 / 4 )
M o t i v a c i ó n : w r a p p e r s d e f u n c i o n e s :
c l a s s A :
d e f b ( ) :
d o _ s o m e t h i n g ( )
b = s t a t i c m e t h o d ( b )
D e c o r a d o r d e l a f u n c i ó n a c t ú a c o m o w r a p p e r :
c l a s s A :
@ s t a t i c m e t h o d
d e f b ( ) :
d o _ s o m e t h i n g ( )
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 31/40
D e c o r a d o r e s d e f u n c i o n e s ( 2 / 4 )
m = M u t e x ( )
d e f l o c k i n g ( f ) :
d e f g ( ∗ a r g s ) :
m . P ( )
f ( ∗ a r g s )
m . V ( )
r e t u r n g
@ l o c k i n g
d e f t a k e _ f o r k ( n ) :
b l a b l a ( )
@ l o c k i n g
d e f s h a v e _ c u s t o m e r ( ) :
b l a b l a ( )
D e c o r a d o r e s d e f u n c i o n e s ( 3 / 4 )
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 32/40
d e f r e q u i r e _ n a t u r a l _ a r g u m e n t ( f ) :
d e f g ( x ) :
i f n o t i s i n s t a n c e ( x , i n t ) o r x < = 0 :
r a i s e V a l u e E r r o r , " a r g m u s t b e i n t "
r e t u r n f ( x )
r e t u r n g
@ r e q u i r e _ n a t u r a l _ a r g u m e n t
d e f i s p r i m e ( x ) :
r e t u r n n o t a n y ( d % x = = 0
f o r d i n r a n g e ( s q r t ( x ) ) )
@ r e q u i r e _ n a t u r a l _ a r g u m e n t
d e f h a r m o n i c ( n ) :
r e t u r n s u m ( 1 . / i f o r i i n r a n g e ( 1 , n ) )
D e c o r a d o r e s d e f u n c i o n e s ( 4 / 4 )
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 33/40
c l a s s M e m o i z e :
d e f _ _ i n i t _ _ ( s e l f , f ) :
s e l f . f = f
s e l f . m e m o = { }
d e f _ _ c a l l _ _ ( s e l f , ∗ a r g s ) :
i f n o t a r g s i n s e l f . m e m o :
s e l f . m e m o [ a r g s ] = s e l f . f (
∗a r g s )
r e t u r n s e l f . m e m o [ a r g s ]
@ M e m o i z e
d e f f i b o n a c c i ( n ) :
b l a h b l a h ( )
@ M e m o i z e
d e f p r i m e f a c t o r i z a t i o n ( n ) :
b l u h b l u h ( )
I m p o r t a c i ó n d e m ó d u l o s ( 1 / 2 )
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 34/40
i m p o r t m a t h
f o r f r e q i n r a n g e ( 1 0 ) :
t h e t a = 2 ∗ m a t h . p i ∗ f r e q
p r i n t m a t h . s i n ( t h e t a )
I m p o r t a c i ó n d e m ó d u l o s ( 2 / 2 )
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 35/40
f r o m r a n d o m i m p o r t c h o i c e , s h u f f l e
l e t t e r s = l i s t ( ' a b c d e f g h ' )
s h u f f l e ( l e t t e r s )
p r i n t " " . j o i n ( l e t t e r s )
t i r a d a s = [ ]
f o r i i n r a n g e ( 1 0 ) :
t i r a d a s . a p p e n d ( c h o i c e ( ( ' c a r a ' , ' s e l l o ' ) ) )
i f t i r a d a s . c o u n t ( ' c a r a ' ) < > 5 :
p r i n t " M o n e d a s o s p e c h o s a "
Z e n o f P y t h o n ( 1 / 2 )
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 36/40
B e a u t i f u l i s b e t t e r t h a n u g l y .
E x p l i c i t i s b e t t e r t h a n i m p l i c i t .
S i m p l e i s b e t t e r t h a n c o m p l e x .
C o m p l e x i s b e t t e r t h a n c o m p l i c a t e d .
F l a t i s b e t t e r t h a n n e s t e d .
S p a r s e i s b e t t e r t h a n d e n s e .
R e a d a b i l i t y c o u n t s .
S p e c i a l c a s e s a r e n ' t s p e c i a l e n o u g h t o b r e a k t h e r u l e s .
A l t h o u g h p r a c t i c a l i t y b e a t s p u r i t y .
E r r o r s s h o u l d n e v e r p a s s s i l e n t l y .
U n l e s s e x p l i c i t l y s i l e n c e d .
Z e n o f P y t h o n ( 2 / 2 )
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 37/40
I n t h e f a c e o f a m b i g u i t y , r e f u s e t h e t e m p t a t i o n t o g u e s s .
T h e r e s h o u l d b e o n e a n d p r e f e r a b l y o n l y o n e o b v i o u s w a y
t o d o i t .
A l t h o u g h t h a t w a y m a y n o t b e o b v i o u s a t r s t .
N o w i s b e t t e r t h a n n e v e r .
A l t h o u g h n e v e r i s o f t e n b e t t e r t h a n r i g h t n o w .
I f t h e i m p l e m e n t a t i o n i s h a r d t o e x p l a i n , i t ' s a b a d i d e a .
I f t h e i m p l e m e n t a t i o n i s e a s y t o e x p l a i n , i t m a y b e a g o o d i d e a .
N a m e s p a c e s a r e o n e h o n k i n g g r e a t i d e a l e t ' s d o m o r e o f
t h o s e !
O t r o s p r i n c i p i o s i m p o r t a n t e s
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 38/40
T O O W T D I
D R Y
E A F P
Y A G N I
U s e c a s e s r e q u i r e d
B a t t e r i e s i n c l u d e d
B a c k w a r d s c o m p a t i b i l i t y
P y 3 k
I m p l e m e n t a c i o n e s
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 39/40
C P y t h o n
J y t h o n
I r o n P y t h o n
P y P y
S t a c k l e s s
8/7/2019 Introducción conceptual y práctica al lenguaje de Programacion Python
http://slidepdf.com/reader/full/introduccion-conceptual-y-practica-al-lenguaje-de-programacion-python 40/40
The End