Upgrade to Pro — share decks privately, control downloads, hide ads and more …

python-intro

 python-intro

Eueung Mulyana

November 05, 2015
Tweet

More Decks by Eueung Mulyana

Other Decks in Programming

Transcript

  1. Python - An Introduction Eueung Mulyana | http://eueung.github.io/EL6240/py based on

    the material @Codecademy Attribution-ShareAlike CC BY-SA 1 / 121
  2. Agenda 1. Python Syntax 2. Strings and Console Output 3.

    Conditionals and Control Flow 4. Functions 5. Lists and Dictionaries 6. Lists and Functions 7. Loops 8. Advanced Topics in Python 9. Introduction to Classes 10. File Input and Output 2 / 121
  3. Agenda 1. Python Syntax 2. Strings and Console Output 3.

    Conditionals and Control Flow 4. Functions 5. Lists and Dictionaries 6. Lists and Functions 7. Loops 8. Advanced Topics in Python 9. Introduction to Classes 10. File Input and Output 3 / 121
  4. Python Syntax Basic Types: s t r i n g

    , i n t , f l o a t , b o o l Function: p r i n t (python 2) Comment: # p r i n t " W e l c o m e t o P y t h o n ! " m y _ v a r i a b l e = 1 0 m y _ i n t = 7 m y _ f l o a t = 1 . 2 3 m y _ b o o l = T r u e # c h a n g e v a l u e m y _ i n t = 7 m y _ i n t = 3 p r i n t m y _ i n t 4 / 121
  5. Python Syntax Note: I n d e n t a

    t i o n E r r o r : e x p e c t e d a n i n d e n t e d b l o c k # f i r s t d e f s p a m ( ) : e g g s = 1 2 r e t u r n e g g s p r i n t s p a m ( ) # s e c o n d d e f s p a m ( ) : e g g s = 1 2 r e t u r n e g g s p r i n t s p a m ( ) 5 / 121
  6. Python Syntax Comment: # , " " " Operators: +

    ,- ,* ,/ ,* * ,% # s i n g l e l i n e c o m m e n t " " " t e s t m u l t i l i n e s c o m m e n t " " " a d d i t i o n = 7 2 + 2 3 s u b t r a c t i o n = 1 0 8 - 2 0 4 m u l t i p l i c a t i o n = 1 0 8 * 0 . 5 d i v i s i o n = 1 0 8 / 9 # a d d i t i o n c o u n t _ t o = 5 0 0 0 + 6 0 0 0 . 6 p r i n t c o u n t _ t o # s q u a r e , e x p o n e n t i a t i o n , t o t h e p o w e r o f e g g s = 1 0 * * 2 p r i n t e g g s # m o d u l o s p a m = 5 % 4 p r i n t s p a m # 1 , m o d u l o 6 / 121
  7. Python Syntax Learned: Variables, which store values for later use

    Data types, such as numbers and booleans Whitespace, which separates statements Comments, which make your code easier to read Arithmetic operations, including +, -, , /, *, and % # c o m m e n t m o n t y = T r u e p y t h o n = 1 . 2 3 4 m o n t y _ p y t h o n = p y t h o n * * 2 7 / 121
  8. Python Syntax Tip Calculator m e a l = 4

    4 . 5 0 # 6 . 7 5 % t a x = 0 . 0 6 7 5 # 1 5 % t i p = 0 . 1 5 m e a l = m e a l + m e a l * t a x t o t a l = m e a l + m e a l * t i p p r i n t ( " % . 2 f " % t o t a l ) 8 / 121
  9. Agenda 1. Python Syntax 2. Strings and Console Output 3.

    Conditionals and Control Flow 4. Functions 5. Lists and Dictionaries 6. Lists and Functions 7. Loops 8. Advanced Topics in Python 9. Introduction to Classes 10. File Input and Output 9 / 121
  10. Strings and Console Output Assignment, Escaping Chars " M O

    N T Y " [ 4 ] c a e s a r = " G r a h a m " p r i n t c a e s a r # E s c a p i n g c h a r a c t e r s a b c = ' T h i s i s n \ ' t f l y i n g , t h i s i s f a l l i n g w i t h s t y l e ! ' p r i n t a b c " " " T h e s t r i n g " P Y T H O N " h a s s i x c h a r a c t e r s , n u m b e r e d 0 t o 5 , a s s h o w n b e l o w : + - - - + - - - + - - - + - - - + - - - + - - - + | P | Y | T | H | O | N | + - - - + - - - + - - - + - - - + - - - + - - - + 0 1 2 3 4 5 S o i f y o u w a n t e d " Y " , y o u c o u l d j u s t t y p e " P Y T H O N " [ 1 ] ( a l w a y s s t a r t c o u n t i n g f r o m 0 ! ) " " " f i f t h _ l e t t e r = " M O N T Y " [ 4 ] p r i n t f i f t h _ l e t t e r # Y 10 / 121
  11. Strings and Console Output Functions: l e n ( )

    , l o w e r ( ) , u p p e r ( ) , s t r ( ) p a r r o t = " N o r w e g i a n B l u e " p r i n t l e n ( p a r r o t ) # 1 4 # S t r i n g m e t h o d s : l e n ( ) , l o w e r ( ) , u p p e r ( ) , s t r ( ) p a r r o t = " N o r w e g i a n B l u e " p r i n t p a r r o t . l o w e r ( ) p r i n t p a r r o t . u p p e r ( ) # t o s t r i n g p i = 3 . 1 4 p r i n t s t r ( p i ) # r e v i e w m i n i s t r y = " T h e M i n i s t r y o f S i l l y W a l k s " p r i n t l e n ( m i n i s t r y ) p r i n t m i n i s t r y . u p p e r ( ) 11 / 121
  12. Strings and Console Output Printing String Variables String Concatenation Functions:

    r a w _ i n p u t ( ) # t o s t r i n g , p r i n t i n g v a r i a b l e s , s t r i n g c o n c a t e n a t i o n p r i n t " S p a m " + " a n d " + " e g g s " p r i n t " T h e v a l u e o f p i i s a r o u n d " + 3 . 1 4 # e r r o r p r i n t " T h e v a l u e o f p i i s a r o u n d " + s t r ( 3 . 1 4 ) s t r i n g _ 1 = " C a m e l o t " s t r i n g _ 2 = " p l a c e " p r i n t " L e t ' s n o t g o t o % s . ' T i s a s i l l y % s . " % ( s t r i n g _ 1 , s t r i n g _ 2 ) # s t r i n g f o r m a t t i n g n a m e = " M i k e " p r i n t " H e l l o % s " % ( n a m e ) # s t r i n g f o r m a t t i n g , t a n d a % s d a n % # f u n g s i r a w _ i n p u t ( i n p u t d a r i k e y b o a r d p a s r u n t i m e ) n a m e = r a w _ i n p u t ( " W h a t i s y o u r n a m e ? " ) q u e s t = r a w _ i n p u t ( " W h a t i s y o u r q u e s t ? " ) c o l o r = r a w _ i n p u t ( " W h a t i s y o u r f a v o r i t e c o l o r ? " ) p r i n t " A h , s o y o u r n a m e i s % s , y o u r q u e s t i s % s , " \ " a n d y o u r f a v o r i t e c o l o r i s % s . " % ( n a m e , q u e s t , c o l o r ) 12 / 121
  13. Strings and Console Output Date and Time f r o

    m d a t e t i m e i m p o r t d a t e t i m e n o w = d a t e t i m e . n o w ( ) p r i n t n o w # m e m b e r , b u k a n m e t h o d p r i n t n o w . y e a r p r i n t n o w . m o n t h p r i n t n o w . d a y p r i n t ' % s - % s - % s ' % ( n o w . y e a r , n o w . m o n t h , n o w . d a y ) p r i n t ' % s / % s / % s ' % ( n o w . d a y , n o w . m o n t h , n o w . y e a r ) p r i n t n o w . h o u r p r i n t n o w . m i n u t e p r i n t n o w . s e c o n d p r i n t ' % s : % s : % s ' % ( n o w . h o u r , n o w . m i n u t e , n o w . s e c o n d ) p r i n t ' % s / % s / % s % s : % s : % s ' % ( n o w . d a y , n o w . m o n t h , n o w . y e a r , n o w . h o u r , n o w . m i n u t e , n o w . s e c o n d ) 13 / 121
  14. Agenda 1. Python Syntax 2. Strings and Console Output 3.

    Conditionals and Control Flow 4. Functions 5. Lists and Dictionaries 6. Lists and Functions 7. Loops 8. Advanced Topics in Python 9. Introduction to Classes 10. File Input and Output 14 / 121
  15. Conditionals and Control Flow # r a w _ i

    n p u t d e f c l i n i c ( ) : p r i n t " Y o u ' v e j u s t e n t e r e d t h e c l i n i c ! " p r i n t " D o y o u t a k e t h e d o o r o n t h e l e f t o r t h e r i g h t ? " a n s w e r = r a w _ i n p u t ( " T y p e l e f t o r r i g h t a n d h i t ' E n t e r ' . " ) . l o w e r ( ) i f a n s w e r = = " l e f t " o r a n s w e r = = " l " : p r i n t " T h i s i s t h e V e r b a l A b u s e R o o m , y o u h e a p o f p a r r o t d r o p p i n g s ! " e l i f a n s w e r = = " r i g h t " o r a n s w e r = = " r " : p r i n t " O f c o u r s e t h i s i s t h e A r g u m e n t R o o m , I ' v e t o l d y o u t h a t a l r e a d y ! " e l s e : p r i n t " Y o u d i d n ' t p i c k l e f t o r r i g h t ! T r y a g a i n . " c l i n i c ( ) c l i n i c ( ) 15 / 121
  16. Conditionals and Control Flow Boolean Comparisons b o o l

    _ o n e = T r u e # 1 7 < 3 2 8 b o o l _ t w o = T r u e # 1 0 0 = = ( 2 * 5 0 ) b o o l _ t h r e e = T r u e # 1 9 < = 1 9 b o o l _ f o u r = F a l s e # - 2 2 > = - 1 8 b o o l _ f i v e = F a l s e # 9 9 ! = ( 9 8 + 1 ) b o o l _ o n e = F a l s e # ( 2 0 - 1 0 ) > 1 5 b o o l _ t w o = F a l s e # ( 1 0 + 1 7 ) = = 3 * * 1 6 b o o l _ t h r e e = F a l s e # 1 * * 2 < = - 1 b o o l _ f o u r = T r u e # 4 0 * 4 > = - 4 b o o l _ f i v e = F a l s e # 1 0 0 ! = 1 0 * * 2 # - - - - - b o o l _ o n e = 3 < 5 # T r u e b o o l _ t w o = 3 > 5 # F a l s e b o o l _ t h r e e = 5 = = 5 # T r u e b o o l _ f o u r = 3 ! = 3 # F a l s e b o o l _ f i v e = 3 < = 3 # T r u e 16 / 121
  17. Conditionals and Control Flow Operators: a n d , o

    r b o o l _ o n e = F a l s e a n d F a l s e b o o l _ t w o = - ( - ( - ( - 2 ) ) ) = = - 2 a n d 4 > = 1 6 * * 0 . 5 b o o l _ t h r e e = 1 9 % 4 ! = 3 0 0 / 1 0 / 1 0 a n d F a l s e b o o l _ f o u r = - ( 1 * * 2 ) < 2 * * 0 a n d 1 0 % 1 0 < = 2 0 - 1 0 * 2 b o o l _ f i v e = T r u e a n d T r u e # - - - - - b o o l _ o n e = 2 * * 3 = = 1 0 8 % 1 0 0 o r ' C l e e s e ' = = ' K i n g A r t h u r ' b o o l _ t w o = T r u e o r F a l s e b o o l _ t h r e e = 1 0 0 * * 0 . 5 > = 5 0 o r F a l s e b o o l _ f o u r = T r u e o r T r u e b o o l _ f i v e = 1 * * 1 0 0 = = 1 0 0 * * 1 o r 3 * 2 * 1 ! = 3 + 2 + 1 17 / 121
  18. Conditionals and Control Flow Operators: n o t b o

    o l _ o n e = n o t T r u e b o o l _ t w o = n o t 3 * * 4 < 4 * * 3 b o o l _ t h r e e = n o t 1 0 % 3 < = 1 0 % 2 b o o l _ f o u r = n o t 3 * * 2 + 4 * * 2 ! = 5 * * 2 b o o l _ f i v e = n o t n o t F a l s e # - - - - - b o o l _ o n e = F a l s e o r n o t T r u e a n d T r u e b o o l _ t w o = F a l s e a n d n o t T r u e o r T r u e b o o l _ t h r e e = T r u e a n d n o t ( F a l s e o r F a l s e ) b o o l _ f o u r = n o t n o t T r u e o r F a l s e a n d n o t T r u e b o o l _ f i v e = F a l s e o r n o t ( T r u e a n d T r u e ) # - - - - - # n o t f i r s t , b e r i k u t n y a a n d , o r - - s a m a p r i o r i t a s , k l n o t n o t s a m a d e n g a n n o t ( n o t ) b o o l _ o n e = ( 2 < = 2 ) a n d " A l p h a " = = " B r a v o " 18 / 121
  19. Conditionals and Control Flow Conditional Statement a n s w

    e r = " L e f t " i f a n s w e r = = " L e f t " : p r i n t " T h i s i s t h e V e r b a l A b u s e R o o m , y o u h e a p o f p a r r o t d r o p p i n g s ! " # W i l l t h e a b o v e p r i n t s t a t e m e n t p r i n t t o t h e c o n s o l e ? # - - - d e f u s i n g _ c o n t r o l _ o n c e ( ) : i f T r u e : r e t u r n " S u c c e s s # 1 " d e f u s i n g _ c o n t r o l _ a g a i n ( ) : i f T r u e : r e t u r n " S u c c e s s # 2 " p r i n t u s i n g _ c o n t r o l _ o n c e ( ) p r i n t u s i n g _ c o n t r o l _ a g a i n ( ) 19 / 121
  20. Conditionals and Control Flow Conditional Statement # c o n

    d i t i o n a n s w e r = " ' T i s b u t a s c r a t c h ! " d e f b l a c k _ k n i g h t ( ) : i f a n s w e r = = " ' T i s b u t a s c r a t c h ! " : r e t u r n T r u e e l s e : r e t u r n F a l s e # M a k e s u r e t h i s r e t u r n s F a l s e d e f f r e n c h _ s o l d i e r ( ) : i f a n s w e r = = " G o a w a y , o r I s h a l l t a u n t y o u a s e c o n d t i m e ! " : r e t u r n T r u e e l s e : r e t u r n F a l s e # M a k e s u r e t h i s r e t u r n s F a l s e 20 / 121
  21. Conditionals and Control Flow Conditional Statement # c o n

    d i t i o n d e f g r e a t e r _ l e s s _ e q u a l _ 5 ( a n s w e r ) : i f a n s w e r > 5 : r e t u r n 1 e l i f a n s w e r < 5 : r e t u r n - 1 e l s e : r e t u r n 0 p r i n t g r e a t e r _ l e s s _ e q u a l _ 5 ( 4 ) p r i n t g r e a t e r _ l e s s _ e q u a l _ 5 ( 5 ) p r i n t g r e a t e r _ l e s s _ e q u a l _ 5 ( 6 ) # r e v i e w d e f t h e _ f l y i n g _ c i r c u s ( ) : i f 5 = = 5 : # S t a r t c o d i n g h e r e ! # D o n ' t f o r g e t t o i n d e n t # t h e c o d e i n s i d e t h i s b l o c k ! r e t u r n T r u e e l i f T r u e a n d T r u e : # K e e p g o i n g h e r e . # Y o u ' l l w a n t t o a d d t h e e l s e s t a t e m e n t , t o o ! r e t u r n F a l s e e l s e : r e t u r n F a l s e 21 / 121
  22. Conditionals and Control Flow PygLatin (Python Pig Latin) Ask the

    user to input a word in English. Make sure the user entered a valid word. Convert the word from English to Pig Latin. Display the translation result. p r i n t ' W e l c o m e t o t h e P i g L a t i n T r a n s l a t o r ! ' o r i g i n a l = r a w _ i n p u t ( " E n t e r a w o r d : " ) i f l e n ( o r i g i n a l ) > 0 a n d o r i g i n a l . i s a l p h a ( ) : p r i n t o r i g i n a l e l s e : p r i n t " e m p t y " 22 / 121
  23. Conditionals and Control Flow PygLatin (Python Pig Latin) You move

    the first letter of the word to the end and then append the suffix 'ay'. Example: p y t h o n - > y t h o n p a y p y g = ' a y ' o r i g i n a l = r a w _ i n p u t ( ' E n t e r a w o r d : ' ) i f l e n ( o r i g i n a l ) > 0 a n d o r i g i n a l . i s a l p h a ( ) : w o r d = o r i g i n a l . l o w e r ( ) f i r s t = w o r d [ 0 ] n e w _ w o r d = w o r d + f i r s t + p y g n e w _ w o r d = n e w _ w o r d [ 1 : l e n ( n e w _ w o r d ) ] p r i n t n e w _ w o r d e l s e : p r i n t ' e m p t y ' 23 / 121
  24. Agenda 1. Python Syntax 2. Strings and Console Output 3.

    Conditionals and Control Flow 4. Functions 5. Lists and Dictionaries 6. Lists and Functions 7. Loops 8. Advanced Topics in Python 9. Introduction to Classes 10. File Input and Output 24 / 121
  25. Functions d e f t a x ( b i

    l l ) : " " " A d d s 8 % t a x t o a r e s t a u r a n t b i l l . " " " b i l l * = 1 . 0 8 p r i n t " W i t h t a x : % f " % b i l l r e t u r n b i l l d e f t i p ( b i l l ) : " " " A d d s 1 5 % t i p t o a r e s t a u r a n t b i l l . " " " b i l l * = 1 . 1 5 p r i n t " W i t h t i p : % f " % b i l l r e t u r n b i l l m e a l _ c o s t = 1 0 0 m e a l _ w i t h _ t a x = t a x ( m e a l _ c o s t ) m e a l _ w i t h _ t i p = t i p ( m e a l _ w i t h _ t a x ) 25 / 121
  26. Functions d e f s q u a r e

    ( n ) : s q u a r e d = n * * 2 p r i n t " % d s q u a r e d i s % d . " % ( n , s q u a r e d ) r e t u r n s q u a r e d s q u a r e ( 1 0 ) # - - - d e f p o w e r ( b a s e , e x p o n e n t ) : # A d d y o u r p a r a m e t e r s h e r e ! r e s u l t = b a s e * * e x p o n e n t p r i n t " % d t o t h e p o w e r o f % d i s % d . " % ( b a s e , e x p o n e n t , r e s u l t ) p o w e r ( 3 7 , 4 ) # A d d y o u r a r g u m e n t s h e r e ! 26 / 121
  27. Functions # F u n c t i o n

    s c a l l f u n c t i o n s d e f o n e _ g o o d _ t u r n ( n ) : r e t u r n n + 1 d e f d e s e r v e s _ a n o t h e r ( n ) : r e t u r n o n e _ g o o d _ t u r n ( n ) + 2 # - - - d e f s h o u t ( p h r a s e ) : i f p h r a s e = = p h r a s e . u p p e r ( ) : r e t u r n " Y O U ' R E S H O U T I N G ! " e l s e : r e t u r n " C a n y o u s p e a k u p ? " s h o u t ( " I ' M I N T E R E S T E D I N S H O U T I N G " ) # - - - d e f c u b e ( n u m b e r ) : r e t u r n n u m b e r * * 3 d e f b y _ t h r e e ( n u m b e r ) : i f n u m b e r % 3 = = 0 : r e t u r n c u b e ( n u m b e r ) e l s e : r e t u r n F a l s e 27 / 121
  28. Functions Importing a Module: a module is a file that

    contains definitions— including variables and functions—that you can use once it is imported Math Standard Library # 1 i m p o r t m a t h p r i n t m a t h . s q r t ( 2 5 ) # 2 f r o m m a t h i m p o r t s q r t p r i n t s q r t ( 2 5 ) f r o m m a t h i m p o r t * p r i n t s q r t ( 2 5 ) # 3 i m p o r t m a t h e v e r y t h i n g = d i r ( m a t h ) # S e t s e v e r y t h i n g t o a l i s t o f t h i n g s f r o m m a t h p r i n t e v e r y t h i n g # P r i n t s ' e m a l l ! " " " [ ' _ _ d o c _ _ ' , ' _ _ n a m e _ _ ' , ' _ _ p a c k a g e _ _ ' , ' a c o s ' , ' a c o s h ' , ' a s i n ' , ' a s i n h ' , ' a t a n ' , ' a t a n 2 ' , ' a t a n h ' , ' c e i l ' , ' c o p y s i g n ' , " " " 28 / 121
  29. Functions Math * a r g s : one or

    more arguments Functions: m a x , m i n , a b s d e f b i g g e s t _ n u m b e r ( * a r g s ) : p r i n t m a x ( a r g s ) r e t u r n m a x ( a r g s ) d e f s m a l l e s t _ n u m b e r ( * a r g s ) : p r i n t m i n ( a r g s ) r e t u r n m i n ( a r g s ) d e f d i s t a n c e _ f r o m _ z e r o ( a r g ) : p r i n t a b s ( a r g ) r e t u r n a b s ( a r g ) b i g g e s t _ n u m b e r ( - 1 0 , - 5 , 5 , 1 0 ) s m a l l e s t _ n u m b e r ( - 1 0 , - 5 , 5 , 1 0 ) d i s t a n c e _ f r o m _ z e r o ( - 1 0 ) # - - - m a x i m u m = m a x ( 2 . 3 , 4 . 1 , 6 ) m i n i m u m = m i n ( 2 . 3 , 4 . 1 , 6 ) a b s o l u t e = a b s ( - 4 2 ) p r i n t m a x i m u m 29 / 121
  30. Functions Function: t y p e p r i n

    t t y p e ( 4 2 ) # < t y p e ' i n t ' > p r i n t t y p e ( 4 . 2 ) # < t y p e ' f l o a t ' > p r i n t t y p e ( ' s p a m ' ) # < t y p e ' s t r ' > # - - - d e f s p e a k ( m e s s a g e ) : r e t u r n m e s s a g e i f h a p p y ( ) : s p e a k ( " I ' m h a p p y ! " ) e l i f s a d ( ) : s p e a k ( " I ' m s a d . " ) e l s e : s p e a k ( " I d o n ' t k n o w w h a t I ' m f e e l i n g . " ) # - - - d e f s h u t _ d o w n ( s ) : i f s = = " y e s " : r e t u r n " S h u t t i n g d o w n " e l i f s = = " n o " : r e t u r n " S h u t d o w n a b o r t e d " e l s e : r e t u r n " S o r r y " 30 / 121
  31. Functions d e f i s _ n u m

    e r i c ( n u m ) : r e t u r n t y p e ( n u m ) = = i n t o r t y p e ( n u m ) = = f l o a t : # - - - d e f d i s t a n c e _ f r o m _ z e r o ( a ) : i f t y p e ( a ) = = i n t o r t y p e ( a ) = = f l o a t : r e t u r n a b s ( a ) e l s e : r e t u r n " N o p e " 31 / 121
  32. Functions Taking a Vacation d e f h o t

    e l _ c o s t ( n i g h t s ) : r e t u r n 1 4 0 * n i g h t s d e f p l a n e _ r i d e _ c o s t ( c i t y ) : i f c i t y = = " C h a r l o t t e " : r e t u r n 1 8 3 e l i f c i t y = = " T a m p a " : r e t u r n 2 2 0 e l i f c i t y = = " P i t t s b u r g h " : r e t u r n 2 2 2 e l i f c i t y = = " L o s A n g e l e s " : r e t u r n 4 7 5 d e f r e n t a l _ c a r _ c o s t ( d a y s ) : c o s t p e r d a y = 4 0 i f d a y s > = 7 : t o t a l = d a y s * c o s t p e r d a y - 5 0 e l i f d a y s > = 3 : t o t a l = d a y s * c o s t p e r d a y - 2 0 e l s e : t o t a l = d a y s * c o s t p e r d a y r e t u r n t o t a l d e f t r i p _ c o s t ( c i t y , d a y s , s p e n d i n g _ m o n e y ) : r e t u r n r e n t a l _ c a r _ c o s t ( d a y s ) + h o t e l _ c o s t ( d a y s ) + p l a n e _ r i d e _ c o s t ( c i t y ) + s p e n d i n g _ m o n e y p r i n t t r i p _ c o s t ( " L o s A n g e l e s " , 5 , 6 0 0 ) 32 / 121
  33. Agenda 1. Python Syntax 2. Strings and Console Output 3.

    Conditionals and Control Flow 4. Functions 5. Lists and Dictionaries 6. Lists and Functions 7. Loops 8. Advanced Topics in Python 9. Introduction to Classes 10. File Input and Output 33 / 121
  34. Lists and Dictionaries List : Array Dictionary : Asc. Array

    (notation {} -> object in JS) List # l i s t _ n a m e = [ i t e m _ 1 , i t e m _ 2 ] , e m p t y [ ] z o o _ a n i m a l s = [ " p a n g o l i n " , " c a s s o w a r y " , " s l o t h " , " g a j a h " ] ; i f l e n ( z o o _ a n i m a l s ) > 3 : p r i n t " T h e f i r s t a n i m a l a t t h e z o o i s t h e " + z o o _ a n i m a l s [ 0 ] p r i n t " T h e s e c o n d a n i m a l a t t h e z o o i s t h e " + z o o _ a n i m a l s [ 1 ] p r i n t " T h e t h i r d a n i m a l a t t h e z o o i s t h e " + z o o _ a n i m a l s [ 2 ] p r i n t " T h e f o u r t h a n i m a l a t t h e z o o i s t h e " + z o o _ a n i m a l s [ 3 ] 34 / 121
  35. Lists and Dictionaries List Methods: a p p e n

    d , l e n s u i t c a s e = [ ] s u i t c a s e . a p p e n d ( " s u n g l a s s e s " ) # Y o u r c o d e h e r e ! s u i t c a s e . a p p e n d ( ' a ' ) s u i t c a s e . a p p e n d ( ' b ' ) s u i t c a s e . a p p e n d ( ' c ' ) l i s t _ l e n g t h = l e n ( s u i t c a s e ) # S e t t h i s t o t h e l e n g t h o f s u i t c a s e p r i n t " T h e r e a r e % d i t e m s i n t h e s u i t c a s e . " % ( l i s t _ l e n g t h ) p r i n t s u i t c a s e " " " T h e r e a r e 4 i t e m s i n t h e s u i t c a s e . [ ' s u n g l a s s e s ' , ' a ' , ' b ' , ' c ' ] " " " 35 / 121
  36. Lists and Dictionaries List Slicing: [ i n d e

    x _ i n c l u s i v e : i n d e x _ e x c l u s i v e ] s u i t c a s e = [ " s u n g l a s s e s " , " h a t " , " p a s s p o r t " , " l a p t o p " , " s u i t " , " s h o e s " ] f i r s t = s u i t c a s e [ 0 : 2 ] # T h e f i r s t a n d s e c o n d i t e m s ( i n d e x z e r o a n d o n e ) m i d d l e = s u i t c a s e [ 2 : 4 ] # T h i r d a n d f o u r t h i t e m s ( i n d e x t w o a n d t h r e e ) l a s t = s u i t c a s e [ 4 : 6 ] # T h e l a s t t w o i t e m s ( i n d e x f o u r a n d f i v e ) # < 6 b u k a n < = 6 # W e s t a r t a t t h e i n d e x b e f o r e t h e c o l o n a n d c o n t i n u e u p t o b u t n o t i n c l u d i n g t h e i n d e x a f t e r t h e c o l o n . # - - - a n i m a l s = " c a t d o g f r o g " c a t = a n i m a l s [ : 3 ] # T h e f i r s t t h r e e c h a r a c t e r s o f a n i m a l s d o g = a n i m a l s [ 3 : 6 ] # T h e f o u r t h t h r o u g h s i x t h c h a r a c t e r s f r o g = a n i m a l s [ 6 : ] # F r o m t h e s e v e n t h c h a r a c t e r t o t h e e n d # t a n p a s t a r t / e n d 36 / 121
  37. Lists and Dictionaries List Methods: i n d e x

    , i n s e r t a n i m a l s = [ " a a r d v a r k " , " b a d g e r " , " d u c k " , " e m u " , " f e n n e c f o x " ] d u c k _ i n d e x = a n i m a l s . i n d e x ( " d u c k " ) # U s e i n d e x ( ) t o f i n d " d u c k " # Y o u r c o d e h e r e ! a n i m a l s . i n s e r t ( d u c k _ i n d e x , " c o b r a " ) p r i n t a n i m a l s # O b s e r v e w h a t p r i n t s a f t e r t h e i n s e r t o p e r a t i o n # [ ' a a r d v a r k ' , ' b a d g e r ' , ' c o b r a ' , ' d u c k ' , ' e m u ' , ' f e n n e c f o x ' ] 37 / 121
  38. Lists and Dictionaries List f o r e l e

    m e n t i n t h e _ l i s t . s o r t ( ) # 1 m y _ l i s t = [ 1 , 9 , 3 , 8 , 5 , 7 ] f o r n u m b e r i n m y _ l i s t : p r i n t 2 * n u m b e r # 2 # . s o r t s t a r t _ l i s t = [ 5 , 3 , 1 , 2 , 4 ] s q u a r e _ l i s t = [ ] f o r n u m i n s t a r t _ l i s t : s q u a r e _ l i s t . a p p e n d ( n u m * * 2 ) s q u a r e _ l i s t . s o r t ( ) p r i n t s q u a r e _ l i s t 38 / 121
  39. Lists and Dictionaries Dict Key : Value Pair # d

    i c t i o n a r y k e y v a l u e n o t a s i o b j e c t { } r e s i d e n t s = { ' P u f f i n ' : 1 0 4 , ' S l o t h ' : 1 0 5 , ' B u r m e s e P y t h o n ' : 1 0 6 } p r i n t r e s i d e n t s [ ' P u f f i n ' ] # P r i n t s P u f f i n ' s r o o m n u m b e r # Y o u r c o d e h e r e ! p r i n t r e s i d e n t s [ ' S l o t h ' ] p r i n t r e s i d e n t s [ ' B u r m e s e P y t h o n ' ] 39 / 121
  40. Lists and Dictionaries Dict # - - - m e

    n u = { } # E m p t y d i c t i o n a r y m e n u [ ' C h i c k e n A l f r e d o ' ] = 1 4 . 5 0 # A d d i n g n e w k e y - v a l u e p a i r p r i n t m e n u [ ' C h i c k e n A l f r e d o ' ] # a p p e n d m e n u [ ' k a r e d o k ' ] = 2 . 2 m e n u [ ' l o t e k ' ] = 2 . 3 m e n u [ ' r u j a k ' ] = 3 . 3 p r i n t " T h e r e a r e " + s t r ( l e n ( m e n u ) ) + " i t e m s o n t h e m e n u . " p r i n t m e n u " " " 1 4 . 5 T h e r e a r e 4 i t e m s o n t h e m e n u . { ' l o t e k ' : 2 . 3 , ' C h i c k e n A l f r e d o ' : 1 4 . 5 , ' k a r e d o k ' : 2 . 2 , ' r u j a k ' : 3 . 3 } " " " 40 / 121
  41. Lists and Dictionaries Dict A dictionary (or list) declaration may

    break across multiple lines d e l # k e y - a n i m a l _ n a m e : v a l u e - l o c a t i o n z o o _ a n i m a l s = { ' U n i c o r n ' : ' C o t t o n C a n d y H o u s e ' , ' S l o t h ' : ' R a i n f o r e s t E x h i b i t ' , ' B e n g a l T i g e r ' : ' J u n g l e H o u s e ' , ' A t l a n t i c P u f f i n ' : ' A r c t i c E x h i b i t ' , ' R o c k h o p p e r P e n g u i n ' : ' A r c t i c E x h i b i t ' } # R e m o v i n g t h e ' U n i c o r n ' e n t r y . ( U n i c o r n s a r e i n c r e d i b l y e x p e n s i v e . ) d e l z o o _ a n i m a l s [ ' U n i c o r n ' ] d e l z o o _ a n i m a l s [ ' S l o t h ' ] d e l z o o _ a n i m a l s [ ' B e n g a l T i g e r ' ] z o o _ a n i m a l s [ ' R o c k h o p p e r P e n g u i n ' ] = ' A r c t i c E x h i b i t M o d ' p r i n t z o o _ a n i m a l s 41 / 121
  42. Lists and Dictionaries List & Dict r e m o

    v e Complex Dict # r e m o v e b a c k p a c k = [ ' x y l o p h o n e ' , ' d a g g e r ' , ' t e n t ' , ' b r e a d l o a f ' ] b a c k p a c k . r e m o v e ( " d a g g e r " ) # - - - i n v e n t o r y = { ' g o l d ' : 5 0 0 , ' p o u c h ' : [ ' f l i n t ' , ' t w i n e ' , ' g e m s t o n e ' ] , # A s s i g n e d a n e w l i s t t o ' p o u c h ' k e y ' b a c k p a c k ' : [ ' x y l o p h o n e ' , ' d a g g e r ' , ' b e d r o l l ' , ' b r e a d l o a f ' ] } # A d d i n g a k e y ' b u r l a p b a g ' a n d a s s i g n i n g a l i s t t o i t i n v e n t o r y [ ' b u r l a p b a g ' ] = [ ' a p p l e ' , ' s m a l l r u b y ' , ' t h r e e - t o e d s l o t h ' ] # S o r t i n g t h e l i s t f o u n d u n d e r t h e k e y ' p o u c h ' i n v e n t o r y [ ' p o u c h ' ] . s o r t ( ) i n v e n t o r y [ ' p o c k e t ' ] = [ ' s e a s h e l l ' , ' s t r a n g e b e r r y ' , ' l i n t ' ] i n v e n t o r y [ ' b a c k p a c k ' ] . s o r t ( ) i n v e n t o r y [ ' b a c k p a c k ' ] . r e m o v e ( ' d a g g e r ' ) i n v e n t o r y [ ' g o l d ' ] + = 5 0 42 / 121
  43. Lists and Dictionaries A Day at the Supermarket n a

    m e s = [ " A d a m " , " A l e x " , " M a r i a h " , " M a r t i n e " , " C o l u m b u s " ] f o r i t e m i n n a m e s : p r i n t i t e m # - - - # i n d e n t f i r s t e l e m e n t d a l a m d i c t d a l a m { } , s p t d i b h s l a i n w e b s t e r = { " A a r d v a r k " : " A s t a r o f a p o p u l a r c h i l d r e n ' s c a r t o o n s h o w . " , " B a a " : " T h e s o u n d a g o a t m a k e s . " , " C a r p e t " : " G o e s o n t h e f l o o r . " , " D a b " : " A s m a l l a m o u n t . " } f o r k e y i n w e b s t e r : p r i n t w e b s t e r [ k e y ] Note that dictionaries are unordered, meaning that any time you loop through a dictionary, you will go through every key, but you are not guaranteed to get them in any particular order. 43 / 121
  44. Lists and Dictionaries A Day at the Supermarket a =

    [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 1 0 , 1 1 , 1 2 , 1 3 ] f o r n u m b e r i n a : i f n u m b e r % 2 = = 0 : p r i n t n u m b e r # - - - d e f c o u n t _ s m a l l ( n u m b e r s ) : t o t a l = 0 f o r n i n n u m b e r s : i f n < 1 0 : t o t a l = t o t a l + 1 r e t u r n t o t a l l o s t = [ 4 , 8 , 1 5 , 1 6 , 2 3 , 4 2 ] s m a l l = c o u n t _ s m a l l ( l o s t ) p r i n t s m a l l # - - - d e f f i z z _ c o u n t ( x ) : c o u n t = 0 f o r i t e m i n x : i f i t e m = = ' f i z z ' : c o u n t + = 1 r e t u r n c o u n t f i z z _ c o u n t ( [ " f i z z " , " c a t " , " f i z z " ] ) 44 / 121
  45. Lists and Dictionaries A Day at the Supermarket As we've

    mentioned, strings are like lists with characters as elements. You can loop through strings the same way you loop through lists! f o r l e t t e r i n " C o d e c a d e m y " : p r i n t l e t t e r # E m p t y l i n e s t o m a k e t h e o u t p u t p r e t t y p r i n t p r i n t w o r d = " P r o g r a m m i n g i s f u n ! " f o r l e t t e r i n w o r d : # O n l y p r i n t o u t t h e l e t t e r i i f l e t t e r = = " i " : p r i n t l e t t e r 45 / 121
  46. Lists and Dictionaries A Day at the Supermarket p r

    i c e s = { " b a n a n a " : 4 , " a p p l e " : 2 , " o r a n g e " : 1 . 5 , " p e a r " : 3 } s t o c k = { " b a n a n a " : 6 , " a p p l e " : 0 , " o r a n g e " : 3 2 , " p e a r " : 1 5 } f o r k e y i n s t o c k : p r i n t k e y p r i n t " p r i c e : % s " % p r i c e s [ k e y ] p r i n t " s t o c k : % s " % s t o c k [ k e y ] 46 / 121
  47. Lists and Dictionaries A Day at the Supermarket # t

    a k e 2 p r i c e s = { " b a n a n a " : 4 , " a p p l e " : 2 , " o r a n g e " : 1 . 5 , " p e a r " : 3 , } s t o c k = { " b a n a n a " : 6 , " a p p l e " : 0 , " o r a n g e " : 3 2 , " p e a r " : 1 5 , } f o r k e y i n p r i c e s : p r i n t k e y p r i n t " p r i c e : % s " % p r i c e s [ k e y ] p r i n t " s t o c k : % s " % s t o c k [ k e y ] t o t a l = 0 f o r k e y i n p r i c e s : p r i n t p r i c e s [ k e y ] * s t o c k [ k e y ] t o t a l + = p r i c e s [ k e y ] * s t o c k [ k e y ] p r i n t t o t a l 47 / 121
  48. Lists and Dictionaries A Day at the Supermarket # t

    a k e 3 s h o p p i n g _ l i s t = [ " b a n a n a " , " o r a n g e " , " a p p l e " ] s t o c k = { " b a n a n a " : 6 , " a p p l e " : 0 , " o r a n g e " : 3 2 , " p e a r " : 1 5 } p r i c e s = { " b a n a n a " : 4 , " a p p l e " : 2 , " o r a n g e " : 1 . 5 , " p e a r " : 3 } d e f c o m p u t e _ b i l l ( f o o d ) : t o t a l = 0 f o r i t e m i n f o o d : i f s t o c k [ i t e m ] > 0 : t o t a l + = p r i c e s [ i t e m ] s t o c k [ i t e m ] - = 1 r e t u r n t o t a l 48 / 121
  49. Student Becomes the Teacher List of Dicts l l o

    y d = { " n a m e " : " L l o y d " , " h o m e w o r k " : [ 9 0 . 0 , 9 7 . 0 , 7 5 . 0 , 9 2 . 0 ] , " q u i z z e s " : [ 8 8 . 0 , 4 0 . 0 , 9 4 . 0 ] , " t e s t s " : [ 7 5 . 0 , 9 0 . 0 ] } a l i c e = { " n a m e " : " A l i c e " , " h o m e w o r k " : [ 1 0 0 . 0 , 9 2 . 0 , 9 8 . 0 , 1 0 0 . 0 ] , " q u i z z e s " : [ 8 2 . 0 , 8 3 . 0 , 9 1 . 0 ] , " t e s t s " : [ 8 9 . 0 , 9 7 . 0 ] } t y l e r = { " n a m e " : " T y l e r " , " h o m e w o r k " : [ 0 . 0 , 8 7 . 0 , 7 5 . 0 , 2 2 . 0 ] , " q u i z z e s " : [ 0 . 0 , 7 5 . 0 , 7 8 . 0 ] , " t e s t s " : [ 1 0 0 . 0 , 1 0 0 . 0 ] } s t u d e n t s = [ l l o y d , a l i c e , t y l e r ] f o r s t u d e n t i n s t u d e n t s : p r i n t s t u d e n t [ ' n a m e ' ] p r i n t s t u d e n t [ ' h o m e w o r k ' ] p r i n t s t u d e n t [ ' q u i z z e s ' ] p r i n t s t u d e n t [ ' t e s t s ' ] 49 / 121
  50. Student Becomes the Teacher Arithmetics Notes 5 / 2 #

    2 5 . 0 / 2 # 2 . 5 f l o a t ( 5 ) / 2 # 2 . 5 b i a r j a d i f l o a t k a r e n a i n t / i n t 50 / 121
  51. Student Becomes the Teacher l l o y d =

    { " n a m e " : " L l o y d " , " h o m e w o r k " : [ 9 0 . 0 , 9 7 . 0 , 7 5 . 0 , 9 2 . 0 ] , " q u i z z e s " : [ 8 8 . 0 , 4 0 . 0 , 9 4 . 0 ] , " t e s t s " : [ 7 5 . 0 , 9 0 . 0 ] } a l i c e = { " n a m e " : " A l i c e " , " h o m e w o r k " : [ 1 0 0 . 0 , 9 2 . 0 , 9 8 . 0 , 1 0 0 . 0 ] , " q u i z z e s " : [ 8 2 . 0 , 8 3 . 0 , 9 1 . 0 ] , " t e s t s " : [ 8 9 . 0 , 9 7 . 0 ] } t y l e r = { " n a m e " : " T y l e r " , " h o m e w o r k " : [ 0 . 0 , 8 7 . 0 , 7 5 . 0 , 2 2 . 0 ] , " q u i z z e s " : [ 0 . 0 , 7 5 . 0 , 7 8 . 0 ] , " t e s t s " : [ 1 0 0 . 0 , 1 0 0 . 0 ] } # - - - d e f a v e r a g e ( n u m b e r s ) : t o t a l = s u m ( n u m b e r s ) t o t a l = f l o a t ( t o t a l ) r e t u r n t o t a l / l e n ( n u m b e r s ) d e f g e t _ a v e r a g e ( s t u d e n t ) : h o m e w o r k = a v e r a g e ( s t u d e n t [ ' h o m e w o r k ' ] ) q u i z z e s = a v e r a g e ( s t u d e n t [ ' q u i z z e s ' ] ) t e s t s = a v e r a g e ( s t u d e n t [ ' t e s t s ' ] ) r e t u r n 0 . 1 * h o m e w o r k + 0 . 3 * q u i z z e s + 0 . 6 * t e s t s d e f g e t _ l e t t e r _ g r a d e ( s c o r e ) : i f s c o r e > = 9 0 : r e t u r n " A " e l i f s c o r e > = 8 0 : r e t u r n " B " e l i f s c o r e > = 7 0 : r e t u r n " C " e l i f s c o r e > = 6 0 : r e t u r n " D " e l s e : r e t u r n " F " d e f g e t _ c l a s s _ a v e r a g e ( s t u d e n t s ) : r e s u l t s = [ ] f o r s t u d e n t i n s t u d e n t s : r e s u l t s . a p p e n d ( g e t _ a v e r a g e ( s t u d e n t ) ) r e t u r n a v e r a g e ( r e s u l t s ) 51 / 121
  52. Student Becomes the Teacher a v e r a g

    e ( A - L i s t ) returns f l o a t g e t _ a v e r a g e ( A - D i c t ) returns f l o a t --> better g e t _ n u m e r i c _ g r a d e ( ) g e t _ l e t t e r _ g r a d e ( n u m ) returns s t r i n g g e t _ c l a s s _ a v e r a g e ( L i s t - o f - D i c t s ) returns f l o a t # - - - s t u d e n t s = [ l l o y d , a l i c e , t y l e r ] p r i n t g e t _ c l a s s _ a v e r a g e ( s t u d e n t s ) p r i n t g e t _ l e t t e r _ g r a d e ( g e t _ c l a s s _ a v e r a g e ( s t u d e n t s ) ) # - - - # c l a s s a v e r a g e = g e t _ c l a s s _ a v e r a g e ( [ l l o y d , a l i c e , t y l e r ] ) # p r i n t g e t _ l e t t e r _ g r a d e ( g e t _ a v e r a g e ( l l o y d ) ) # - - - 52 / 121
  53. Agenda 1. Python Syntax 2. Strings and Console Output 3.

    Conditionals and Control Flow 4. Functions 5. Lists and Dictionaries 6. Lists and Functions 7. Loops 8. Advanced Topics in Python 9. Introduction to Classes 10. File Input and Output 53 / 121
  54. Lists and Functions Access to List Method: . a p

    p e n d ( ) , . p o p ( ) , . r e m o v e ( ) Function: d e l n = [ 1 , 3 , 5 ] # D o y o u r m u l t i p l i c a t i o n h e r e n [ 1 ] * = 5 n . a p p e n d ( 4 ) p r i n t n # [ 1 , 1 5 , 5 , 4 ] n = [ 1 , 3 , 5 ] # R e m o v e t h e f i r s t i t e m i n t h e l i s t h e r e n . p o p ( 0 ) # a t a u d e l ( n [ 0 ] ) # n . r e m o v e ( 1 ) v a l u e 1 , b u k a n i n d e x # p o p n g e - r e t u r n v a l u e , d e l v o i d p r i n t n 54 / 121
  55. Lists and Functions # 1 n = " H e

    l l o " d e f s t r i n g _ f u n c t i o n ( s ) : r e t u r n s + ' w o r l d ' p r i n t s t r i n g _ f u n c t i o n ( n ) # 2 d e f l i s t _ f u n c t i o n ( x ) : x [ 1 ] + = 3 r e t u r n x n = [ 3 , 5 , 7 ] p r i n t l i s t _ f u n c t i o n ( n ) # 3 n = [ 3 , 5 , 7 ] d e f l i s t _ e x t e n d e r ( l s t ) : l s t . a p p e n d ( 9 ) ; r e t u r n l s t p r i n t l i s t _ e x t e n d e r ( n ) 55 / 121
  56. Lists and Functions # 1 r a n g e

    n = [ 3 , 5 , 7 ] d e f p r i n t _ l i s t ( x ) : f o r i i n r a n g e ( 0 , l e n ( x ) ) : p r i n t x [ i ] p r i n t _ l i s t ( n ) # e a c h l i n e : 3 5 7 # 2 n = [ 3 , 5 , 7 ] d e f d o u b l e _ l i s t ( x ) : f o r i i n r a n g e ( 0 , l e n ( x ) ) : x [ i ] = x [ i ] * 2 r e t u r n x p r i n t d o u b l e _ l i s t ( n ) # [ 6 , 1 0 , 1 4 ] 56 / 121
  57. Lists and Functions # 3 r a n g e

    ( 6 ) # = > [ 0 , 1 , 2 , 3 , 4 , 5 ] r a n g e ( 1 , 6 ) # = > [ 1 , 2 , 3 , 4 , 5 ] r a n g e ( 1 , 6 , 3 ) # = > [ 1 , 4 ] # r a n g e ( s t o p ) # r a n g e ( s t a r t , s t o p ) # r a n g e ( s t a r t , s t o p , s t e p ) d e f m y _ f u n c t i o n ( x ) : f o r i i n r a n g e ( 0 , l e n ( x ) ) : x [ i ] = x [ i ] * 2 r e t u r n x p r i n t m y _ f u n c t i o n ( r a n g e ( 3 ) ) # [ 0 , 1 , 2 ] # [ 0 , 2 , 4 ] 57 / 121
  58. Lists and Functions # 1 n = [ 3 ,

    5 , 7 ] d e f t o t a l ( n u m b e r s ) : r e s u l t = 0 f o r n u m i n n u m b e r s : # f o r i i n r a n g e ( l e n ( n u m b e r s ) ) : r e s u l t + = n u m # r e s u l t + = n u m b e r s [ i ] r e t u r n r e s u l t # 2 n = [ " M i c h a e l " , " L i e b e r m a n " ] d e f j o i n _ s t r i n g s ( w o r d s ) : r e s u l t = " " f o r i t e m i n w o r d s : r e s u l t + = i t e m r e t u r n r e s u l t p r i n t j o i n _ s t r i n g s ( n ) 58 / 121
  59. Lists and Functions # 3 o p e r a

    t o r + a g a k b e d a d i p y t h o n b u a t l i s t m = [ 1 , 2 , 3 ] n = [ 4 , 5 , 6 ] d e f j o i n _ l i s t s ( x , y ) : r e t u r n x + y p r i n t j o i n _ l i s t s ( m , n ) # [ 1 , 2 , 3 , 4 , 5 , 6 ] # 4 L i s t o f L i s t s n = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 , 7 , 8 , 9 ] ] d e f f l a t t e n ( l i s t s ) : r e s u l t s = [ ] f o r n u m b e r s i n l i s t s : f o r n u m i n n u m b e r s : r e s u l t s . a p p e n d ( n u m ) r e t u r n r e s u l t s p r i n t f l a t t e n ( n ) # [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] 59 / 121
  60. Lists and Functions Battleship! In this project you will build

    a simplified, one-player version of the classic board game Battleship! In this version of the game, there will be a single ship hidden in a random location on a 5x5 grid. The player will have 10 guesses to try to sink the ship. # t a m b a h e l e m e n b e r u l a n g p d l i s t b o a r d = [ ] f o r i i n r a n g e ( 5 ) : b o a r d . a p p e n d ( [ " O " ] * 5 ) # [ " O " , " O " , " O " , " O " , " O " ] p r i n t b o a r d # [ [ ] , . . , [ ] ] # p r i n t 1 r o w e l e m e n , 1 b a r i s b o a r d = [ ] f o r i i n r a n g e ( 5 ) : b o a r d . a p p e n d ( [ " O " ] * 5 ) # [ " O " , " O " , " O " , " O " , " O " ] d e f p r i n t _ b o a r d ( b o a r d ) : f o r r o w i n b o a r d : p r i n t r o w p r i n t _ b o a r d ( b o a r d ) 60 / 121
  61. Lists and Functions Battleship! (1) # . j o i

    n m e t h o d u s e s t h e s t r i n g t o c o m b i n e t h e i t e m s i n t h e l i s t . f r o m r a n d o m i m p o r t r a n d i n t b o a r d = [ ] f o r x i n r a n g e ( 5 ) : b o a r d . a p p e n d ( [ " O " ] * 5 ) d e f p r i n t _ b o a r d ( b o a r d ) : f o r r o w i n b o a r d : p r i n t " " . j o i n ( r o w ) p r i n t " L e t ' s p l a y B a t t l e s h i p ! " p r i n t _ b o a r d ( b o a r d ) # - - - d e f r a n d o m _ r o w ( b o a r d ) : r e t u r n r a n d i n t ( 0 , l e n ( b o a r d ) - 1 ) d e f r a n d o m _ c o l ( b o a r d ) : r e t u r n r a n d i n t ( 0 , l e n ( b o a r d [ 0 ] ) - 1 ) s h i p _ r o w = r a n d o m _ r o w ( b o a r d ) s h i p _ c o l = r a n d o m _ c o l ( b o a r d ) # p r i n t s h i p _ r o w # p r i n t s h i p _ c o l 61 / 121
  62. Lists and Functions Battleship! (2) # M u l t

    i l i n e i f c o n d i t i o n , g u e s s _ r o w n o t i n r a n g e ( 5 ) f o r t u r n i n r a n g e ( 4 ) : p r i n t " T u r n " , t u r n + 1 g u e s s _ r o w = i n t ( r a w _ i n p u t ( " G u e s s R o w : " ) ) g u e s s _ c o l = i n t ( r a w _ i n p u t ( " G u e s s C o l : " ) ) i f g u e s s _ r o w = = s h i p _ r o w a n d g u e s s _ c o l = = s h i p _ c o l : p r i n t " C o n g r a t u l a t i o n s ! Y o u s a n k m y b a t t l e s h i p ! " b r e a k e l s e : i f ( g u e s s _ r o w < 0 o r g u e s s _ r o w > 4 ) o r \ ( g u e s s _ c o l < 0 o r g u e s s _ c o l > 4 ) : p r i n t " O o p s , t h a t ' s n o t e v e n i n t h e o c e a n . " e l i f ( b o a r d [ g u e s s _ r o w ] [ g u e s s _ c o l ] = = " X " ) : p r i n t " Y o u g u e s s e d t h a t o n e a l r e a d y . " e l s e : p r i n t " Y o u m i s s e d m y b a t t l e s h i p ! " b o a r d [ g u e s s _ r o w ] [ g u e s s _ c o l ] = " X " p r i n t _ b o a r d ( b o a r d ) i f t u r n = = 3 : p r i n t " G a m e O v e r " 62 / 121
  63. Agenda 1. Python Syntax 2. Strings and Console Output 3.

    Conditionals and Control Flow 4. Functions 5. Lists and Dictionaries 6. Lists and Functions 7. Loops 8. Advanced Topics in Python 9. Introduction to Classes 10. File Input and Output 63 / 121
  64. Loops w h i l e # 1 c o

    u n t = 0 i f c o u n t < 5 : p r i n t " H e l l o , I a m a n i f s t a t e m e n t a n d c o u n t i s " , c o u n t w h i l e c o u n t < = 9 : p r i n t " H e l l o , I a m a w h i l e a n d c o u n t i s " , c o u n t c o u n t + = 1 # - - - # 2 l o o p _ c o n d i t i o n = T r u e w h i l e l o o p _ c o n d i t i o n : p r i n t " I a m a l o o p " l o o p _ c o n d i t i o n = F a l s e # - - - # 3 n u m = 1 w h i l e n u m < = 1 0 : # F i l l i n t h e c o n d i t i o n p r i n t n u m * * 2 n u m + = 1 64 / 121
  65. Loops w h i l e , b r e

    a k # - - - # 4 c h o i c e = r a w _ i n p u t ( ' E n j o y i n g t h e c o u r s e ? ( y / n ) ' ) w h i l e c h o i c e ! = ' y ' a n d c h o i c e ! = ' n ' : c h o i c e = r a w _ i n p u t ( " S o r r y , I d i d n ' t c a t c h t h a t . E n t e r a g a i n : " ) # - - - # 5 b r e a k c o u n t = 0 w h i l e T r u e : p r i n t c o u n t c o u n t + = 1 i f c o u n t > = 1 0 : b r e a k # - - - 65 / 121
  66. Loops w h i l e , e l s

    e # 6 w h i l e e l s e i m p o r t r a n d o m p r i n t " L u c k y N u m b e r s ! 3 n u m b e r s w i l l b e g e n e r a t e d . " p r i n t " I f o n e o f t h e m i s a ' 5 ' , y o u l o s e ! " c o u n t = 0 w h i l e c o u n t < 3 : n u m = r a n d o m . r a n d i n t ( 1 , 6 ) p r i n t n u m i f n u m = = 5 : p r i n t " S o r r y , y o u l o s e ! " b r e a k c o u n t + = 1 e l s e : p r i n t " Y o u w i n ! " # e l s e d i e k s e k u s i s e t e l a h k o n d i s i f a l s e ; t d k p e r n a h d i e k s e k u s i s e t e l a h b r e a k # - - - 66 / 121
  67. Loops w h i l e , e l s

    e # 7 f r o m r a n d o m i m p o r t r a n d i n t # G e n e r a t e s a n u m b e r f r o m 1 t h r o u g h 1 0 i n c l u s i v e r a n d o m _ n u m b e r = r a n d i n t ( 1 , 1 0 ) g u e s s e s _ l e f t = 3 w h i l e g u e s s e s _ l e f t > 0 : g u e s s = i n t ( r a w _ i n p u t ( " Y o u r g u e s s : " ) ) i f g u e s s = = r a n d o m _ n u m b e r : p r i n t ' Y o u w i n ! ' b r e a k g u e s s e s _ l e f t - = 1 e l s e : p r i n t ' Y o u l o s e . ' 67 / 121
  68. Loops f o r i n # 1 h o

    b b i e s = [ ] # A d d y o u r c o d e b e l o w ! f o r i i n r a n g e ( 3 ) : h o b b y = s t r ( r a w _ i n p u t ( " Y o u r h o b b y : " ) ) h o b b i e s . a p p e n d ( h o b b y ) ; # - - - # 2 t h i n g = " s p a m ! " f o r c i n t h i n g : p r i n t c w o r d = " e g g s ! " f o r c i n w o r d : p r i n t c 68 / 121
  69. Loops f o r i n Looping over a Dictionary

    # 3 p h r a s e = " A b i r d i n t h e h a n d . . . " f o r c h a r i n p h r a s e : i f c h a r = = " A " o r c h a r = = " a " : p r i n t " X " , e l s e : p r i n t c h a r , p r i n t # T h e , c h a r a c t e r a f t e r o u r p r i n t s t a t e m e n t m e a n s t h a t # o u r n e x t p r i n t s t a t e m e n t k e e p s p r i n t i n g o n t h e s a m e l i n e . # p l u s 1 s p a c e # - - - # 4 L o o p i n g o v e r a d i c t i o n a r y d = { ' a ' : ' a p p l e ' , ' b ' : ' b e r r y ' , ' c ' : ' c h e r r y ' } f o r k e y i n d : p r i n t k e y + " " + d [ k e y ] 69 / 121
  70. Loops f o r i n d e x ,

    i t e m i n e n u m e r a t e ( l i s t ) f o r a , b i n z i p ( l i s t _ a , l i s t _ b ) # 5 c h o i c e s = [ ' p i z z a ' , ' p a s t a ' , ' s a l a d ' , ' n a c h o s ' ] p r i n t ' Y o u r c h o i c e s a r e : ' f o r i n d e x , i t e m i n e n u m e r a t e ( c h o i c e s ) : p r i n t i n d e x + 1 , i t e m # - - - # 6 l i s t _ a = [ 3 , 9 , 1 7 , 1 5 , 1 9 ] l i s t _ b = [ 2 , 4 , 8 , 1 0 , 3 0 , 4 0 , 5 0 , 6 0 , 7 0 , 8 0 , 9 0 ] f o r a , b i n z i p ( l i s t _ a , l i s t _ b ) : # A d d y o u r c o d e h e r e ! i f a > b : p r i n t a e l s e : p r i n t b # z i p w i l l c r e a t e p a i r s o f e l e m e n t s w h e n p a s s e d t w o l i s t s , # a n d w i l l s t o p a t t h e e n d o f t h e s h o r t e r l i s t . # z i p c a n h a n d l e t h r e e o r m o r e l i s t s a s w e l l ! 70 / 121
  71. Loops f o r , e l s e #

    7 f r u i t s = [ ' b a n a n a ' , ' a p p l e ' , ' o r a n g e ' , ' t o m a t o ' , ' p e a r ' , ' g r a p e ' ] p r i n t ' Y o u h a v e . . . ' f o r f i n f r u i t s : i f f = = ' t o m a t o ' : p r i n t ' A t o m a t o i s n o t a f r u i t ! ' # ( I t a c t u a l l y i s . ) b r e a k p r i n t ' A ' , f e l s e : p r i n t ' A f i n e s e l e c t i o n o f f r u i t s ! ' # s p t e l s e w h i l e , k l b r e a k t i d a k d i r u n , k l e x i t n o r m a l y e s # - - - 71 / 121
  72. Loops f o r , e l s e #

    8 f r u i t s = [ ' b a n a n a ' , ' a p p l e ' , ' o r a n g e ' , ' t o m a t o ' , ' p e a r ' , ' g r a p e ' ] p r i n t ' Y o u h a v e . . . ' f o r f i n f r u i t s : i f f = = ' t o m a t o 1 ' : # k l m a t c h , e x e c u t e d ; e l s e n e v e r p r i n t ' A t o m a t o i s n o t a f r u i t ! ' b r e a k p r i n t ' A ' , f e l s e : p r i n t ' A f i n e s e l e c t i o n o f f r u i t s ! ' # - - - # 9 t h e l i s t = [ ' a ' , ' b ' , ' c ' ] f o r i i n t h e l i s t : p r i n t i e l s e : p r i n t ' c o m p l e t e d ! ' 72 / 121
  73. Loops Practice Makes Perfect # 1 d e f i

    s _ e v e n ( x ) : i f x % 2 = = 0 : r e t u r n T r u e e l s e : r e t u r n F a l s e # - - - # 2 d e f i s _ i n t ( x ) : d e l t a = x - i n t ( x ) i f d e l t a = = 0 . 0 : r e t u r n T r u e e l s e : r e t u r n F a l s e p r i n t i s _ i n t ( 7 . 0 ) p r i n t i s _ i n t ( 7 . 9 ) p r i n t i s _ i n t ( - 1 . 2 ) 73 / 121
  74. Loops Practice Makes Perfect # - - - # 3

    c a s t k e s t r , c a s t k e i n t d e f d i g i t _ s u m ( n ) : d s u m = 0 n _ s t r = s t r ( n ) f o r c i n n _ s t r : d s u m + = i n t ( c ) r e t u r n d s u m p r i n t d i g i t _ s u m ( 1 2 3 4 ) # - - - # 4 r e k u r s i f d e f f a c t o r i a l ( x ) : i f x = = 1 : r e t u r n 1 e l s e : r e t u r n x * f a c t o r i a l ( x - 1 ) p r i n t f a c t o r i a l ( 4 ) 74 / 121
  75. Loops Practice Makes Perfect # 5 d e f i

    s _ p r i m e ( x ) : i f x < 2 : r e t u r n F a l s e f o r n i n r a n g e ( x - 2 ) : i f x % ( n + 2 ) = = 0 : r e t u r n F a l s e r e t u r n T r u e p r i n t i s _ p r i m e ( 9 ) p r i n t i s _ p r i m e ( 1 1 ) # - - - # 6 d e f r e v e r s e ( t e x t ) : v a r l e n g t h = l e n ( t e x t ) t e x t r e v = " " f o r i i n r a n g e ( v a r l e n g t h ) : t e x t r e v + = t e x t [ v a r l e n g t h - 1 - i ] r e t u r n t e x t r e v p r i n t r e v e r s e ( ' a b c @ d e f ' ) # Y o u m a y n o t u s e r e v e r s e d o r [ : : - 1 ] t o h e l p y o u w i t h t h i s . # - - - 75 / 121
  76. Loops Practice Makes Perfect # 7 d e f a

    n t i _ v o w e l ( t e x t ) : r e t t e x t = " " f o r c i n t e x t : c l o w e r = c . l o w e r ( ) i f c l o w e r ! = ' a ' a n d c l o w e r ! = ' e ' a n d c l o w e r ! = ' i ' a n d c l o w e r ! = ' o ' a n d \ c l o w e r ! = ' u ' : r e t t e x t + = c r e t u r n r e t t e x t p r i n t a n t i _ v o w e l ( " H e y Y o u ! " ) # - - - s c o r e = { " a " : 1 , " c " : 3 , " b " : 3 , " e " : 1 , " d " : 2 , " g " : 2 , " f " : 4 , " i " : 1 , " h " : 4 , " k " : 5 , " j " : 8 , " m " : 3 , " l " d e f s c r a b b l e _ s c o r e ( w o r d ) : w o r d l o w e r = w o r d . l o w e r ( ) r e t s c o r e = 0 f o r c i n w o r d l o w e r : f o r k e y i n s c o r e : i f k e y = = c : r e t s c o r e + = s c o r e [ k e y ] b r e a k r e t u r n r e t s c o r e p r i n t s c r a b b l e _ s c o r e ( " H e l i x " ) 76 / 121
  77. Loops Practice Makes Perfect # 8 d e f c

    e n s o r ( t e x t , w o r d ) : t e x t l i s t = t e x t . s p l i t ( ) t e x t l i s t _ n e w = [ ] f o r i t e m i n t e x t l i s t : i f i t e m ! = w o r d : t e x t l i s t _ n e w . a p p e n d ( i t e m ) e l s e : t e x t l i s t _ n e w . a p p e n d ( " * " * l e n ( i t e m ) ) r e t u r n " " . j o i n ( t e x t l i s t _ n e w ) p r i n t c e n s o r ( " t h i s h a c k i s w a c k h a c k " , " h a c k " ) # - - - # 9 d e f c o u n t ( s e q u e n c e , i t e m ) : v a r c o u n t = 0 f o r i i n s e q u e n c e : i f i = = i t e m : v a r c o u n t + = 1 r e t u r n v a r c o u n t p r i n t c o u n t ( [ 1 , 2 , 1 , 1 ] , 1 ) 77 / 121
  78. Loops Practice Makes Perfect # 1 0 d e f

    p u r i f y ( n u m b e r s ) : r e t n u m b e r s = [ ] f o r n u m i n n u m b e r s : i f n u m % 2 = = 0 : r e t n u m b e r s . a p p e n d ( n u m ) r e t u r n r e t n u m b e r s p r i n t p u r i f y ( [ 1 , 2 , 3 ] ) # - - - # 1 1 d e f p r o d u c t ( n u m l i s t ) : r e s = 1 f o r n u m i n n u m l i s t : r e s * = n u m r e t u r n r e s p r i n t p r o d u c t ( [ 4 , 5 , 5 ] ) 78 / 121
  79. Loops Practice Makes Perfect # 1 2 d e f

    r e m o v e _ d u p l i c a t e s ( v a r l i s t ) : n e w l i s t = [ ] f o r v a r i n v a r l i s t : i f v a r n o t i n n e w l i s t : n e w l i s t . a p p e n d ( v a r ) r e t u r n n e w l i s t p r i n t r e m o v e _ d u p l i c a t e s ( [ 1 , 1 , 2 , 2 ] ) # - - - # 1 3 s o r t e d ( ) # T h e m e d i a n i s t h e m i d d l e n u m b e r i n a s o r t e d s e q u e n c e o f n u m b e r s . I f y o u a r e g i v e n a s e q u e n c e w i t h # a n e v e n n u m b e r o f e l e m e n t s , y o u m u s t a v e r a g e t h e t w o e l e m e n t s s u r r o u n d i n g t h e m i d d l e . # s o r t e d ( [ 5 , 2 , 3 , 1 , 4 ] ) - - > [ 1 , 2 , 3 , 4 , 5 ] d e f m e d i a n ( v a r l i s t ) : s o r t e d l i s t = s o r t e d ( v a r l i s t ) l e n g t h = l e n ( v a r l i s t ) i f l e n g t h % 2 = = 1 : r e t u r n s o r t e d l i s t [ ( ( l e n g t h + 1 ) / 2 - 1 ) ] e l s e : r e t u r n ( s o r t e d l i s t [ l e n g t h / 2 - 1 ] + s o r t e d l i s t [ l e n g t h / 2 ] ) / 2 . 0 p r i n t m e d i a n ( [ 1 , 1 , 2 ] ) # 1 p r i n t m e d i a n ( [ 7 , 3 , 1 , 4 ] ) # 3 . 5 79 / 121
  80. Exam Statistics (1) g r a d e s =

    [ 1 0 0 , 1 0 0 , 9 0 , 4 0 , 8 0 , 1 0 0 , 8 5 , 7 0 , 9 0 , 6 5 , 9 0 , 8 5 , 5 0 . 5 ] d e f p r i n t _ g r a d e s ( g r a d e s ) : f o r g r a d e i n g r a d e s : p r i n t g r a d e d e f g r a d e s _ s u m ( s c o r e s ) : t o t a l = 0 f o r s c o r e i n s c o r e s : t o t a l + = s c o r e r e t u r n t o t a l d e f g r a d e s _ a v e r a g e ( g r a d e s ) : r e t u r n g r a d e s _ s u m ( g r a d e s ) / f l o a t ( l e n ( g r a d e s ) ) p r i n t _ g r a d e s ( g r a d e s ) p r i n t g r a d e s _ s u m ( g r a d e s ) p r i n t g r a d e s _ a v e r a g e ( g r a d e s ) 80 / 121
  81. Exam Statistics (2) d e f g r a d

    e s _ a v e r a g e ( g r a d e s ) : s u m _ o f _ g r a d e s = g r a d e s _ s u m ( g r a d e s ) a v e r a g e = s u m _ o f _ g r a d e s / f l o a t ( l e n ( g r a d e s ) ) r e t u r n a v e r a g e d e f g r a d e s _ v a r i a n c e ( s c o r e s ) : a v e r a g e = g r a d e s _ a v e r a g e ( s c o r e s ) v a r i a n c e = 0 f o r s c o r e i n s c o r e s : v a r i a n c e + = ( a v e r a g e - s c o r e ) * * 2 v a r i a n c e / = f l o a t ( l e n ( s c o r e s ) ) r e t u r n v a r i a n c e d e f g r a d e s _ s t d _ d e v i a t i o n ( v a r i a n c e ) : r e t u r n v a r i a n c e * * 0 . 5 v a r i a n c e = g r a d e s _ v a r i a n c e ( g r a d e s ) p r i n t p r i n t _ g r a d e s ( g r a d e s ) p r i n t g r a d e s _ s u m ( g r a d e s ) p r i n t g r a d e s _ a v e r a g e ( g r a d e s ) p r i n t g r a d e s _ v a r i a n c e ( g r a d e s ) p r i n t g r a d e s _ s t d _ d e v i a t i o n ( v a r i a n c e ) 81 / 121
  82. Agenda 1. Python Syntax 2. Strings and Console Output 3.

    Conditionals and Control Flow 4. Functions 5. Lists and Dictionaries 6. Lists and Functions 7. Loops 8. Advanced Topics in Python 9. Introduction to Classes 10. File Input and Output 82 / 121
  83. Advanced Topics in Python List Comprehensions m y _ d

    i c t = { " N a m e " : " O t o n g " , " A g e " : 2 3 , " T i t l e " : " D r " } p r i n t m y _ d i c t . i t e m s ( ) # [ ( ' A g e ' , 2 3 ) , ( ' N a m e ' , ' O t o n g ' ) , ( ' T i t l e ' , ' D r ' ) ] p r i n t m y _ d i c t . k e y s ( ) # [ ' A g e ' , ' N a m e ' , ' T i t l e ' ] p r i n t m y _ d i c t . v a l u e s ( ) # [ 2 3 , ' O t o n g ' , ' D r ' ] f o r k e y i n m y _ d i c t : p r i n t k e y , m y _ d i c t [ k e y ] # A g e 2 3 # N a m e O t o n g # T i t l e D r # Y o u s h o u l d u s e p r i n t a , b r a t h e r t h a n p r i n t a + " " + b What if we wanted to generate a list according to some logic—for example, a list of all the even numbers from 0 to 50? List comprehensions are a powerful way to generate lists using the for/in and if keywords 83 / 121
  84. Advanced Topics in Python List Comprehensions # 1 e v

    e n s _ t o _ 5 0 = [ i f o r i i n r a n g e ( 5 1 ) i f i % 2 = = 0 ] p r i n t e v e n s _ t o _ 5 0 # [ 0 , 2 , 4 , 6 , 8 , 1 0 , 1 2 , 1 4 , 1 6 , 1 8 , 2 0 , 2 2 , 2 4 , 2 6 , 2 8 , 3 0 , 3 2 , 3 4 , 3 6 , 3 8 , 4 0 , 4 2 , 4 4 , 4 6 , 4 8 , 5 0 ] # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 2 n e w _ l i s t = [ x f o r x i n r a n g e ( 1 , 6 ) ] # = > [ 1 , 2 , 3 , 4 , 5 ] d o u b l e s = [ x * 2 f o r x i n r a n g e ( 1 , 6 ) ] # = > [ 2 , 4 , 6 , 8 , 1 0 ] d o u b l e s _ b y _ 3 = [ x * 2 f o r x i n r a n g e ( 1 , 6 ) i f ( x * 2 ) % 3 = = 0 ] # = > [ 6 ] # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 3 d o u b l e s _ b y _ 3 = [ x * 2 f o r x i n r a n g e ( 1 , 6 ) i f ( x * 2 ) % 3 = = 0 ] e v e n _ s q u a r e s = [ x * * 2 f o r x i n r a n g e ( 1 , 1 2 ) i f x % 2 = = 0 ] p r i n t e v e n _ s q u a r e s # [ 4 , 1 6 , 3 6 , 6 4 , 1 0 0 ] # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 84 / 121
  85. Advanced Topics in Python List Comprehensions # 4 c =

    [ ' C ' f o r x i n r a n g e ( 5 ) i f x < 3 ] p r i n t c # [ ' C ' , ' C ' , ' C ' ] # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 5 c u b e s _ b y _ f o u r = [ x * * 3 f o r x i n r a n g e ( 1 , 1 1 ) i f ( x * * 3 ) % 4 = = 0 ] p r i n t c u b e s _ b y _ f o u r # [ 8 , 6 4 , 2 1 6 , 5 1 2 , 1 0 0 0 ] # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 6 l = [ i * * 2 f o r i i n r a n g e ( 1 , 1 1 ) ] # S h o u l d b e [ 1 , 4 , 9 , 1 6 , 2 5 , 3 6 , 4 9 , 6 4 , 8 1 , 1 0 0 ] p r i n t l [ 2 : 9 : 2 ] # [ 9 , 2 5 , 4 9 , 8 1 ] - - 2 b e l a k a n g , 1 d i l o n c a t # l i s t s l i c i n g # [ s t a r t : e n d : s t r i d e ] # W h e r e s t a r t d e s c r i b e s w h e r e t h e s l i c e s t a r t s ( i n c l u s i v e ) , e n d i s w h e r e i t e n d s ( e x c l u s i v e ) , # a n d s t r i d e d e s c r i b e s t h e s p a c e b e t w e e n i t e m s i n t h e s l i c e d l i s t . F o r e x a m p l e , a s t r i d e o f 2 # w o u l d s e l e c t e v e r y o t h e r i t e m f r o m t h e o r i g i n a l l i s t t o p l a c e i n t h e s l i c e d l i s t . # s t a r t & e n d - - > i n d e x 85 / 121
  86. Advanced Topics in Python List Comprehensions # 7 t o

    _ f i v e = [ ' A ' , ' B ' , ' C ' , ' D ' , ' E ' ] p r i n t t o _ f i v e [ 3 : ] # [ ' D ' , ' E ' ] p r i n t t o _ f i v e [ : 2 ] # [ ' A ' , ' B ' ] p r i n t t o _ f i v e [ : : 2 ] # [ ' A ' , ' C ' , ' E ' ] " " " T h e d e f a u l t s t a r t i n g i n d e x i s 0 . T h e d e f a u l t e n d i n g i n d e x i s t h e e n d o f t h e l i s t . T h e d e f a u l t s t r i d e i s 1 . " " " # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 8 m y _ l i s t = r a n g e ( 1 , 1 1 ) # L i s t o f n u m b e r s 1 - 1 0 p r i n t m y _ l i s t [ : : 2 ] # [ 1 , 3 , 5 , 7 , 9 ] # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 86 / 121
  87. Advanced Topics in Python List Comprehensions # 9 l e

    t t e r s = [ ' A ' , ' B ' , ' C ' , ' D ' , ' E ' ] p r i n t l e t t e r s [ : : - 1 ] # [ ' E ' , ' D ' , ' C ' , ' B ' , ' A ' ] r i g h t t o l e f t # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 1 0 m y _ l i s t = r a n g e ( 1 , 1 1 ) b a c k w a r d s = m y _ l i s t [ : : - 1 ] # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 1 1 t o _ o n e _ h u n d r e d = r a n g e ( 1 0 1 ) b a c k w a r d s _ b y _ t e n s = t o _ o n e _ h u n d r e d [ : : - 1 0 ] p r i n t b a c k w a r d s _ b y _ t e n s # [ 1 0 0 , 9 0 , 8 0 , 7 0 , 6 0 , 5 0 , 4 0 , 3 0 , 2 0 , 1 0 , 0 ] # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # 1 2 t o _ 2 1 = r a n g e ( 1 , 2 2 ) o d d s = t o _ 2 1 [ : : 2 ] m i d d l e _ t h i r d = t o _ 2 1 [ 7 : 1 4 ] 87 / 121
  88. Advanced Topics in Python Lambda - Anonymous Function Functional programming

    : means that you're allowed to pass functions around just as if they were variables or values. The function the lambda creates is an anonymous function. Lambdas are useful when you need a quick function to do some work for you. When we pass the lambda to filter, filter uses the lambda to determine what to filter, and the second argument (my_list, which is just the numbers 0 – 15) is the list it does the filtering on. # e k i v a l e n l a m b d a x : x % 3 = = 0 d e f b y _ t h r e e ( x ) : r e t u r n x % 3 = = 0 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # f i l t e r m y _ l i s t = r a n g e ( 1 6 ) p r i n t f i l t e r ( l a m b d a x : x % 3 = = 0 , m y _ l i s t ) # y g b s d i b a g i 3 - > [ 0 , 3 , 6 , 9 , 1 2 , 1 5 ] # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 88 / 121
  89. Advanced Topics in Python Lambda - Anonymous Function l a

    n g u a g e s = [ " H T M L " , " J a v a S c r i p t " , " P y t h o n " , " R u b y " ] p r i n t f i l t e r ( l a m b d a x : x = = " P y t h o n " , l a n g u a g e s ) # [ ' P y t h o n ' ] # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - c u b e s = [ x * * 3 f o r x i n r a n g e ( 1 , 1 1 ) ] p r i n t f i l t e r ( l a m b d a x : x % 3 = = 0 , c u b e s ) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - s q u a r e s = [ x * * 2 f o r x i n r a n g e ( 1 , 1 1 ) ] p r i n t f i l t e r ( l a m b d a x : x > 3 0 a n d x < = 7 0 , s q u a r e s ) # [ 3 6 , 4 9 , 6 4 ] # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 89 / 121
  90. Advanced Topics in Python Recap # D i c t

    i o n a r y m o v i e s = { " M o n t y P y t h o n a n d t h e H o l y G r a i l " : " G r e a t " , " M o n t y P y t h o n ' s L i f e o f B r i a n " : " G o o d " , " M o n t y P y t h o n ' s M e a n i n g o f L i f e " : " O k a y " } p r i n t m o v i e s . i t e m s ( ) # f o r k e y i n m o v i e s : p r i n t k e y , m y _ d i c t [ k e y ] # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # C o m p r e h e n d i n g C o m p r e h e n s i o n s s q u a r e s = [ x * * 2 f o r x i n r a n g e ( 5 ) ] t h r e e s _ a n d _ f i v e s = [ x f o r x i n r a n g e ( 1 , 1 6 ) i f x % 3 = = 0 o r x % 5 = = 0 ] 90 / 121
  91. Advanced Topics in Python Recap # L i s t

    S l i c i n g s t r = " A B C D E F G H I J " # s t r [ s t a r t : e n d : s t r i d e ] - > s t a r t , e n d , s t r i d e = 1 , 6 , 2 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - g a r b l e d = " ! X e X g X a X s X s X e X m X X t X e X r X c X e X s X X e X h X t X X m X a X X I " r e v _ g a r b l e d = g a r b l e d [ : : - 1 ] m e s s a g e = r e v _ g a r b l e d [ : : 2 ] p r i n t m e s s a g e # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # L a m b d a m y _ l i s t = r a n g e ( 1 6 ) p r i n t f i l t e r ( l a m b d a x : x % 3 = = 0 , m y _ l i s t ) g a r b l e d = " I X X X a X X m X a X X X n X o X X X X X t X h X e X X X X r X s X X X X e X c X X X r X e X t m X X e X s X X X s X a X X X X X X g X e X ! X X " m e s s a g e = f i l t e r ( l a m b d a x : x ! = ' X ' , g a r b l e d ) p r i n t m e s s a g e 91 / 121
  92. Advanced Topics in Python Bitwise Operators # b i t

    w i s e o p e r a t o r p r i n t 5 > > 4 # R i g h t S h i f t 1 0 1 - > 0 p r i n t 5 < < 1 # L e f t S h i f t 1 0 1 - > 1 0 1 0 p r i n t 8 & 5 # B i t w i s e A N D 1 0 0 0 & 0 1 0 1 - > 0 p r i n t 9 | 4 # B i t w i s e O R p r i n t 1 2 ^ 4 2 # B i t w i s e X O R p r i n t ~ 8 8 # B i t w i s e N O T # 0 | 1 0 | 0 | 1 3 | 3 8 | - 8 9 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # h a s i l o p e r a s i p r i n t - > d e s i m a l p r i n t 0 b 1 , # 1 p r i n t 0 b 1 0 , # 2 p r i n t 0 b 1 1 , # 3 p r i n t 0 b 1 0 0 , # 4 p r i n t 0 b 1 0 1 , # 5 p r i n t 0 b 1 1 0 , # 6 p r i n t 0 b 1 1 1 # 7 # 1 2 3 4 5 6 7 p r i n t 0 b 1 + 0 b 1 1 # 4 p r i n t 0 b 1 1 * 0 b 1 1 # 9 92 / 121
  93. Advanced Topics in Python Bitwise Operators o n e =

    0 b 1 ; t w o = 0 b 1 0 ; t h r e e = 0 b 1 1 ; f o u r = 0 b 1 0 0 ; f i v e = 0 b 1 0 1 ; s i x = 0 b 1 1 0 ; s e v e n = 0 b 1 1 1 ; e i g h t = 0 b 1 0 0 0 ; n i n e = 0 b 1 0 0 1 ; t e n = 0 b 1 0 1 0 ; e l e v e n = 0 b 1 0 1 1 ; t w e l v e = 0 b 1 1 0 0 p r i n t e i g h t # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - p r i n t b i n ( 1 ) # 0 b 1 p r i n t b i n ( 2 ) # 0 b 1 0 p r i n t b i n ( 3 ) # 0 b 1 1 p r i n t b i n ( 4 ) # 0 b 1 0 0 p r i n t b i n ( 5 ) # 0 b 1 0 1 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - p r i n t i n t ( " 1 " , 2 ) # 1 p r i n t i n t ( " 1 0 " , 2 ) # 2 p r i n t i n t ( " 1 1 1 " , 2 ) # 7 p r i n t i n t ( " 0 b 1 0 0 " , 2 ) # 4 p r i n t i n t ( b i n ( 5 ) , 2 ) # 5 # P r i n t o u t t h e d e c i m a l e q u i v a l e n t o f t h e b i n a r y 1 1 0 0 1 0 0 1 . p r i n t i n t ( " 0 b 1 1 0 0 1 0 0 1 " , 2 ) # 2 0 1 # b a s e 2 s e m u a , p a k e i 0 b a t a u g a k , s a m a 93 / 121
  94. Advanced Topics in Python Bitwise Operators s h i f

    t _ r i g h t = 0 b 1 1 0 0 s h i f t _ l e f t = 0 b 1 s h i f t _ r i g h t = s h i f t _ r i g h t > > 2 s h i f t _ l e f t = s h i f t _ l e f t < < 2 p r i n t b i n ( s h i f t _ r i g h t ) # 0 b 1 1 p r i n t b i n ( s h i f t _ l e f t ) # 0 b 1 0 0 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - p r i n t b i n ( 0 b 1 1 1 0 & 0 b 1 0 1 ) # 0 b 1 0 0 p r i n t b i n ( 0 b 1 1 1 0 | 0 b 1 0 1 ) # 0 b 1 1 1 1 p r i n t b i n ( 0 b 1 1 1 0 ^ 0 b 1 0 1 ) # x o r 0 b 1 0 1 1 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - p r i n t ~ 1 # - 2 p r i n t ~ 2 # - 3 p r i n t ~ 3 # - 4 p r i n t ~ 4 2 # - 4 3 p r i n t ~ 1 2 3 # - 1 2 4 # t h i s i s e q u i v a l e n t t o a d d i n g o n e t o t h e n u m b e r a n d t h e n m a k i n g i t n e g a t i v e . 94 / 121
  95. Advanced Topics in Python Bitwise Operators # m a s

    k d e f c h e c k _ b i t 4 ( i n p u t ) : # i n p u t r e s e r v e d ? m a s k = 0 b 1 0 0 0 d e s i r e d = i n p u t & m a s k i f d e s i r e d > 0 : r e t u r n " o n " e l s e : r e t u r n " o f f " # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - a = 0 b 1 0 1 1 1 0 1 1 m a s k = 0 b 1 0 0 p r i n t b i n ( a | m a s k ) # 0 b 1 0 1 1 1 1 1 1 p r i n t b i n ( a ^ m a s k ) # 0 b 1 0 1 1 1 1 1 1 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - a = 0 b 1 1 1 0 1 1 1 0 m a s k = 0 b 1 1 1 1 1 1 1 1 # f l i p * s e m u a * b i t a p r i n t b i n ( a ^ m a s k ) # 0 b 1 0 0 0 1 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - d e f f l i p _ b i t ( n u m b e r , n ) : m a s k = ( 0 b 1 < < ( n - 1 ) ) r e s u l t = n u m b e r ^ m a s k r e t u r n b i n ( r e s u l t ) # x o r k a l a u m a s k 1 n g e - f l i p ; k a l o m a s k 0 t e t a p 95 / 121
  96. Agenda 1. Python Syntax 2. Strings and Console Output 3.

    Conditionals and Control Flow 4. Functions 5. Lists and Dictionaries 6. Lists and Functions 7. Loops 8. Advanced Topics in Python 9. Introduction to Classes 10. File Input and Output 96 / 121
  97. Introduction to Classes Python is an object-oriented programming language, which

    means it manipulates programming constructs called objects. You can think of an object as a single data structure that contains data as well as functions; functions of objects are called methods. c l a s s F r u i t ( o b j e c t ) : " " " A c l a s s t h a t m a k e s v a r i o u s t a s t y f r u i t s . " " " d e f _ _ i n i t _ _ ( s e l f , n a m e , c o l o r , f l a v o r , p o i s o n o u s ) : s e l f . n a m e = n a m e s e l f . c o l o r = c o l o r s e l f . f l a v o r = f l a v o r s e l f . p o i s o n o u s = p o i s o n o u s d e f d e s c r i p t i o n ( s e l f ) : p r i n t " I ' m a % s % s a n d I t a s t e % s . " % ( s e l f . c o l o r , s e l f . n a m e , s e l f . f l a v o r ) d e f i s _ e d i b l e ( s e l f ) : i f n o t s e l f . p o i s o n o u s : p r i n t " Y e p ! I ' m e d i b l e . " e l s e : p r i n t " D o n ' t e a t m e ! I a m s u p e r p o i s o n o u s . " # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - l e m o n = F r u i t ( " l e m o n " , " y e l l o w " , " s o u r " , F a l s e ) l e m o n . d e s c r i p t i o n ( ) l e m o n . i s _ e d i b l e ( ) 97 / 121
  98. Introduction to Classes p a s s doesn't do anything,

    but it's useful as a placeholder in areas of your code where Python expects an expression _ _ i n i t _ _ ( ) function is required for classes, and it's used to initialize the objects it creates. _ _ i n i t _ _ ( ) always takes at least one argument, s e l f , that refers to the object being created. You can think of _ _ i n i t _ _ ( ) as the function that "boots up" each object the class creates. We can access attributes of our objects using dot notation c l a s s A n i m a l ( o b j e c t ) : p a s s c l a s s A n i m a l ( o b j e c t ) : d e f _ _ i n i t _ _ ( s e l f ) : p a s s c l a s s A n i m a l ( o b j e c t ) : d e f _ _ i n i t _ _ ( s e l f , n a m e ) : s e l f . n a m e = n a m e z e b r a = A n i m a l ( " J e f f r e y " ) p r i n t z e b r a . n a m e # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - c l a s s S q u a r e ( o b j e c t ) : d e f _ _ i n i t _ _ ( s e l f ) : s e l f . s i d e s = 4 m y _ s h a p e = S q u a r e ( ) p r i n t m y _ s h a p e . s i d e s 98 / 121
  99. Introduction to Classes c l a s s A n

    i m a l ( o b j e c t ) : " " " M a k e s c u t e a n i m a l s . " " " d e f _ _ i n i t _ _ ( s e l f , n a m e , a g e , i s _ h u n g r y ) : s e l f . n a m e = n a m e s e l f . a g e = a g e s e l f . i s _ h u n g r y = i s _ h u n g r y # N o t e t h a t s e l f i s o n l y u s e d i n t h e _ _ i n i t _ _ ( ) f u n c t i o n * d e f i n i t i o n * ; # w e d o n ' t n e e d t o p a s s i t t o o u r i n s t a n c e o b j e c t s . z e b r a = A n i m a l ( " J e f f r e y " , 2 , T r u e ) g i r a f f e = A n i m a l ( " B r u c e " , 1 , F a l s e ) p a n d a = A n i m a l ( " C h a d " , 7 , T r u e ) p r i n t z e b r a . n a m e , z e b r a . a g e , z e b r a . i s _ h u n g r y # J e f f r e y 2 T r u e p r i n t g i r a f f e . n a m e , g i r a f f e . a g e , g i r a f f e . i s _ h u n g r y # B r u c e 1 F a l s e p r i n t p a n d a . n a m e , p a n d a . a g e , p a n d a . i s _ h u n g r y # C h a d 7 T r u e 99 / 121
  100. Introduction to Classes Class Scope Another important aspect of Python

    classes is scope. The scope of a variable is the context in which it's visible to the program. It may surprise you to learn that not all variables are accessible to all parts of a Python program at all times. When dealing with classes, you can have variables that are available everywhere (global variables), variables that are only available to members of a certain class (member variables), and variables that are only available to particular instances of a class (instance variables). 100 / 121
  101. Introduction to Classes Class Scope variables: global, member, instance c

    l a s s A n i m a l ( o b j e c t ) : " " " M a k e s c u t e a n i m a l s . " " " i s _ a l i v e = T r u e d e f _ _ i n i t _ _ ( s e l f , n a m e , a g e ) : s e l f . n a m e = n a m e s e l f . a g e = a g e z e b r a = A n i m a l ( " J e f f r e y " , 2 ) g i r a f f e = A n i m a l ( " B r u c e " , 1 ) p a n d a = A n i m a l ( " C h a d " , 7 ) p r i n t z e b r a . n a m e , z e b r a . a g e , z e b r a . i s _ a l i v e # J e f f r e y 2 T r u e p r i n t g i r a f f e . n a m e , g i r a f f e . a g e , g i r a f f e . i s _ a l i v e # B r u c e 1 T r u e p r i n t p a n d a . n a m e , p a n d a . a g e , p a n d a . i s _ a l i v e # C h a d 7 T r u e # i s _ a l i v e m e m b e r v a r i a b l e ; n a m e , a g e i n s t a n c e v a r i a b l e s 101 / 121
  102. Introduction to Classes Class Scope c l a s s

    A n i m a l ( o b j e c t ) : i s _ a l i v e = T r u e d e f _ _ i n i t _ _ ( s e l f , n a m e , a g e ) : s e l f . n a m e = n a m e s e l f . a g e = a g e d e f d e s c r i p t i o n ( s e l f ) : p r i n t s e l f . n a m e p r i n t s e l f . a g e h i p p o = A n i m a l ( " N i l " , 5 ) h i p p o . d e s c r i p t i o n ( ) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - h i p p o = A n i m a l ( " J a k e " , 1 2 ) c a t = A n i m a l ( " B o o t s " , 3 ) p r i n t h i p p o . i s _ a l i v e # T r u e h i p p o . i s _ a l i v e = F a l s e p r i n t h i p p o . i s _ a l i v e # F a l s e p r i n t c a t . i s _ a l i v e # T r u e 102 / 121
  103. Introduction to Classes Class Scope c l a s s

    A n i m a l ( o b j e c t ) : i s _ a l i v e = T r u e h e a l t h = " g o o d " d e f _ _ i n i t _ _ ( s e l f , n a m e , a g e ) : s e l f . n a m e = n a m e s e l f . a g e = a g e d e f d e s c r i p t i o n ( s e l f ) : p r i n t s e l f . n a m e p r i n t s e l f . a g e h i p p o = A n i m a l ( " N i l " , 5 ) h i p p o . d e s c r i p t i o n ( ) s l o t h = A n i m a l ( " s l o t h N a m e " , 6 ) o c e l o t = A n i m a l ( " o c e l o t N a m e " , 7 ) p r i n t h i p p o . h e a l t h # g o o d p r i n t s l o t h . h e a l t h # g o o d p r i n t o c e l o t . h e a l t h # g o o d # m e m b e r v a r i a b l e = c l a s s v a r i a b l e ( b u k a n i n s t a n c e , a v a i l a b l e t o a l l i n s t ) 103 / 121
  104. Introduction to Classes Class Scope c l a s s

    S h o p p i n g C a r t ( o b j e c t ) : " " " C r e a t e s s h o p p i n g c a r t o b j e c t s f o r u s e r s o f o u r f i n e w e b s i t e . " " " i t e m s _ i n _ c a r t = { } d e f _ _ i n i t _ _ ( s e l f , c u s t o m e r _ n a m e ) : s e l f . c u s t o m e r _ n a m e = c u s t o m e r _ n a m e d e f a d d _ i t e m ( s e l f , p r o d u c t , p r i c e ) : " " " A d d p r o d u c t t o t h e c a r t . " " " i f n o t p r o d u c t i n s e l f . i t e m s _ i n _ c a r t : s e l f . i t e m s _ i n _ c a r t [ p r o d u c t ] = p r i c e p r i n t p r o d u c t + " a d d e d . " e l s e : p r i n t p r o d u c t + " i s a l r e a d y i n t h e c a r t . " d e f r e m o v e _ i t e m ( s e l f , p r o d u c t ) : " " " R e m o v e p r o d u c t f r o m t h e c a r t . " " " i f p r o d u c t i n s e l f . i t e m s _ i n _ c a r t : d e l s e l f . i t e m s _ i n _ c a r t [ p r o d u c t ] p r i n t p r o d u c t + " r e m o v e d . " e l s e : p r i n t p r o d u c t + " i s n o t i n t h e c a r t . " 104 / 121
  105. Introduction to Classes Class Scope m y _ c a

    r t 1 = S h o p p i n g C a r t ( " O t o n g " ) m y _ c a r t 2 = S h o p p i n g C a r t ( " U d j a n g " ) m y _ c a r t 1 . a d d _ i t e m ( " O b e n g " , 2 0 0 0 ) # O b e n g a d d e d . m y _ c a r t 1 . a d d _ i t e m ( " O b e n g " , 1 0 0 0 ) # O b e n g i s a l r e a d y i n t h e c a r t . p r i n t m y _ c a r t 1 . i t e m s _ i n _ c a r t # { ' O b e n g ' : 2 0 0 0 } m y _ c a r t 2 . a d d _ i t e m ( " S a p u " , 2 0 0 0 ) # S a p u a d d e d . m y _ c a r t 2 . a d d _ i t e m ( " S a p u " , 1 0 0 0 ) # S a p u i s a l r e a d y i n t h e c a r t . p r i n t m y _ c a r t 2 . i t e m s _ i n _ c a r t # { ' O b e n g ' : 2 0 0 0 , ' S a p u ' : 2 0 0 0 } m y _ c a r t 1 . a d d _ i t e m ( " S a p u " , 5 0 0 0 ) # S a p u i s a l r e a d y i n t h e c a r t . p r i n t m y _ c a r t 1 . i t e m s _ i n _ c a r t # { ' O b e n g ' : 2 0 0 0 , ' S a p u ' : 2 0 0 0 } m y _ c a r t 2 . i t e m s _ i n _ c a r t = { ' B u k u ' : 3 0 0 0 } p r i n t m y _ c a r t 2 . i t e m s _ i n _ c a r t # { ' B u k u ' : 3 0 0 0 } p r i n t m y _ c a r t 1 . i t e m s _ i n _ c a r t # { ' O b e n g ' : 2 0 0 0 , ' S a p u ' : 2 0 0 0 } m y _ c a r t 2 . a d d _ i t e m ( " S a p u " , 1 0 0 0 ) # S a p u a d d e d . p r i n t m y _ c a r t 2 . i t e m s _ i n _ c a r t # { ' S a p u ' : 1 0 0 0 , ' B u k u ' : 3 0 0 0 } m y _ c a r t 1 . a d d _ i t e m ( " D o d o l " , 1 0 0 0 0 ) # D o d o l a d d e d . p r i n t m y _ c a r t 1 . i t e m s _ i n _ c a r t # { ' O b e n g ' : 2 0 0 0 , ' S a p u ' : 2 0 0 0 , ' D o d o l ' : 1 0 0 0 0 } p r i n t m y _ c a r t 2 . i t e m s _ i n _ c a r t # { ' S a p u ' : 1 0 0 0 , ' B u k u ' : 3 0 0 0 } 105 / 121
  106. Introduction to Classes Classes can be very useful for storing

    complicated objects with their own methods and variables. Defining a class is much like defining a function, but we use the class keyword instead. We also use the word object in parentheses because we want our classes to inherit the object class. This means that our class has all the properties of an object, which is the simplest, most basic class. Later we'll see that classes can inherit other, more complicated classes. Notes for Variables: To create instance variables, initialize them in the init function When Python sees self.X (object.X) it looks if there's a property X in your object, and if there is none, it looks at its class. 106 / 121
  107. Introduction to Classes Inheritance Inheritance is the process by which

    one class takes on the attributes and methods of another, and it's used to express an is-a relationship. For example, a Panda is a bear, so a Panda class could inherit from a Bear class. c l a s s C u s t o m e r ( o b j e c t ) : " " " P r o d u c e s o b j e c t s t h a t r e p r e s e n t c u s t o m e r s . " " " d e f _ _ i n i t _ _ ( s e l f , c u s t o m e r _ i d ) : s e l f . c u s t o m e r _ i d = c u s t o m e r _ i d d e f d i s p l a y _ c a r t ( s e l f ) : p r i n t " I ' m a s t r i n g t h a t s t a n d s i n f o r t h e c o n t e n t s o f y o u r s h o p p i n g c a r t ! " c l a s s R e t u r n i n g C u s t o m e r ( C u s t o m e r ) : " " " F o r c u s t o m e r s o f t h e r e p e a t v a r i e t y . " " " d e f d i s p l a y _ o r d e r _ h i s t o r y ( s e l f ) : p r i n t " I ' m a s t r i n g t h a t s t a n d s i n f o r y o u r o r d e r h i s t o r y ! " m o n t y _ p y t h o n = R e t u r n i n g C u s t o m e r ( " I D : 1 2 3 4 5 " ) m o n t y _ p y t h o n . d i s p l a y _ c a r t ( ) # C u s t o m e r . d i s p l a y _ c a r t m o n t y _ p y t h o n . d i s p l a y _ o r d e r _ h i s t o r y ( ) # R e t u r n i n g C u s t o m e r 107 / 121
  108. Introduction to Classes Inheritance c l a s s S

    h a p e ( o b j e c t ) : " " " M a k e s s h a p e s ! " " " d e f _ _ i n i t _ _ ( s e l f , n u m b e r _ o f _ s i d e s ) : s e l f . n u m b e r _ o f _ s i d e s = n u m b e r _ o f _ s i d e s c l a s s T r i a n g l e ( S h a p e ) : d e f _ _ i n i t _ _ ( s e l f , s i d e 1 , s i d e 2 , s i d e 3 ) : s e l f . s i d e 1 = s i d e 1 s e l f . s i d e 2 = s i d e 2 s e l f . s i d e 3 = s i d e 3 # O v e r r i d e _ _ i n i t _ _ s e g i t i g a 2 = T r i a n g l e ( 1 , 2 , 3 ) p r i n t s e g i t i g a 2 . s i d e 1 # p r i n t s e g i t i g a 2 . n u m b e r _ o f _ s i d e s # s u p e r ? 108 / 121
  109. Introduction to Classes Inheritance c l a s s E

    m p l o y e e ( o b j e c t ) : d e f _ _ i n i t _ _ ( s e l f , n a m e ) : s e l f . n a m e = n a m e d e f g r e e t ( s e l f , o t h e r ) : p r i n t " H e l l o , % s " % o t h e r . n a m e c l a s s C E O ( E m p l o y e e ) : d e f g r e e t ( s e l f , o t h e r ) : p r i n t " G e t b a c k t o w o r k , % s ! " % o t h e r . n a m e c e o = C E O ( " E m i l y " ) e m p = E m p l o y e e ( " S t e v e " ) e m p . g r e e t ( c e o ) # H e l l o , E m i l y c e o . g r e e t ( e m p ) # G e t b a c k t o w o r k , S t e v e ! 109 / 121
  110. Introduction to Classes Inheritance Sometimes you'll want one class that

    inherits from another to not only take on the methods and attributes of its parent, but to override one or more of them. On the flip side, sometimes you'll be working with a derived class (or subclass) and realize that you've overwritten a method or attribute defined in that class' base class (also called a parent or superclass) that you actually need. Have no fear! You can directly access the attributes or methods of a superclass with Python's built-in super call. 110 / 121
  111. Introduction to Classes Inheritance c l a s s E

    m p l o y e e ( o b j e c t ) : " " " M o d e l s r e a l - l i f e e m p l o y e e s ! " " " d e f _ _ i n i t _ _ ( s e l f , e m p l o y e e _ n a m e ) : s e l f . e m p l o y e e _ n a m e = e m p l o y e e _ n a m e d e f c a l c u l a t e _ w a g e ( s e l f , h o u r s ) : s e l f . h o u r s = h o u r s r e t u r n h o u r s * 2 0 . 0 0 c l a s s P a r t T i m e E m p l o y e e ( E m p l o y e e ) : d e f c a l c u l a t e _ w a g e ( s e l f , h o u r s ) : s e l f . h o u r s = h o u r s r e t u r n h o u r s * 1 2 . 0 0 d e f f u l l _ t i m e _ w a g e ( s e l f , h o u r s ) : r e t u r n s u p e r ( P a r t T i m e E m p l o y e e , s e l f ) . c a l c u l a t e _ w a g e ( h o u r s ) m i l t o n = P a r t T i m e E m p l o y e e ( " M i l t o n " ) p r i n t m i l t o n . f u l l _ t i m e _ w a g e ( 1 0 ) 111 / 121
  112. Introduction to Classes c l a s s T r

    i a n g l e ( o b j e c t ) : n u m b e r _ o f _ s i d e s = 3 d e f _ _ i n i t _ _ ( s e l f , a n g l e 1 , a n g l e 2 , a n g l e 3 ) : s e l f . a n g l e 1 = a n g l e 1 s e l f . a n g l e 2 = a n g l e 2 s e l f . a n g l e 3 = a n g l e 3 d e f c h e c k _ a n g l e s ( s e l f ) : s u m _ a n g l e = s e l f . a n g l e 1 + s e l f . a n g l e 2 + s e l f . a n g l e 3 i f s u m _ a n g l e = = 1 8 0 : r e t u r n T r u e e l s e : r e t u r n F a l s e c l a s s E q u i l a t e r a l ( T r i a n g l e ) : a n g l e = 6 0 d e f _ _ i n i t _ _ ( s e l f ) : s e l f . a n g l e 1 = s e l f . a n g l e s e l f . a n g l e 2 = s e l f . a n g l e s e l f . a n g l e 3 = s e l f . a n g l e m y _ t r i a n g l e = T r i a n g l e ( 9 0 , 3 0 , 6 0 ) p r i n t m y _ t r i a n g l e . n u m b e r _ o f _ s i d e s # 3 i n h e r i t e d p r i n t m y _ t r i a n g l e . c h e c k _ a n g l e s ( ) # T r u e 112 / 121
  113. Introduction to Classes c l a s s C a

    r ( o b j e c t ) : c o n d i t i o n = " n e w " d e f _ _ i n i t _ _ ( s e l f , m o d e l , c o l o r , m p g ) : s e l f . m o d e l = m o d e l ; s e l f . c o l o r = c o l o r ; s e l f . m p g = m p g d e f d i s p l a y _ c a r ( s e l f ) : r e t u r n " T h i s i s a % s % s w i t h % s M P G . " % ( s e l f . c o l o r , s e l f . m o d e l , s t r ( s e l f . m p g ) ) d e f d r i v e _ c a r ( s e l f ) : s e l f . c o n d i t i o n = " u s e d " c l a s s E l e c t r i c C a r ( C a r ) : d e f _ _ i n i t _ _ ( s e l f , m o d e l , c o l o r , m p g , b a t t e r y _ t y p e ) : s u p e r ( E l e c t r i c C a r , s e l f ) . _ _ i n i t _ _ ( m o d e l , c o l o r , m p g ) s e l f . b a t t e r y _ t y p e = b a t t e r y _ t y p e d e f d r i v e _ c a r ( s e l f ) : s e l f . c o n d i t i o n = " l i k e n e w " m y _ c a r = E l e c t r i c C a r ( " D e L o r e a n " , " s i l v e r " , 8 8 , " m o l t e n s a l t " ) p r i n t m y _ c a r . c o n d i t i o n # n e w m y _ c a r . d r i v e _ c a r ( ) # p r i n t m y _ c a r . c o n d i t i o n # l i k e n e w m y _ c a r 1 = C a r ( " D e L o r e a n " , " s i l v e r " , 8 8 ) p r i n t m y _ c a r 1 . c o n d i t i o n # n e w p r i n t m y _ c a r 1 . m o d e l # D e L o r e a n p r i n t m y _ c a r 1 . d i s p l a y _ c a r ( ) # T h i s i s a s i l v e r D e L o r e a n w i t h 8 8 M P G . m y _ c a r 1 . d r i v e _ c a r ( ) p r i n t m y _ c a r 1 . c o n d i t i o n # u s e d 113 / 121
  114. Introduction to Classes Usually, classes are most useful for holding

    and accessing abstract collections of data. One useful class method to override is the built-in _ _ r e p r _ _ ( ) method, which is short for representation; by providing a return value in this method, we can tell Python how to represent an object of our class (for instance, when using a print statement). c l a s s P o i n t 3 D ( o b j e c t ) : d e f _ _ i n i t _ _ ( s e l f , x , y , z ) : s e l f . x = x s e l f . y = y s e l f . z = z d e f _ _ r e p r _ _ ( s e l f ) : r e t u r n " ( % d , % d , % d ) " % ( s e l f . x , s e l f . y , s e l f . z ) m y _ p o i n t = P o i n t 3 D ( 1 , 2 , 3 ) p r i n t m y _ p o i n t # ( 1 , 2 , 3 ) 114 / 121
  115. Agenda 1. Python Syntax 2. Strings and Console Output 3.

    Conditionals and Control Flow 4. Functions 5. Lists and Dictionaries 6. Lists and Functions 7. Loops 8. Advanced Topics in Python 9. Introduction to Classes 10. File Input and Output 115 / 121
  116. File Input and Output Built-in io functions Write # G

    e n e r a t e s a l i s t o f s q u a r e s o f t h e n u m b e r s 1 - 1 0 m y _ l i s t = [ i * * 2 f o r i i n r a n g e ( 1 , 1 1 ) ] f = o p e n ( " o u t p u t . t x t " , " w " ) f o r i t e m i n m y _ l i s t : f . w r i t e ( s t r ( i t e m ) + " \ n " ) f . c l o s e ( ) # T h i s t o l d P y t h o n t o o p e n o u t p u t . t x t i n " w " m o d e ( " w " s t a n d s f o r " w r i t e " ) . # W e s t o r e d t h e r e s u l t o f t h i s o p e r a t i o n i n a f i l e o b j e c t , f . 116 / 121
  117. File Input and Output Write m y _ l i

    s t = [ i * * 2 f o r i i n r a n g e ( 1 , 1 1 ) ] m y _ f i l e = o p e n ( " o u t p u t . t x t " , " r + " ) f o r i i n m y _ l i s t : m y _ f i l e . w r i t e ( s t r ( i ) + " \ n " ) m y _ f i l e . c l o s e ( ) # " r + " a s a s e c o n d a r g u m e n t t o t h e f u n c t i o n s o t h e # f i l e w i l l a l l o w y o u t o r e a d a n d w r i t e 117 / 121
  118. File Input and Output Read m y _ f i

    l e = o p e n ( " o u t p u t . t x t " , " r " ) p r i n t m y _ f i l e . r e a d ( ) m y _ f i l e . c l o s e ( ) # - - - # i f w e w a n t t o r e a d f r o m a f i l e l i n e b y l i n e , # r a t h e r t h a n p u l l i n g t h e e n t i r e f i l e i n a t o n c e . m y _ f i l e = o p e n ( " t e x t . t x t " , " r " ) p r i n t m y _ f i l e . r e a d l i n e ( ) p r i n t m y _ f i l e . r e a d l i n e ( ) p r i n t m y _ f i l e . r e a d l i n e ( ) m y _ f i l e . c l o s e ( ) 118 / 121
  119. File Input and Output During the I/O process, data is

    buffered: this means that it is held in a temporary location before being written to the file. Python doesn't flush the buffer—that is, write data to the file—until it's sure you're done writing. One way to do this is to close the file. If you write to a file without closing, the data won't make it to the target file. # O p e n t h e f i l e f o r r e a d i n g r e a d _ f i l e = o p e n ( " t e x t . t x t " , " r " ) # U s e a s e c o n d f i l e h a n d l e r t o o p e n t h e f i l e f o r w r i t i n g w r i t e _ f i l e = o p e n ( " t e x t . t x t " , " w " ) # W r i t e t o t h e f i l e w r i t e _ f i l e . w r i t e ( " N o t c l o s i n g f i l e s i s V E R Y B A D . " ) w r i t e _ f i l e . c l o s e ( ) # T r y t o r e a d f r o m t h e f i l e p r i n t r e a d _ f i l e . r e a d ( ) r e a d _ f i l e . c l o s e ( ) 119 / 121
  120. File Input and Output File objects contain a special pair

    of built-in methods: _ _ e n t e r _ _ ( ) and _ _ e x i t _ _ ( ) . The details aren't important, but what is important is that when a file object's _ _ e x i t _ _ ( ) method is invoked, it automatically closes the file. How do we invoke this method? With with and as. w i t h o p e n ( " t e x t . t x t " , " w " ) a s t e x t f i l e : t e x t f i l e . w r i t e ( " S u c c e s s ! " ) # g a k p e r l u c l o s e , c l o s e a u t o m a t i c a l l y # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - f = o p e n ( " b g . t x t " ) f . c l o s e d # F a l s e f . c l o s e ( ) f . c l o s e d # T r u e # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - w i t h o p e n ( " t e x t . t x t " , " w " ) a s m y _ f i l e : m y _ f i l e . w r i t e ( " o t o n g " ) ; i f m y _ f i l e . c l o s e d = = F a l s e : m y _ f i l e . c l o s e ( ) p r i n t m y _ f i l e . c l o s e d 120 / 121
  121. Python - An Introduction END Eueung Mulyana | http://eueung.github.io/EL6240/py based

    on the material @Codecademy Attribution-ShareAlike CC BY-SA 121 / 121