Hands-On Python a Tutorial Introduction for Beginners

download Hands-On Python a Tutorial Introduction for Beginners

of 139

Transcript of Hands-On Python a Tutorial Introduction for Beginners

  • 8/18/2019 Hands-On Python a Tutorial Introduction for Beginners

    1/139

    Hands-On Python

    A Tutorial Introduction for Beginners

    Python 3.1 Version

    Dr. Andrew N. Harrington

    Computer Science Department, Loyola University Chicago

    ©  Released under the Creative commons Attribution-Noncommercial-Share

    Alike 3.0 United States License

    http://creativecommons.org/licenses/by-nc-sa/3.0/us/

  • 8/18/2019 Hands-On Python a Tutorial Introduction for Beginners

    2/139

  • 8/18/2019 Hands-On Python a Tutorial Introduction for Beginners

    3/139

    Contents

    Chapter 1. Beginning With Python 41.1. Context 41.2. The Python Interpreter and Idle, Part I 61.3. Whirlwind Intro duction To Typ es and Functions 111.4. Integer Arithmetic 121.5. Strings, Part I 141.6. Variables and Assignment 151.7. Print Function, Part I 161.8. Strings Part I I 17

    1.9. The Idle Editor and Execution 171.10. Input and Output 191.11. Defining Functions of your Own 231.12. Dictionaries 311.13. Lo ops and Sequences 351.14. Decimals, Floats, and Floating Point Arithmetic 451.15. Summary 47

    Chapter 2. Ob jects and Metho ds 532.1. Strings, Part I I I 532.2. More Classes and Metho ds 592.3. Mad Libs Revisited 612.4. Graphics 662.5. Files 882.6. Summary 90

    Chapter 3. More On Flow of Control 933.1. If Statements 933.2. Lo ops and Tuples 1053.3. While Statements 1093.4. Arbitrary Typ es Treated As Bo olean 1203.5. Further Topics to Consider 1223.6. Summary 123

    Chapter 4. Dynamic Web Pages 1264.1. Web page Basics 1264.2. Comp osing Web Pages in Python 128

    4.3. CGI - Dynamic Web Pages 1314.4. Summary 138

    3

  • 8/18/2019 Hands-On Python a Tutorial Introduction for Beginners

    4/139

    C HA P TE R 1

    Beginning With Python

    1.1. Context

    Yo u h av e p r ob a bl r y u s ed c o mp u te r s t o d o a l l s o rt s o f u s ef u l a n d i nt e re s ti n g t h in g s. I n e a ch a p pl i ca t io n ,t h e c o m p u te r r e s p o n d s i n d i ff e re n t w ay s t o y o ur i n p u t , f r o m t h e k e y b oa r d , m o u s e o r a fi l e . S t i ll t h e u n d e rl y i ngo p e ra t io n s a r e d e te r mi n ed b y t h e d e si g n o f t h e p r o g ra m y ou a r e g i v en . I n t h is s e t o f t u to r ia l s y o u w il l l e ar nt o w r it e y ou r o wn c o mp u te r p r og r am s , s o y ou c a n g i v e t h e c o mp u te r i n st r uc t io n s t o r e ac t i n t h e w a y you want.

    1.1.1. Low-Level and High-Level Computer Operations.   F i r s t l e t u s p l a c e P y t h o n p r o g r a m m i n gi n t h e c o nt e xt o f t h e c o mp u te r h a rd wa r e. A t t h e m o st f u nd a me nt a l l e v el i n t h e c o mp u te r t h er e a r e i n st r uc -

    t i o n s b u i l t i n t o t h e h a r d wa r e . T h e se a r e v e r y s i m pl e i n s t r u c t io n s , p e c u l i a r t o t h e h a r d w ar e o f y o ur p a r t ic u l art yp e o f c o mp u te r . T h e i n s tr u ct i on s a r e d e s i gn e d t o b e s i mp l e f o r t h e h a r d wa r e t o e x ec u te , n o t f o r h um a ns t of o l lo w . T h e e a r l i es t p r o g r a m mi n g w a s d o n e w i t h s u c h i n s t r u c ti o n s . I f w a s d i ffi c u l t a n d e r r o r - p ro n e . A m a j o ra d va n ce w as t h e d e ve l op m en t o f h i gh e r- l ev el l a ng u ag e s a n d t r an s la t or s f o r t h em . H ig h er - le ve l l a ng u ag e sa l lo w c o mp u te r p r og r am me r s t o w ri t e i n st r uc t io n s i n a f o rm a t t h at i s e a si e r f o r h um a ns t o u n de r st a nd . Fo rexample

    z = x+ y

    i s a n i n s t ru c t i on i n m a ny h i g h -l e ve l l a n gu a g e s t h a t m e a n s s o m e t h in g l i ke :

    ( 1 ) A cc e ss t h e v a lu e s t or e d a t a l o ca t io n l a b el e d x( 2 ) C a lc u la t e t h e s u m o f t h is va l ue a n d t h e va l ue s t or e d a t a l o ca t io n l a b el e d y( 3) S to re t he r es ul t i n a l o ca ti on l ab e le d z .

    N o c o m p u t e r u n d e r st a n d s t h e h i g h - l e ve l i n s t r u c ti o n d i r e c t ly ; i t i s n o t i n m a ch i n e l a n g u a ge . A s p e c i a l p r o g r a m

    m u st fi r s t t r a n s l a te i n s t ru c t io n s l i ke t h i s o n e i n to m a ch i n e l a n g u a g e. T h i s o n e h i g h -l e ve l i n s tr u c t io n m i g ht b et r a n s l a t e d i n t o a s e q u e n c e o f t h r e e m a c h i n e l a n g u a g e i n s t r u c t i o n s c o r r e s p o n d i n g t o t h e t h r e e s t e p d e s c r i p t i o nabove:

    0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 1

    0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0

    0 0 0 0 0 1 0 1 1 0 0 0 0 0 1 1

    O b vi o u s ly h i g h -l e ve l l a n gu a g e s w e r e a g r e a t a d v a n ce i n c l a ri t y!I f y ou f ol lo w a b ro ad i nt ro d uc ti on t o c om pu ti ng , y ou w il l l ea rn m or e a b ou t t he l aye rs t ha t c on ne ct

    l o w- l e ve l d i g it a l c o m p ut e r c i r cu i t s t o h i g h -l e ve l l a n g ua g e s .

    1.1.2. Why Python.   T he re a re m an y h ig h- le ve l l an gu ag es . T he l an gu ag e yo u w il l b e l ea rn in g i sP y th o n. P y th o n i s o n e o f t h e e a s i es t l a ng u ag e s t o l e ar n a n d u s e , w h il e a t t h e s a m e t i me b e i ng v er y p o we r fu l :

    I t i s u se d b y m an y o f t he m os t h ig hl y p ro d uc ti ve p ro fe ss io na l p ro gr am me rs . A f ew o f t he p la ce s t ha t u seP y t h on e x t e ns i ve l y a r e G o o g l e, t h e N e w Yo r k S t o c k E x ch a n g e, I n d u st r i al L i g ht a n d M a g i c , . . . . A l s o P y t h o ni s a f r ee l a ng u ag e ! I f y ou h av e y ou r o wn c o mp u te r , y o u c a n d o w nl o ad i t f r om t h e I n t er n et . .. .

    1.1.3. Obtaining Python for Your Computer.   I f y ou a r e n o t s u re w h et h er y ou r c o mp u te r a l re a dyh a s P y t h on , c o n ti nu e t o S e ct i on 1 . 2. 2 , a n d g i ve i t a t r y. I f i t w or k s, y ou a r e a l l s e t.

    I f yo u d o n ee d a c opy o f P yt ho n, g o t o t he D ow nl oa ds p ag e l in ke d t o   h t t p : / / w w w . p y t h o n . o r g . B ec a re f ul t o c ho o s e t h e v er s io n f o r y ou r o p e ra t in g s y st e m a n d h a rd wa r e. C h os s e a s t ab l e v er s io n , 3 . 1 o r l a te r .D o n ot c ho o se a v er si on 2 .X , w hi ch i s i nc om pa ti bl e. ( Ve rs io n 2 .6 i s d es cr ib e d i n a n o ld er v er si on o f t hi stutorial.)

    4

  • 8/18/2019 Hands-On Python a Tutorial Introduction for Beginners

    5/139

    1.1. CONTEXT 5

    W in dow s Yo u j us t n ee d t o e xe cu te t he i ns ta ll er , a nd i nt er ac t e no ug h t o a gr ee t o a ll t he d ef au lt ch oi ce s.P y th o n w o r ks i n W in d ow s a s w el l a s o n A p pl e s a n d i n t h e f r e e o p e ra t in g s y st e m L i n ux .

    O S X D ou bl e- cl ick o n t he i ns ta ll er . F in d a nd r un t he M ac Py th on .m pk g t ha t i s i n si de . Fo ll ow t hed e f a ul t s f o r i n s t al l a ti o n .

    L in ux P yt ho n i s g e n er al ly i n s ta ll ed , t h ou gh I d l e i s n o t a lwa ys i ns ta ll ed . L o ok f o r s o me th in g l i ke ’ i dl e-p y th o n ’ ( t h e n a m e i n t h e U b u n t u d i s t r i b ut i o n) .

    1.1.4. Philosophy and Implementation of the Hands-On Python Tutorials.   Although Pythoni s a h i g h - l e v e l l a n g u a g e , i t i s  not   E n g l is h o r s o m e o t h e r n a t u r a l h u m an l a n gu a g e . T h e P y t h o n t r a n s l at o r d o e sn o t u n d e r s t an d “ a d d t h e n u m b er s t w o a n d t h r e e”. P y t h on i s a f o r ma l l a n g u a g e w i t h i t s o w n s p e c i fic r u l e s a n df o rm a ts , w hi ch t h es e t u to r ia l s w il l i nt r o du c e g r ad u al l y, a t a p a ce i nt e nd e d f o r a b e g in n er . T h es e t u to r ia l sa r e a l s o a p p r o p r i a t e f o r b e g i n n e r s b e c a u s e t h e y g r a d u a l l y i n t r o d u c e f u n d a m e n t a l l o g i c a l p r o g r a m m i n g s k i l l s .L e ar n in g t h es e s k il l s w il l a l lo w y ou t o m uc h m o re e a si l y p r og r am i n o t he r l a ng u ag e s b e s id e s P y th o n. S o meo f t h e s k i ll s y o u w i ll l e ar n a r e

    •   b r e a k i n g d o w n p r o b le m s i n to m a n ag e a b le p a r t s•   b u i l d i n g u p c r e a ti v e s o l ut i o n s•   m a ki n g s u r e t h e s o lu t io n s a r e c l ea r f o r h um a ns•   m a ki n g s u re t h e s o lu t io n s a l so w or k c o rr e ct l y o n t h e c o mp u te r .

    G u i d in g P r i n ci p a ls f o r t h e H a n ds - o n P y t h on Tu t o r ia l s :

    •   T h e b e s t w ay t o l e ar n i s b y a c ti ve p a rt i ci p at i on . I n fo r ma t io n i s p r in c ip a ll y i nt r o du c ed i n s m al lq u an ti t ie s , w he r e y ou r a c ti ve p a rt i ci p at i on , e x p er i en c in g P y th o n, i s a s su m ed . I n m a ny p l ac e y ouw i ll o n ly b e a b le t o s e e w h a t P y t ho n d o e s b y d o in g i t y o u rs e lf ( i n a h a nd s -o n f a s h io n ). T he t u to r ia lw i ll o f te n n o t s h ow . A mo n g t h e m o st c o mm o n a n d i m p or t an t w o r ds i n t h e t u t o ri a l a r e “ Tr y t h is : ”

    •   O t h er r e qu e st s a r e f o r m o re c r ea t iv e r e sp o n se s . S o me t im e s t h e r e a r e H in ts , w h ic h e n d u p a s h y-p e r li n ks i n t h e w eb p a ge v er s io n , a n d f o o tn o te r e fe r en c es i n t h e p d f v er s io n . B o th f o rm a ts s h ou l de n co u ra g e y o u t o t h in k a c ti ve l y a b o ut y ou r r e sp o n se fi r st b e f or e l o o ki n g u p t h e h i n t.

    T h e t u t or i a l s a l s o p r o vi d e l a b e l ed e x e r ci s e s , f o r f u r th e r p r a c ti c e , w i t h o u t i m m e d i at e a n s we r s p r o -v i de d . T h e e x e r ci s es a r e l a b e l ed a t t h re e l e ve l s

    *:   I mm e di a te r e in f or c em en t o f b a si c i d ea s – p r ef e ra b ly d o o n y ou r fi r st p a ss .

    **:   I m p o r ta nt a n d m o r e s u b s ta nt i al – b e s u re y ou c a n e n d u p d o in g t h es e .***:   M o s t c r e a ti v e

    •   I nf or ma ti on i s i nt ro d uc ed i n a n o rd er t ha t g iv es y ou w ha t y ou n ee d a s s o on a s p o ss ib le . T hei n fo r ma t io n i s p r es e nt e d i n c o nt e xt . C o mp l ex i ty a n d i nt r ic a cy t h at i s n o t i mm e di a te l y n e ed e d i sd e l ay e d u n t i l l a t e r , w h e n y o u a r e m o r e e x p e r i e n ce d .

    •   I n m a ny p l ac e s t h er e a r e c o mp l ic a ti o ns t h at   are   i m po rt an t i n t he b e gi nn in g, b e ca us e t he re i s acommon    e r ro r c au se d b y a s li gh t m is us e o f t he c ur re nt t op ic . I f s uch a c om mo n e rr or i s l ik el y t om ak e n o s en se a nd s lo w y o u d ow n, m or e i nf or ma ti on i s g iv en t o a ll ow yo u t o h ea d o ff o r e as il yr e ac t t o s u ch a n e r ro r .

    A l th o ug h t h is a p pr o ac h i s a n e ff ec t iv e w ay t o i nt r o du c e m at e ri a l, i t i s n o t s o g o o d f o r r e f e re n ce . R e fe r en c in gi s a d d r es s e d i n s e ve r a l w a y s :

    •   A n e x t en s i ve Ta b l e o f C o nt e nt s•   E a s y j u mp i ng t o c ho s en t e xt i n a b r ow s er l i ke F i re f ox•   C r o s s r e f e r e n ce s t o s e c t i o n s t h a t e l a b o ra t e o n a n i n tr o d u c to r y s e c t i o n•   C o n c i s e c h a p te r s u m ma r i e s, g r o u pi n g l o g i c a ll y r e l a t e d i t e m s , e v e n i f t h a t d o e s n o t m a t c h t h e o r d e r

    o f i n tr o d u c ti o n .

    S o me p e o pl e l e ar n b e t te r v i su a ll y a n d v er b al ly f r om t h e v e r y b e g in n in g . S o me p a rt s o f t h e t u t o ri a l w i l l a l soh av e l i nk s t o c o rr e sp o n di n g fl a sh v i de o s e gm en ts . M a ny p e o pl e w il l fi n d r e ad i ng f a st e r a n d m o re e ff ec t iv e,b u t t h e v i d e o s e g m e n t s m a y b e p a r t i c u l a r l y u s e f u l w h e r e a c o m p u t e r i n t e r f a c e c a n b e n o t o n l y e x p l a i n e d b u ta ct ua ll y d em on st ra te d. T he l in ks t o s uch s eg me nt s w il l b e l ab e le d. T he y w il l n ee d a b ro ad ba nd l in k o r aC D ( n o t y e t g e n e ra t e d) .

  • 8/18/2019 Hands-On Python a Tutorial Introduction for Beginners

    6/139

    1.2. THE PYTHON INTERPRETER AND IDLE, PART I 6

    I n t h e F i r e f o x b r o w s e r , t h e i n c r e m e n t a l fi n d i s e x c e l l e n t , a n d p a r t i c u l a r l y u s e f u l w i t h t h e s i n g l e w e b p a g ev er s io n o f t h e t u to r ia l s. ( I t o n l y f a il s t o s e ar ch f o o tn o te s .) I t i s p a rt i cu l ar l y e a s y t o j u mp t h ro u gh d i ffe r en ts e c t i o n s i n a f o r m l i k e 1 . 2 . 4 .

    1.2. The Python Interpreter and Idle, Part I

    1.2.1. Your Python Folder and Python Examples.F i rs t y ou n e ed t o s e t u p a l o ca t io n t o s t or e y ou r w or k a n d t h e e x am p le p r og r am s f r o m t h is t u to r ia l . I f          

    y ou a r e o n a W i nd ow s c o mp u te r , f o ll ow j u st  one    o f t h e t h r e e c h o ic e s b e l o w t o fi n d a n a p pr o pr i at e p l ac e t od o wn l o ad t h e e x a mp l e a r c h i ve e x a m pl e s . zi p , a n d t h e n f o l l o w t h e l a t er i n s tr u c t io n s t o u n z i p t h e a r ch i ve .

    Your Own Computer:   I f y ou a re a t y ou r o wn c om pu te r, y ou c an p ut t he f ol de r f or yo ur P yt ho np r og r am s m o st a ny w he r e y ou l i ke . Fo r C h ap t er 4 , i t w il l b e i m p or t an t t h a t n o ne o f t h e d i re c to r ie sl e ad i ng d ow n t o y ou r P y th o n f o ld e r c o nt a in a ny b l an k s i n t h em . I n p a rt i cu l ar i n W i nd ow s , “ M yD o c u m en t s” i s a b a d l o c a t io n . I n W i n d o ws y o u c a n c r e a t e a d i r e ct o r y i n C : d r i v e , l i k e C : \ my P y t ho n .Yo u s h o u ld h a ve i n s ta l l ed P y t h on t o c o nt i nu e .

    Your Flash Drive:   I f y o u d o n o t h a v e y o u r o w n c o m p u t e r , o r y o u w a n t t o h a v e y o u r m a t e r i a l s e a s i l yt r av el b a ck a n d f o r t h b e t we e n t h e l a b a n d h o me , y ou w i ll n e ed a fl a sh d r iv e.

    P l ug y ou r fl a sh d r iv e i nt o t h e c o m p ut e r U SB p o r t.

    O n t h e c o m p u t e r s i n t h e L o y o l a l a b D H 3 4 2 , y o u c a n a t t a c h t o t h e e n d o f a c a b l e t h a t r e a c h e s c l o s et o t he ke yb o ar d. I n D H 3 3 9 , t h e re a re U SB p o rt s o n t he m on it or . P le as e N ot e: F la sh d ri ve s a ree a sy f o r m e t o f o r g et a n d l e av e i n t h e c o m pu t er . I h av e l o s t a f e w t h is w ay. I f y o u a r e a s f o r g et f ul a sI , y o u m i g h t c o n s i d e r a s t r i n g f r o m t h e fl a s h d r i v e t o s o m e t h i n g y o u w i l l n o t f o r g e t t o t a k e w i t h y o u .

    Open   My Computer    ( o n t he d es kt op ) t o s ee w he re t he fl as h d ri ve i s m ou nt ed , a nd o pe n t ha tdrive.

    Temporary:   I f y ou ( te mp o ra ri ly ) d o n ot h av e a fl as h d ri ve a nd y ou a re a t a L oy ol a l ab c om pu te r:Open   My Computer   f r o m t h e d e s k to p , a n d t h en s e le c t d r i ve  D: . C re at e a f ol de r o n d r i ve D : w i thy ou r n am e o r i ni ti al s t o m ake i t e as y f or y ou t o s av e a nd r em ov e t hi ng s. C ha ng e t o t ha t f ol de r.Yo u s h ou l d p l ac e t h e e x am p le s a r ch i ve h e re . Yo u w il l n e ed t o s av e y ou r w or k s o me h ow b e f or e y oul o g o ff o f t h e c o m p ut e r. Yo u m ay w an t t o e m ai l i n di v id u al fi l es t o y ou r se l f, o r r e zi p t h e e x a m pl e s

    f o ld e r a n d s e nd j u st t h e o n e a r ch i ve fi l e t o y ou r se l f e a c h t i m e u nt i l y o u r e me mb e r a fl a sh d r iv e!I n W i n d ow s , a f t e r y o u h a v e c h o s e n a l o c a t io n f o r t h e a r c hi v e, e x a mp l e s .z i p , d o w n lo a d i t b y right   c l i c k i n g o nh t t p : / / c s . l u c. e d u / a n h / p y t ho n / h a n d s - o n / 3 . 0 / e x a m pl e s . z i p   a n d s e le c ti n g “ S av e A s ” o r t h e e q ui va l en to n y ou r b r ow s er a n d t h e n n av i ga t e t o s av e t h e a r ch i ve t o t h e c h o se n l o c at i on o n y ou r c o mp u te r . N o te t h et h e e x a m pl e s, l i ke t h is v er s io n o f t h e t u t o ri a l, a r e f o r P y th o n 3 . 1 . T h er e w er e m a j or c ha n ge s t o P y th o n i nv e rs i o n 3 . 0 , m a k in g i t i n c om p a ti b l e w i t h e a r l ie r v e rs i o n s.

    I f y ou a r e u s i n g P y th o n v e r si o n 2 . 5 o r 2 . 6, y ou s h ou l d c o n ti nu e w it h t h e o l d e r v er s io n o f t h e t u to r ia l .G o t o h t t p : / / c s . l u c. e d u / ~ a n h / p y t ho n / h a n d s - o n   a n d fi n d t h e l i n ks t o t h e p r o p e r v e rs i on o f t h e t u t o ri a la n d e x a mp l e s .

    O n ce y ou h av e t h e a r ch i ve , o p e n a fi l e b r ow s er w in d ow f o r t h at d i re c to r y, r i gh t c l ic k o n e x am p le s .z i p,s e le c t E x t r ac t A ll . T h is w i ll c r ea t e t h e f o ld e r   e x a m p l e s . E nd u p w it h a fi le b ro ws er w in do w s h ow in g t hec o nt e nt s o f t h e e x a m pl e s f o ld e r. T h is w il l b e y ou r Python folder   i n l a t er d i s c us s i o n.

    Caution 1:   O n W in do ws , fi le s i n a z ip a rc hi ve c an b e v ie we d w hi le t he y a re s ti ll i n t he z ip a rc hi ve .M o di fy in g a nd a dd in g fi le s i s n ot s o t ra ns pa re nt . B e s ur e t ha t yo u u nz ip t he a rc hi ve a nd wo rk f ro m t her e g u la r d i r e ct o r y t h a t h o l ds t h e r e s u l t i n g u n z i p p e d fi l e s .

    Caution 2 : M a ke s u re t h at a l l t h e d i re c to r ie s l e ad i ng d ow n t o y ou r P y th o n e x a m pl e s d i r e ct o ry d o n o ti n c lu d e a n y spaces   i n t h em . T hi s w i ll b e i mp o r ta nt i n C h a p te r 4 f o r t h e l o c al w eb s er ve r . I n p a rt i cu l ar , t h a tm e a ns y o u s h o u l d not    p l a c e y o u r f o l d er u n d e r “ M y D o c u m en t s ”. A d i r e ct o r y l i k e C : \ ha n d s -o n o r C : \ py t h o nw ou l d b e fi n e.

    Yo u a l s o h a v e t h e o p t i on o f d o wn l o ad i n g

    •   A n a r c h i v e c o n t a i n i n g t h e w e b v e r s i o n o f t h e t u t o r i a l h t t p : / / c s . l uc . e d u / a n h / p y th o n / h a n d s - o n /3 . 0 / h a n d s o n H t m l . z i p   f o r l o c a l v i ew i ng , w it h ou t t h e I n t er n et . D ow n lo a d i t a n d u n z i p a s w i th t h e

  • 8/18/2019 Hands-On Python a Tutorial Introduction for Beginners

    7/139

    1.2. THE PYTHON INTERPRETER AND IDLE, PART I 7

    e x am p le s . T h e l o c a l fi l e t o o p e n i n y ou r b r ow s er i n i n h a nd s on H tm l f o l d er y ou u n zi p p ed a n d t h em a in w eb p a ge fi l e t o o p e n i s c a ll e d h a n d s on . ht m l.

    •   T h e P DF v er si on o f t he t ut or ia l f or p ri nt in g   h t t p : / / c s . l u c. e d u / a n h / p y t h on / h a n d s - o n / 3 . 0 /h a n d s o n . p d f.

    T h e d i s a dva nt a ge o f a l o ca l c o py i s t h at t h e t u t o ri a l m a y b e u p d at e d o n l i ne a f te r y ou g e t y o u r d ow n lo a d.T h e c h a n g e l o g fi l e h t t p : / / w w w . cs . l u c . e d u / ~ a nh / p y t h o n / h a n d s- o n / c h a n g e l o g . h t m l  w i l l s h o w w h e n t h e

    l a te s t u p d a t e w a s m a de a n d a s u mm a ry o f a ny m a jo r c ha n ge s .

    1.2.2. Running A Sample Program.T h is s e ct i on a s su m es P y th o n, v er s io n a t l e as t 3 . 1, i s a l re a dy o n y ou r c o mp u te r . W i nd ow s d o e s n o t c o me

    w it h P yt ho n. ( To l oa d P yt ho n s ee S ec ti on 1 .1 .2 ) O n a M ac o r L in ux c om pu te r e no ug h o f P yt ho n c om esi n st a ll e d t o b e a b le t o r u n t h e s a mp l e p r o g ra m.

    I f y ou a re i n a W in do ws l ab w it h P yt ho n 3 .1 i ns ta ll ed , b ut n ot s et u p a s t he d ef au lt v er si on , s ee t hefootnote.1

    B e fo r e g e tt i ng t o t h e i n di v id u al d e ta i ls o f P y th o n, y ou w i ll r u n a s i mp l e t e xt - ba s ed s a mp l e p r og r am .Find   m a d l i b . p y   i n y o u r P y t h on f o l de r ( S e c ti o n 1 . 2 .1 ) .

    O p t io n s f o r r u n n i n g t h e p r o g r a m :

    •   I n W in dow s, y ou c an d is pl ay yo ur f ol de r c on te nt s, a nd d ou bl e c li ck o n m ad li b. py t o s ta rt t heprogram.

    •   I n L inu x o r o n a M ac yo u c an o p en a t er mi na l w in dow , ch an ge i nt o y ou r p yt ho n d ir ec to ry, a nde n te r t h e c o m ma n d

    p y t h on m a d li b . p y

    T h e l a tt e r a p pr o ac h o n ly w or k s i n a W i nd ow s c o mm an d w in d ow i f y ou r o p e ra t in g s y st e m e x ec u ti o n p a th i ss e t u p t o fi n d P y t h o n .

    I n w h at e ve r m a nn e r y o u s t ar t t h e p r o g ra m , r u n i t , r e s p on d in g t o t h e p r o m pt s o n t h e s c r e en . B e s u re t op r es s t h e e n t er k ey a t t h e e n d o f e a ch r e sp o n se r e qu e st e d f r o m y ou .

    Tr y t h e p r og r am a s e co n d t i me a n d m a ke d i ffe r en t r e sp o n se s .

    1.2.3. A Sample Program, Explained.   I f y ou wa nt t o g et r ig ht t o t he d et ai le d e xp la na ti on s o f          w r it i ng y ou r o wn P y th o n, y ou c a n skip to the next section   1 . 2. 4 . I f y o u w ou l d l i ke a n o ve r vi e w o f a w or k in gp r og r am , e ve n i f a l l t h e e x pl a na t io n s d o n o t m a k e t o t a l s e n s e y e t , r e a d o n .

    H e re i s t h e t e xt o f t h e m a dl i b. py p r og r am , f o ll ow ed b y l i ne - by - li n e b r ie f e x pl a na t io n s. D o n o t w or r y i f          

    y ou n o t t o t a ll y u n de r st a nd t h e e x p l an a ti o ns ! Tr y t o g e t t h e g i st n ow a n d t h e d e ta i ls l a te r . T h e n u mb e r s o nt h e r i g ht a r e n o t p a rt o f t h e p r og r am fi l e. T he y a r e a d d e d f o r r e fe r en c e i n t h e c o m me nt s b e l ow .

    " " " 1

    S t ri ng S ub st i tu ti on f or a Ma d Li b 2

    A d ap te d fr om co de b y Ki r by U rn er 3

    " " " 4

    5

    s t or yF or ma t = " "" 6

    O n ce u po n a t im e, d ee p i n an a nc ie n t ju ng le , 7

    t h er e li ve d a { an im al }. T hi s {a ni m al } 8

    l i ke d to e at {f oo d} , bu t t he j un gl e h ad 9

    v e ry l it tl e { fo od } to o f fe r. On e d ay , an 10

    e x pl or er f ou n d th e {a ni m al } an d di s co ve re d 11i t l ik ed { fo o d} . The e x pl or er t oo k t he 12

    { a ni ma l} b ac k t o {c it y} , w he re i t c ou ld 13

    e a t as m uc h { fo od } as i t w an te d. H ow ev er , 14

    t h e {a ni ma l} be ca me h om e si ck , so t h e 15

    1If an earlier version of Python is the default in your lab (for instance Python 2,6), you can open the examples folder anddouble-click on the program   default31.cmd. This will make Python 3.1 be the default version until you log out or reboot. Thisis only actually important when you run a Python program directly from a Windows folder. You will shortly see how to start

    a program from inside the Idle interactive environment, and as long as you run all your programs inside that environment, thesystem default version is not important.

  • 8/18/2019 Hands-On Python a Tutorial Introduction for Beginners

    8/139

    1.2. THE PYTHON INTERPRETER AND IDLE, PART I 8

    e x pl or er b ro u gh t it b ac k t o th e ju n gl e, 16

    l e av in g a la r ge s up pl y o f {f oo d} . 17

    1 8

    T h e En d 19

    " " " 20

    2 1

    d e f te ll St or y () : 22us er Pi ck s = d ic t( ) 23

    ad dP ic k( ’ an im al ’, u s er Pi ck s) 24

    ad dP ic k( ’ fo od ’, u se r Pi ck s) 25

    ad dP ic k( ’ ci ty ’, u se r Pi ck s) 26

    st or y = s to ry Fo rm at . fo rm at (* *u s er Pi ck s) 27

    pr in t( st o ry ) 28

    2 9

    d e f ad dP ic k( c ue , di ct io n ar y) : 30

    ’ ’’ Pr om pt f or a u se r r es po ns e u si ng t he c ue s tr in g, 3 1

    a nd p la ce t he c ue -r es po ns e p ai r i n t he d ic ti on ar y. 3 2

    ’’ ’ 33

    pr om pt = ’E nt er an exa mp le for ’ + cu e + ’: ’ 34

    re sp on se = in pu t( pr o mp t) 35di ct io na r y[ cu e] = r e sp on se 36

    3 7

    t e ll St or y( ) 38

    i n pu t( "P re ss En te r to e n d th e pr og r am ." ) 39

    L i n e B y L i n e E x p l a n a ti o n

    " " " 1

    S t ri ng S ub st i tu ti on f or a Ma d Li b 2

    A d ap te d fr om co de b y Ki r by U rn er 3

    " " " 4

    1 -4 T he re is mu lt i- li ne te xt en cl os ed in tr ip le qu ot es . Q uo te d t ex t i s c al le d a  string . A s tr in g a t t hev er y b e g in n in g o f a fi l e l i ke t h is i s documentation   f o r t h e fi l e.

    5 , 21 , 29 , 37 B l an k l i ne s a r e i n cl u de d f o r h um a n r e ad a bi l it y t o s e pa r at e l o gi c al p a rt s . T h e c o mp u te r i g no r est h e b l a n k l i n es .

    s t or yF or ma t = " "" 6

    O n ce u po n a t im e, d ee p i n an a nc ie n t ju ng le , 7

    t h er e li ve d a { an im al }. T hi s {a ni m al } 8

    l i ke d to e at {f oo d} , bu t t he j un gl e h ad 9

    v e ry l it tl e { fo od } to o f fe r. On e d ay , an 10

    e x pl or er f ou n d th e {a ni m al } an d di s co ve re d 11

    i t l ik ed { fo o d} . The e x pl or er t oo k t he 12

    { a ni ma l} b ac k t o {c it y} , w he re i t c ou ld 13

    e a t as m uc h { fo od } as i t w an te d. H ow ev er , 14

    t h e {a ni ma l} be ca me h om e si ck , so t h e 15

    e x pl or er b ro u gh t it b ac k t o th e ju n gl e, 16l e av in g a la r ge s up pl y o f {f oo d} . 17

    1 8

    T h e En d 19

    " " " 20

    6 The equal sign tells the computer that this is an   assignment statement . T h e c o m pu t er w i ll n owa s s o c i a t e t h e v a l u e o f t h e e x p r e s s i o n b e t w e e n t h e t r i p l e q u o t e s , a m u l t i - l i n e  string , w i t h t h e n a m eo n t h e l e f t ,   s t o r y F o r m a t .

    7 -2 0 T he se li ne s c on ta in th e b o dy of th e s tr in g a nd th e e nd in g t ri pl e q uo te s. T hi s   s t o r y F o r m a t   stringc o nt a in s s o me s p e ci a l s y mb o l s m a ki n g i t a   format string , u nl ike t he s tr in g i n l in es 1 -4 . T he

  • 8/18/2019 Hands-On Python a Tutorial Introduction for Beginners

    9/139

    1.2. THE PYTHON INTERPRETER AND IDLE, PART I 9

    s t o r y F o r m a t   s tr i ng w il l b e u s ed l a te r t o p r ov i de a f o rm a t i nt o w hi ch s u bs t it u ti o ns a r e m a de .T h e p a r ts o f t h e s t r in g e n cl o se d i n b r ac e s a r e p l ac e s a s u bs t it u te s t ri n g w i l l b e i n se r te d l a te r . T h es u bs t it u te d s t ri n g w i ll c o me f r om a c u st o m dictionary   t h a t w il l c o nt a in t h e u s er ’ s d e fin i ti o ns o f          t h e s e w o rd s . T h e w o r ds i n t h e b r a c e s: { a n i ma l } , { f o o d } , { c i t y} , i n d i c a te t h a t   "animal" ,   " f o o d " ,and   " c i t y "   a r e w or d s i n a d i ct i on a ry. T h is c u st o m d i c t io n ar y w il l b e c r ea t ed i n t h e p r o g ra m a n dc o nt a in t h e u s er ’ s d e fin i ti o ns o f t h es e w or d s. T h es e u s er ’ s d e fin i ti o ns w il l b e s u bs t it u te d l a te r i n

    the   format string   w h e r e e a ch   { . . . }   is currently.d e f te ll St or y () : 22

    us er Pi ck s = d ic t( ) 23

    ad dP ic k( ’ an im al ’, u s er Pi ck s) 24

    ad dP ic k( ’ fo od ’, u se r Pi ck s) 25

    ad dP ic k( ’ ci ty ’, u se r Pi ck s) 26

    st or y = s to ry Fo rm at . fo rm at (* *u s er Pi ck s) 27

    pr in t( st o ry ) 28

    22   def    i s s ho rt f or   def i ni ti on . T hi s l in e i s t he h ea di ng o f a   def i n it i on , w h ic h m a ke s t h e n a met e l l S t o r y   becomes   def i ne d a s a s ho rt wa y t o r ef er t o t he s eq ue nc e o f s ta te me nt s t ha t s ta rti n de nt e d o n l i ne 2 3 , a n d c o nt i nu e t h ro u gh l i ne 2 7 .

    2 3 T he eq ua l s ig n t el ls th e c om pu te r t ha t t hi s i s a no th er as si gn me nt st at em en t. T h e c om pu te r w il l

    n ow a s so c i at e t h e n a me  userPicks   w i th a n ew e mp ty d ic ti on ar y c re at ed b y t he P yt ho n c o ded i c t ( ).

    24-26   a d d P i c k   i s t he n am e f or a s eq ue nc e o f i ns tr uc ti on s d efi ne d o n l in es 2 9- 31 f or a dd in g a no th erd efi ni ti on t o a d ic ti on ar y, b as ed o n t he u se r’ s i np ut . T he r es ul t o f t he se t hr ee l in es i s t o a ddd e fi n i t i o n s f o r e a c h o f t h e t h r e e w o r d s ’ a n i m a l ’ , ’ f o o d ’ , a n d ’ c i t y ’ t o t h e d i c t i o n a r y c a l l e d u s e r P i c k s .

    27 Assign the name   s t o r y   t o a s t ri n g f o rm e d b y s u bs t it u ti n g i nt o   s t o r y F o r m a t   u s i n g d e fi n it i o n sf r o m t h e d i c t io n a r y u s e r P i ck s , t o g i ve t h e u s e r ’s c u s t om i z ed s t o r y.

    2 8 Th is is wh er e a ll th e wo rk b eco me s v is ibl e: Pr int t he  story   s t r in g t o t h e s c re e n.

    d e f ad dP ic k( c ue , di ct io n ar y) : 30

    ’ ’’ Pr om pt f or a u se r r es po ns e u si ng t he c ue s tr in g, 3 1

    a nd p la ce t he c ue -r es po ns e p ai r i n t he d ic ti on ar y. 3 2

    ’’ ’ 33

    pr om pt = ’E nt er an exa mp le for ’ + cu e + ’: ’ 34

    re sp on se = in pu t( pr o mp t) 35

    di ct io na r y[ cu e] = r e sp on se 36

    3 0 Th is li ne is th e h ead in g of a d efi ni ti on , wh ich gi ve s t he na me  addPick   a s a s h o r t w a y t o r e f e r t ot h e s e qu e nc e o f s t at e me nt s i n de nt e d o n l i ne 3 4 -3 6 . T h e n a m e addPick   i s f o l lo we d b y t wo w or d si n p a r e nt h e s is ,   cue   and  dictionary . T he se t wo wo rd s a re a ss o ci at ed w it h a n a ct ua l c ue wo rda n d d i ct i on a ry g i ve n w he n t h is d e fin i ti o n i s i nv ok ed i n l i ne s 2 4 -2 6 .

    3 1- 33 A d oc um ent at io n c om me nt f or t he   a d d P i c k   definition.3 4 Th e pl us s ign h er e is u se d to c on ca ten ate p ar ts o f the s tr in g as si gn ed t o th e na me   p r o m p t . T he

    c u r r en t va l u e o f            c u e   i s p l ac e d i nt o t h e s t ri n g.3 5 T he r ig ht -h an d- si de o f th is e qu al s ig n ca us es a n int er ac ti on w it h th e us er . T he p ro mp t st ri ng

    i s p ri nt ed t o t he c om pu te r s cr ee n, a nd t he c om pu te r wa it s f or t he u se r t o e nt er a l in e o f t ex t.T h at l i ne o f t e xt t h en b e c om e s a s t ri n g i n s id e t h e p r o gr a m. T hi s s t ri n g i s a s si g ne d t o t h e n a m e

    r e s p o n s e.3 6 T he le ft -h an d- si de o f t he e qu al si gn is a re fe re nc e to t he de fin it io n of t he c ue wo rd i n t he d ic ti on ar y.

    T h e w h o l e l i n e e n d s u p m a k i n g t h e d e fi n i t i o n o f t h e c u r r e n t c u e w o r d b e c o m e t h e r e s p o n s e t y p e db y t h e u s er .

    t e ll St or y( ) 38

    i n pu t( "P re ss En te r to e n d th e pr og r am ." ) 39

    38 The definition of            t e l l S t o r y   a b ov e d o e s n o t m a ke t h e c o mp u te r d o a ny t hi n g b e s id e s remember w h a t t h e i n s t r u c t i o n   t e l l S t o r y   m e an s . I t i s o n ly i n t h i s l i n e, w it h t h e n a me ,   t e l l S t o r y, follow edb y p a r e n t he s e s , t h a t t h e w h o le s e q u en c e o f r e m em b e re d i n s t ru c t io n s a r e a c t u a l l y c a r r i e d o u t .

  • 8/18/2019 Hands-On Python a Tutorial Introduction for Beginners

    10/139

    1.2. THE PYTHON INTERPRETER AND IDLE, PART I 10

    3 9 T hi s l in e i s o nl y h er e t o a cc om mo d at e r un ni ng th e p ro gr am in Wi nd ow s by do ub le cl ic ki ng on it sfi l e i c o n . W i th o ut t h is l i ne , t h e s t o r y w ou l d b e d i sp l ay ed a n d t h e n t h e p r o g ra m w ou l d e n d , a n dW i nd ow s w ou l d m a ke i t i m me d ia t el y d i sa p p ea r f r om t h e s c re e n! T hi s l in e f o rc e s t h e p r og r am t oc o nt i nu e b e i ng d i sp l ay ed u nt i l t h er e i s a n ot h er r e sp o n se f r om t h e u s er , a n d m ea nw h il e t h e u s erm ay l o ok a t t he o ut pu t f ro m   t e l l S t o r y.

    1.2.4. Starting Idle.T h e p r og r am t h at t r an s la t es P y th o n i n st r uc t io n s a n d t h en e x ec u te s t h em i s t h e Python interpreter .T h is i nt e rp r et e r i s e mb e d de d i n a n um b er o f l a rg e r p r og r am s t h at m a ke i t p a rt ic u la r ly e a sy t o d e ve l op

    P y t h on p r o g ra m s . S u c h a p r o g ra m m in g e n vi r o n me n t i s  Idle , a n d i t i s a p a r t o f t h e s t a n d a r d d i s t r i b u t i o n o f          Python.

    R e ad t h e s e ct io n t h at f o ll ow s f o r y ou r o p e ra t in g s y st e m:

    W i nd ow s ( A ss u mi n g y o u a l re a dy h av e P y t h on i n st a ll e d. ) D i sp l ay y ou r P y th o n f o l d er . Yo u s h ou l d s e e i c onfor   I d l e 3 1 S h o r t c u t   ( a nd m ay b e a s i mi l ar i c on w i th a n um b er l a rg e r t h a n 3 1 - i g no r e a n y o t h e r

    u n l e ss y o u k n o w y o u a r e u s i n g t h a t v e r s io n o f P y t h on ) . D o u bl e c l i c k o n t h e a p p r o p ri a t e s h o r t cu t ,a nd a n I dl e w in do w s ho ul d a pp e ar . A ft er t hi s t he i ns tr uc ti on s a re t he s am e i n a ny o p er at in ge nv i ro n me nt . I t i s   important   t o s t ar t I d le t h ro u gh t h es e i n s e ve r al c i rc u ms t an c es .   It is best if it you make it a habit to use this shortcut.   Fo r e x am p le t h e a l te r na t iv e o f o p e ni n g a n e x is t in gP y t h o n p r o g r a m i n W i n d o w s X P o r V i s t a f r o m O p e n W i t h I d l e i n t h e c o n t e x t m e n u l o o k s l i k e i tw or k s a t fi r st b u t t h e n f a il s miserably   but   inexplicably   w h en y ou t r y t o r u n a g r ap h ic s p r og r am .

    M ac O S X t he n ew ve rs io n o f P yt ho n a nd I dl e s ho ul d b e i n a f ol de r c al le d M ac Py th on 3 .1 , i ns id e t heA p pl i ca t io n s f o l d er . I t i s b e s t i f y ou c a n o p e n a t e rm i na l w i nd ow , c ha n ge i nt o y ou r P y th o n f o l d erf r om S e ct i on 1 . 2. 1 , a n d e nt e r t h e c o mm a nd

    i d l e

    I f t h e c o m ma n d i s n o t r e c o gn i ze d , y o u m ay n e ed t o i n cl u de t h e f u l l p a th t o t h e i d l e p r og r am .L in ux T he a p p ro ac h d ep e nd s o n t h e i ns ta ll at io n. I n U bu nt u, y o u s ho ul d fi n e i dl e i n t h e P ro gr am mi ng

    s ec ti on o f t he A pp li ca ti on s m en u. A s w it h O S X a b ov e, y ou a re b e tt er s ta rt in g i dl e f ro m a

    t e r mi n a l , w i t h t h e c u r r e n t d i r e c t or y b e i n g y o u r P y t h on f o l d er .

    1.2.5. Windows in Idle.   I d le h a s s e ve r al p a rt s y ou m ay c ho o s e t o d i sp l ay, e a ch w it h i t s o wn w in d ow .D ep e nd in g o n t he c on fig ur at io n, I dl e c an s ta rt u p s ho wi ng e it he r o f two w in do ws , a n E di t W in dow o r aP y th o n S h el l W i nd ow . Yo u a r e l i k el y t o fi r st s e e a n E d it w i nd ow , w h os e t o p l e ft c o rn e r l o o k s s o me t hi n g l i k ei n W i n do w s:

    Fo r m o re o n t h e E d it W i nd ow , s e e S e ct i on 1 .9 .I f yo u s ee t hi s E di t W in dow w it h i ts Ru n m enu o n t op , g o t o t he Ru n m enu a nd ch oo se P YT HO N

    S H EL L t o o p e n a P y th o n S h e ll W i nd ow f o r n o w. T h en y ou m ay c l os e t h e E d i t W i n d ow .E i t h e r i n i t i a l l y , o r a f t e r e x p l i c i t l y o p e n i n g i t , y o u s h o u l d n o w s e e t h e P y t h o n S h e l l w i n d o w , w i t h a m e n u

    l i ke t h e f o l l o wi n g , t h o u g h t h e t e x t m a y b e s l i gh t ly d i ff e re n t:

  • 8/18/2019 Hands-On Python a Tutorial Introduction for Beginners

    11/139

    1.3. WHIRLWIND INTRODUCTION TO TYPES AND FUNCTIONS 11

    L o o k a t t h e P y t ho n S h e l l. . . .I n t h e S h e ll t h e l a s t l i n e s h o u ld l o o k l i ke

    > > >

    T he   >> >   i s t he  prompt , t e ll i ng y ou I d le i s w ai t in g f o r y o u t o t yp e s o me t hi n g. C o nt i nu i ng o n t h e s a m e l i neenter

    6 + 3

    B e s u re t o e n d w i t h t h e E nt e r k e y. A ft e r t h e S h el l r e sp o n ds , y ou s h ou l d s e e s o me t hi n g l i k e

    > > > 6+ 3

    9> > >

    T he s he ll e va lu at es t he l in e y ou e nt er ed , a nd p ri nt s t he r es ul t. Yo u s ee P yt ho n d o es a ri th me ti c. At t hee n d y o u s e e a f u rt h er p r om p t   > > >   w h er e y ou c a n e nt e r y o u r n e xt l i ne....   T h e r e s u lt l i n e , s h o w in g   9, t h a t i sp r o du c ed b y t h e c o mp u te r , d o e s n o t s t ar t w it h “ > >> ”.

    1.3. Whirlwind Introduction To Types and Functions

    P y th o n d i r e ct l y r e c o gn i ze s a va r ie ty o f t yp e s o f d a ta . H e re a r e a f e w:

    N umb e rs : 3 , 6 , - 7, 1 . 25C haracter strings:   ’ h e l l o ’,   ’ Th e a nsw er is : ’L i st s o f o b j ec t s o f a ny t yp e :   [ 1, 2 , 3 , 4] ,   [ ’y e s’ , ’ n o’ , ’ m ay b e’ ]A s p e c i al d a t um m e a ni n g n o t h in g :   None

    P y th o n h a s l a rg e c o ll e ct i on o f b u il t -i n f u nc t io n s t h at o p e ra t e o n d i ffe r en t k i nd s o f d a ta t o p r o du c e a l l k i n d so f r es ul ts . To m ak e a f un ct io n d o i ts a ct io n, p ar en th es es a re r eq ui re d. T he se p ar en th es es s ur ro un d t hep a ra m et e r o r p a ra m et e rs , a s i n a f u nc t io n i n a l ge b ra c l as s .

    T h e g e n e ra l s y nt a x t o e x ec u te a f u nc t io n i s

    functionName   (   parameters   )

    O n e f u n c t i o n i s c a l l e d   t y p e, a n d i t r e tu r ns t h e t y p e o f a ny o b j ec t . T h e P y t h on S h el l w il l e va l ua t e f u n c ti o ns .I n t h e S h e ll t h e l a s t l i n e s h o u ld l o o k l i ke

    > > >

    C o nt i nu i ng o n t h e s a me l i ne e nt e r

  • 8/18/2019 Hands-On Python a Tutorial Introduction for Beginners

    12/139

    1.4. INTEGER ARITHMETIC 12

    t y p e ( 7 )

    A l wa ys r e me mb e r t o e n d w it h t h e E n t er k ey. A ft e r t h e S h e l l r e s p o nd s , y ou s h ou l d s e e s o me t hi n g l i k e

    > > > t y pe ( 7)

    < c l a ss ’ i n t’ >

    > > >

    I n t h e r e s u l t ,   i n t   i s s h or t f o r i n t eg e r. T h e w o rk class   i s b a si c al l y a s y no ny m f o r t yp e i n P y t h on . A t t h e e n dy ou s e e a f u rt h er p r om p t w h e re y ou c a n e n t er y ou r n e xt l i ne....For the rest of this section , a t t h e   > >>   p r om p t i n t h e P y t h on  Shell , i n di v id u al l y e nt e r e a ch l in e b e l ow

    t h a t i s s e t o ff i n   t y p e w r i t e r   f o nt . S o n e xt e nt e r

    t y p e ( 1 . 2 5 )

    N ot e t he n am e i n t he l as t r es ul t i s   f l o a t , n o t r e a l o r d e ci m al , c o mi n g f r o m t h e t e r m “ fl oa t in g p o i nt ” , f o rr e as o ns t h at w i ll b e e x pl a in e d l a te r , i n S e ct i on 1 . 14 . 1. E nt e r

    t y p e ( ’ h e l l o ’ )

    I n y ou r l a st r e su l t y ou s e e a n ot h er a b br e vi a ti o n:   s t r   r at h e r t h a n s t r in g . E n te r

    t yp e( [1 , 2 , 3 ])

    S t r i ng s a n d l i s t s a r e b o t h s e q u en c e s o f p a r t s ( c ha r a ct e r s o r e l e me n ts ) . We c a n fi n d t h e l e n gt h o f t h a t s e q u e nc ew i t h a n o t h e r f u n c t i on w i t h t h e a b b r ev i a te d n a m e   l e n. Tr y b o t h o f t h e f o ll ow i ng , s e pa r at e ly, i n t h e Shell :

    l en ([ 2, 4 , 6 ])

    l e n ( ’ a b c d ’ )

    S o me f u nc t io n s h av e n o p a ra m et e rs , s o n o th i ng g o e s b e tw ee n t h e p a re nt h es e s. Fo r e x am p le , s o me t yp e ss e rv e a s n o -p a ra m et e r f u nc t io n s t o c r ea t e a s i mp l e va l ue o f t h ei r t yp e . Tr y

    l i s t ( )

    Yo u s e e t h e w a y a n e m pt y l i s t i s d i sp l ay ed .Fu n ct i on s m ay a l so t a ke m or e t h an o n e p a ra m et e r. Tr y

     m a x ( 5 , 1 1 , 2 )

    A b ov e, m a x i s s h or t f o r m a xi mu m .S om e o f t he n am es o f ty p es s er ve a s c on ve rs io n f un ct io ns ( wh er e t he re i s a n o bv io us m ea ni ng f or t he

    c o nv er s io n ). Tr y e a ch o f t h e f o l l ow i ng , o n e a t a t i me , i n t h e Shell :

    s t r ( 2 3 )i n t ( ’ 1 2 5 ’ )

    An often handy Shell feature : a n e ar li er S he ll l in e m ay t o c op ie d a nd e di te d b y c li ck in g a ny wh er e i n t hep r ev i ou s ly d i sp l ay ed l i ne a n d t h en p r es s in g   Enter. Fo r i ns ta nc e y ou s ho ul d h av e e nt er ed s ev er al l in ess t a r ti n g w i t h   l e n. c li ck o n a ny o ne , p r e ss  Enter, a nd e di t t he l in e f or a d iff er en t t e s t.

    1.4. Integer Arithmetic

    1.4.1. Addition and Subtraction.   W e s t ar t w i th t h e i nt e ge r s a n d i nt e ge r a r it h me t ic , n o t b e c au s ea r it h me t ic i s e x ci t in g , b u t b e c au s e t h e s y mb o l is m s h ou l d b e m o st l y f a mi l ia r . O f c o ur s e a r it h me t ic i s i mp o r -t a nt i n m a ny c a se s , b u t P y th o n i s p r ob a bl y m o re o f te n u s ed t o m a ni p ul a te t e xt a n d o t h e r s o r t s o f d a ta , a si n t h e s a mp l e p r og r am i n S e ct i on 1 . 2. 2 .

    P y t h on u n d e rs t a n ds n u mb e r s a n d s t a n da r d a r i th m e t ic .   For the whole section on integer arithmetic ,w he re yo u s ee a s et -o ff l in e i n   t y p e w r i t e r   f o nt , ty p e i nd iv id ua l l in es a t t he > >> p ro mp t i n t he P yt ho nShell . P r es s E nt e r a f t er e a ch l i ne t o g e t P y t ho n t o r e sp o n d:

    7 7

    2 + 3

    5 - 7

    P y th o n s h o u ld e va l ua t e a n d p r in t b a c k t h e va l ue o f e a ch e x pr e ss i on . O f c o ur s e t h e fi r st o n e d o e s n o t r e q u ir ea ny c a lc u la t io n . I t a p p e a rs t h e s h e l l j u s t e c h o es b a ck w h at y ou p r in te d . D o n o te t h at t h e l i n e w i t h t h e va l ueproduced   b y t he s he ll d o es n ot s ta rt w it h > >> a nd a pp e ar s a t t he l ef t m ar gi n. H en ce y ou c an d is ti ng ui shw h at y ou t yp e ( a ft e r t h e “ > >> ” p r om p t) f r om w h at t h e c o mp u te r r e sp o n ds .

  • 8/18/2019 Hands-On Python a Tutorial Introduction for Beginners

    13/139

    1.4. INTEGER ARITHMETIC 13

    T h e P y t h on S h el l i s a n i nt e ra c ti ve i nt e rp r et e r. A s y ou c a n s e e, a f te r y ou p r es s E nt e r, i t i s e va l ua t in gt h e e x pr e ss i on y ou t yp e d i n , a n d t h en p r in ti n g t h e r e su l t a u to m at i ca l ly. T h is i s a v er y h a nd y e nv i ro n me ntt o c he ck o u t s i mp l e P y th o n s y nt a x a n d g e t i n st a nt f e ed b ac k. Fo r m o re e l ab o r at e p r og r am s t h at y ou w an tt o s av e, w e w i l l s w it ch t o a n E d it o r W in d ow l a te r .

    1.4.2. Multiplication, Parentheses, and Precedence.   T ry i n t h e  Shell :

    2 x 3Y o u s h o u l d g e t y o u r fi r s t syntax   e r r o r . T h e ’ x ’ s h o u ld h a ve b e c o m e h i g h l ig h te d , i n d i c a t in g t h e l o c a t i o n w h e r et h e P y th o n i nt e rp r et e r d i sc ov er e d t h at i t c a nn o t u n de r st a nd y ou : P y th o n d o e s n o t u s e x f o r m ul t ip l ic a ti o na s yo u m ay h av e d o ne i n g r a de s ch o ol . T he x c an b e c on fu se d w it h t h e u se o f x a s a va ri ab le ( mo re o n t h a tl a te r ). I n st e ad t h e s y mb o l f o r m ul t ip l ic a ti o n i s a n a s te r is k ’ * ’. E nt e r e a ch o f t h e f o ll ow in g . Yo u m ay i n cl u des p ac e s o r n o t. T h e P y t h on i nt e rp r et e r c a n fi g ur e o u t w h a t y ou m e an e i th e r w a y. Tr y i n t h e Shell :

    2 * 5

    2 + 3 * 4

    I f y o u e x p ec t ed t h e l a s t a n s we r t o b e 2 0 , t h i nk a g ai n : P y th o n u s e s t h e n o rm a l precedence   o f a r i th m e ti c o p e r -a t i on s : M u l ti p l ic a t io n s a n d d i v is i o n s a r e d o n e b e f o r e a d d i ti o n a n d s u b t r a c t io n , u n l e ss t h e r e a r e p a r e nt h e s es .Try

    ( 2 + 3 ) * 4

    2 * ( 4 - 1 )N ow t r y t h e f o l l ow in g i n t h e Shell , e x ac t ly a s w r it t en , f o ll ow ed b y E nt e r, w i th  no   closing parenthesis:

    5 * (2 + 3

    L o ok c ar ef ul ly. T he re i s n o a ns we r g ive n a t t he l ef t m ar gi n o f t he n ex t l in e a nd n o p ro mp t > >> t o s ta rt anew    e x pr es si on . I f y ou a re u si ng I dl e, t he c ur so r h as g on e t o t he n ex t l in e a nd h as o nl y  indented    slightly.P y th o n i s w ai t in g f o r y o u t o fi n is h y ou r e x pr e ss i on . I t i s s m ar t e n ou g h t o k n ow t h at o p e ni n g p a r e nt h es e sa r e a l w ay s f o ll ow ed b y t h e s a me n um b er o f c l os i ng p a re nt h es e s. T h e c u r s or i s o n a   continuation   l i ne . T yp e

        just the matching close-parenthesis and Enter,

    )

    a n d y ou s h ou l d fi n al ly s e e t h e e x pr e ss i on e va l ua t ed . ( I n s o me v er s io n s o f t h e P y th o n i nt e rp r et e r, t h e i nt e r-p r e t er p u t s ’. . .’ a t t h e b e g i n ni n g o f a c o nt i nu a t io n l i n e, r a t h er t h a n j u s t i n d e nt i n g. )

    N e ga t io n a l so w or k s. Tr y i n t h e Shell :

    - ( 2 + 3)

    1.4.3. Division and Remainders.   I f yo u t hi nk a b ou t i t, y ou l ea rn ed s ev er al wa ys t o d o d iv is io n.E ve nt u al l y y o u l e ar n ed h ow t o d o d i vi s io n r e su l ti n g i s a d e ci ma l . Tr y i n t h e Shell :

    5 / 2

    1 4 / 4

    A s y ou s aw i n t h e p r e v io u s s e c t io n , n u mb e r s w i t h d e c im a l p o i nt s i n t h em a r e o f t yp e fl o at i n P y t h on . T h eya r e d i sc u ss e d m o re i n S e ct io n 1 . 14 . 1.

    I n t he e ar li es t g ra de s y ou wo ul d s ay “ 14 d iv id ed b y 4 i s 3 w it h a r em ai nd er o f 2 ”. T he p ro bl em h er e i st h a t t h e a n s w e r i s i n t w o p a r t s , t h e i n t e g e r q u o t i e n t 3 a n d t h e r e m a i n d e r 2 , a n d n e i t h e r o f t h e s e r e s u l t s i s t h es a me a s t h e d e c i ma l r e su l t. P y th o n h a s s e pa r at e o p e ra t io n s t o g e ne r at e e a ch p a rt . P y th o n u s e s t h e d o u b le dd i v is i o n s y mb o l   / /   f o r t h e o p e ra t io n t h at p r o du c es j u st t h e i nt e ge r q u ot i en t, a n d i nt r o du c es t h e s y mb o l   %f o r t h e o p e ra t io n o f fi n di n g t h e r e ma i nd e r. Tr y e a ch i n t h e Shell 

    1 4 / 4

    1 4 / / 4

    1 4 % 4

    N ow p r ed i ct a n d t h e n t r y e a ch o f          

    2 3 / / 5

    2 3 % 5

    2 0 % 5

    6 / / 8

    6 % 8

  • 8/18/2019 Hands-On Python a Tutorial Introduction for Beginners

    14/139

    1.5. STRINGS, PART I 14

    F i nd i ng r e ma i nd e rs w i ll p r ov e m o re u s ef u l t h an y ou m i gh t t h in k i n t h e f u tu r e!

    1.5. Strings, Part I

    E n ou g h w i th n um b er s f o r a w hi l e. S t ri n gs o f c ha r ac t er s a r e a n ot h er i m p or t an t t yp e i n P y th o n.

    1.5.1. String Delimiters, Part I.   A s tr in g i n P yt ho n i s a s eq ue nc e o f ch ar ac te rs . Fo r P yt ho n t or e c o g n i z e a s e q u e n c e o f c h a r a c t e r s , l i k e   h e l l o , a s a s t r i n g , i t m u s t b e e n c l o s e d i n q u o t e s t o d e l i m i t t h e s t r i n g .For this whole section on strings , c o nt i nu e t r yi n g e a c h s e t - off l i ne o f c o d e i n t h e Shell . Tr y

    "h e l l o"

    N o te t h at t h e i nt e rp r et e r g i ve s b a ck t h e s t ri n g w it h single   q u ot e s. P y th o n d o e s n o t c a r e w ha t s y st e m y o uu s e . Tr y

    ’ H i ! ’

    H av i ng t h e c ho i ce o f d e li m it e rs c a n b e h a nd y.

    Exercise   1.5.1.1.   * F i gu r e o u t h ow t o g i ve P y th o n t h e s t ri n g c o n ta i ni n g t h e t e xt :   I ’m h a pp y .   T ry i t .I f y ou g ot a n e rr or , t ry i t w it h a no th er ty p e o f q uo te s, a nd fi gu re o ut w hy t ha t o ne wo rk s a nd n ot t he fi rs t.

    T h e re a r e m a n y v ar i a t io n s o n d e l im i t in g s t r in g s a n d e m b ed d i n g s p e c i a l s y m b o l s. We w i l l c o n s i de r m o r e

    w ay s l a te r i n S e ct i on 1 . 8.A s tr in g c an h av e a ny n um be r o f c ha ra ct er s i n i t, i nc lu di ng 0 . T he e mp ty s tr in g i s   ’ ’   ( t wo q u o t e

    c h ar a c t er s w i t h n o t h in g b e t we e n t h e m) .S t ri n gs a r e a n e w P y t ho n t yp e . Tr y

    t y p e ( ’ d o g ’ )

    t y p e ( ’ 7 ’ )

    t y p e ( 7 )

    T h e l a s t t w o l i n e s s h o w h o w e a s i l y y o u c a n g e t c o n f u s ed ! S t r i ng s c a n i n c l ud e a n y c h ar a c t er s , including   digits.Q u ot e s t u rn e ve n d i gi t s i nt o s t ri n gs . T h is w il l h av e c o ns e qu e nc e s i n t h e n e xt s e ct i on . .. .

    1.5.2. String Concatenation.   S t r i n g s a l s o h a v e o p e r a t io n s y m b o l s . Tr y i n t h e Shell   ( n o t i n g t h e space after   very ):

    ’ ver y ’ + ’h ot’

    T h e p l u s o p e r a t i o n w i t h s t r i n g s m e a n s  concatenate   t h e s t r i ng s . P y th o n l o o k s a t t h e t y p e o f o p e ra n ds b e f or ed e ci d in g w ha t o p e ra t io n i s a s so c i at e d w i th t h e + .

    T h in k o f t h e r e la t io n o f a d di t io n a n d m ul t ip l ic a ti o n o f i nt e ge r s, a n d t h en g u es s t h e m e an i ng o f          

    3 *’ ve ry ’ + ’ ho t’

    We r e y ou r i gh t? T h e a b i l it y t o r e p ea t y ou r se l f e a s i ly c a n b e h a nd y.

    Exercise   1.5.2.1.   * F ig ur e o u t a  compact    w a y t o g e t P y t h on t o m a ke t h e s t r i ng , “ Ye s Ye s Ye s Ye s Ye s ” ,a n d t r y i t . H o w a b o u t “ M ay b e M ay b e M ay b e Ye s Ye s Ye s Ye s Ye s ” ? H i nt :   2

    P r ed i ct t h e f o ll ow i ng a n d t h en t e st . R em e mb e r t h e l a s t s e ct i on o n t yp e s :

    7 + 2

    ’ 7 ’ + ’ 2 ’P y th o n c h e ck s t h e t y p e s a n d i nt e rp r et s t h e p l u s s y mb o l b a se d o n t h e t y p e . Tr y

    ’ 7 ’ + 2

    W i th m i xe d s t ri n g a n d i nt t yp e s , P y th o n s e e s a n a mb i gu o us e x pr e ss i on , a n d d o e s n o t g u es s w h ic h y ou w an t– i t j u s t g i v e s a n e r r o r !   3

    2Hint for the second one: use two *’s and a +.3Be careful if you are a Java programmer! This is unlike Java, where the 2 would be automatically converted to ’2’ so the

    concatenation would make sense.

  • 8/18/2019 Hands-On Python a Tutorial Introduction for Beginners

    15/139

    1.6. VARIABLES AND ASSIGNMENT 15

    1.6. Variables and Assignment

    Each set-off line in this section should be tried in the Shell.Try

    w idt h = 10

    N o th i ng i s d i sp l ay ed b y t h e i nt e rp r et e r a f te r t h is e nt r y, s o i t i s n o t c l ea r a ny t hi n g h a pp e n ed . S o me t hi n g h a s

    h a pp e n ed . T h is i s a n   assignment statement , w i t h a  variable ,   w i d t h , o n t h e l e ft . A va ri ab le i s a n am e f o r ava l ue . A n a s si g nm en t s t a t em en t a s s o c ia t es a va r ia b le n a me o n t h e l e f t o f t h e e q u a l s i g n w i th t h e v a lu e o f a ne x pr e ss i on c a lc u la t ed f r om t h e r i gh t o f t h e e q ua l s i gn . E nt e r

    w i d t h

    O nc e a va ri ab le i s a ss ig ne d a va lu e, t he va ri ab le c an b e u se d i n p la ce o f t ha t va lu e. T he r es p on se t o t heexpression   w i d t h   i s t he s am e a s i f i ts va lu e h ad b e en e nt er ed .

    T h e i n t e r p r e t e r d o e s n o t p r i n t a v a l u e a f t e r a n a s s i g n m e n t s t a t e m e n t b e c a u s e t h e v a l u e o f t h e e x p r e s s i o no n t h e r i g ht i s n o t l o s t. I t c a n b e r e co ve r ed i f y o u l i ke , b y e n t er i ng t h e v a ri a bl e n a m e a n d w e d i d a b o ve .

    Tr y e a ch o f t h e f o ll ow in g l i ne s :

    h eig ht = 1 2

    a re a = w id th * h ei gh t

    a r e a

    T h e e q u a l s i g n i s a n u n f o r t u n a t e c h o i c e o f s y m b o l f o r a s s i g n m e n t , s i n c e P y t h o n ’ s u s a g e i s n o t t h e m a t h e m a t i -c a l u s a ge o f t h e e q u a l s i g n. I f t h e s y m b ol ←h a d a p p e a r e d o n k e y b o a r d s i n t h e e a r l y 1 9 9 0 ’ s , i t w o u l d p r o b a b l yh av e b e e n u s ed f o r a s si g nm en t i n st e ad o f = , e m ph a si z in g t h e a s ym me t ry o f a s si g nm e nt . I n m a th e ma t ic s a ne q ua t io n i s a n assertion   that  both   s i de s o f t he e qu al s ig n are already, in fact, equal . A P y th o n a s s ig n me ntstatement   forces   t h e va r ia b le o n t h e l e f t h a nd s i de  to become   a s s o ci a te d w it h t h e va l ue o f t h e e x pr e ss i on o nt h e r i gh t s i de . T h e d i ffe r en c e f r om t h e m a th e ma t ic a l u s ag e c a n b e i l lu s tr a te d . Tr y :

    1 0 = w id th

    s o t hi s i s n ot e qu iva le nt i n P yt ho n t o   wi dt h = 1 0. The   left hand    s i de mu st b e a va ri ab le , t o w hi ch t hea s s i gn m e nt i s m a d e. Tr y

    w i dt h = wi dt h + 5

    T h is i s , o f c o ur s e, n o ns e ns i ca l a s m a th e ma t ic s , b u t i t m a ke s p e r fe c tl y g o o d s e ns e a s a n a s si g nm e nt , w it h t h e

    r i gh t- h an d s i de c a lc u la t ed fi r st . C a n y o u fi g ur e o u t t h e va l ue t h at i s n ow a s so c i at e d w i t h w id t h? C h ec k b yentering

    w i d t h

    I n t h e a s s i g n m e n t s t a t e m e n t , t h e e x p r e s s i o n o n t h e r i g h t i s e v a l u a t e d  first . A t t ha t p o i nt   w i d t h   w as associatedw i th i t s o r ig i na l va l ue 1 0 , s o   wid th + 5   h ad t he va lu e o f 1 0 + 5 w hi ch i s 1 5. T ha t va lu e wa s t he n a ss ig ne dt o t he va ri ab le o n t he l ef t (w i d t h   a ga in ) t o g i ve i t a  new   v al u e. We w il l m o d if y t h e v a lu e o f va r ia b le s i n asimilar w ay routinely.

    A s s i gn m e nt a n d v ar i a bl e s w o rk e q u a ll y w e ll w i t h s t r i n g s . Tr y :

    f ir st = ’ Su e’

    l as t = ’ Wo ng ’

    n a me = f ir st + ’ ’ + la s t

    n a m e

    Try entering:

    f ir st = f re d

    N o t e t h e d i ff e r e nt f o r m o f t h e e r r o r m e s s ag e . T h e e a r l i er e r r o r s i n t h e s e t u t o ri a l s w e r e syntax   e r r or s : e r r or s i nt r an s la t io n o f t h e i n s t ru c ti o n. I n t h is l a st c a se t h e s y n ta x w as l e ga l , s o t h e i n t er p re t er w en t o n t o e x ec u te t h ei n s t ru c t io n . O n l y then   d i d i t fi n d t h e e r ro r d e sc r ib e d . T h er e a r e n o q u ot e s a r o u nd   fred , s o t h e i nt e rp r et e rassumed   fred   w as a n i d en ti fi er , b u t t h e n a me f r ed w as n o t d e fi n ed a t t h e t i m e t h e l i n e w as e x ec u te d .

    I t i s e as y t o f or ge t q uo te s w he re y ou n ee d t he m a nd p ut t he m a ro un d a va ri ab le n am e t ha t s ho ul d n oth a ve t h e m !

    T r y i n t h e  Shell :

  • 8/18/2019 Hands-On Python a Tutorial Introduction for Beginners

    16/139

    1.7. PRINT FUNCTION, PART I 16

    f r ed = ’ F re d er i ck ’

    f ir st = f re d

    f i r s t

    Now   f r e d, w it h ou t t h e q u ot e s, m a ke s s e ns e .T h er e a r e m o r e s u bt l et i es t o a s si g nm en t a n d t h e i d e a o f a va r ia b le b e i ng a “ n am e f o r” a va l ue , b u t w e

    w il l wo rr y a b ou t t he m l at er , i n S ec ti on 2 .4 .6 . T he y d o n ot c om e u p i f o ur va ri ab le s a re j us t n umb e rs a nd

    strings.Autocompletion: A handy short cut.   P y t h o n r e m e m b e r s a l l t h e v a r i a b l e s y o u h a v e d e fi n e d a t a n y m o m e n t .

    T h is i s h a nd y w h en e d it i ng . W i th o ut p r es s in g E nt e r, t yp e i nt o t h e S h e l l j u s tf     T hen  hold down    t h e A lt k ey a nd p re ss t he ’ /’ k ey. T hi s k ey c om bi na ti on i s a bb re vi at ed A lt -/ . Yo u

    s h ou l d s e e   f   a u t o co m pl e te d t o b e   f i r s t . T hi s i s p ar ti cu la rl y u se fu l i f y ou h av e l on g i de nt ifi er s! Yo u c anp r es s A lt - / s e ve r al t i me s i f m o re t h an o n e i d en ti fi er s t ar t s w it h t h e i n it i al s e qu e nc e o f c ha r ac t er s y ou t yp e d .I f y ou p r es s A l t- / a g ai n y ou s h ou l d s e e   f r e d . B a ck s pa c e a n d e d i t s o y ou h av e   f i, a n d t h e n a n d p r e s s A l t - /a g ai n . Yo u s h ou l d n o t s e e f r e d t h i s t i me , s i n c e i t d o e s n o t s t ar t w it h   f i .

    1.6.1. Literals and Identifiers.   E x p r es s i o ns l i ke   27   or   ’ h e l l o ’   a r e c a l le d   literals , c om in g f ro m t hef a c t t h a t t h e y  literally   m e a n e x ac t ly w h at t h ey s ay. T h ey a r e d i st i ng u is h ed f r om va r ia b le s , w h o va l ue i s not d i r e ct l y d e t e rm i n ed b y t h e ir n a m e.

    T h e s e q u en c e o f c ha r ac t er s u s ed t o f o rm a va r ia b le n a me ( a nd n a me s f o r o t h e r P y th o n e nt i ti e s l a te r ) i sc a l le d a n identifier . I t i d en ti fi es a P y th o n va r ia b le o r o t he r e nt i ty.

    T h er e a r e s o me r e st r ic t io n s o n t h e c ha r ac t er s e qu e nc e t h at m a ke u p a n i d en ti fi er :

    •   T he c ha ra ct er s mu st a ll b e l et te rs , d ig it s, o r u nd er sc or es ’ _’ , a nd m us t s ta rt w it h a l et te r. I np a r t ic u l ar , p u n c tu a t i on a n d b l a n ks a r e n o t a l l ow ed .

    •   T he re a re s om e wo rd s t ha t a re  reserved    f o r s p ec ia l u se i n P yt ho n. Yo u m ay n ot u se t he se wo rd sa s y ou r o wn i d en ti fi er s . T h ey a r e e a sy t o r e co g ni z e i n I d le , b e c au s e t h ey a r e a u to m at i ca l ly c o lo r edo r an g e. Fo r t h e c u r i ou s , y ou m ay  read    t h e f u ll l i st :

    F a ls e c la s s f in a ll y is re tu rn

    N o ne c on t in ue f or la mb d a tr y

    T r ue d ef f ro m no nl o ca l wh il e

    a n d d el g lo b al no t wi th

    a s e li f i f or yi el da s se rt e ls e i mp o rt pa ss

    b r ea k e xc e pt i n ra is e

    T h er e a r e a l s o i d en ti fi er s t h at a r e a u to m at i ca l ly d e fin e d i n P y th o n, a n d t h at y ou c o ul d r e de fi ne , b u t y oup r ob a bl y s h ou l d n o t u n le s s y o u r e al l y k n o w w h a t y o u a r e d o i n g! W h en y ou s t ar t t h e e d i to r , w e w il l s e e h o wI d le u s es c o lo r t o h e lp y ou k n ow w ha t i d en ti fi es a r e p r ed e fin e d.

    P y t h on i s c a s e s e n s it i ve : T h e i d e n t ifi e r s   l a s t,   L A S T , a n d   L a S t   a r e a l l d i ffe r en t. B e s u re t o b e c o ns i st e nt .U s i ng t h e A l t -/ a u t o- c o mp l e t io n s h o r tc u t i n I d l e h e l p s e n s u re y o u a r e c o n s is t e nt .

    W ha t i s l eg al i s d is ti nc t f ro m w ha t i s c onv en ti on al o r g o od p ra ct ic e o r r ec om me nd ed . M ea ni ng fu ln am es f or va ri ab le s a re i mp o rt an t f or t he h um an s w ho a re l o ok in g a t p ro gr am s, u nd er st an di ng t he m,a nd r ev is in g t he m. T ha t s om et im es m ea ns yo u wo ul d l ike t o u se a n am e t ha t i s m or e t ha n o ne wo rdl o n g , l i ke   pr i ce a t o p en i ng, b ut b la nk s a re i ll eg al ! O ne p o o r o p t io n i s j us t l ea vi ng o ut t he b la nk s, l ikep r i c e a t o p e n i n g. T h en i t m ay b e h a rd t o fi g ur e o u t w h e re w or d s s p l it . Tw o p r a ct i ca l o p ti o ns a r e

    •   u n d e rs c o r e s e p a r at e d : p u t ti n g u n d e r s c or e s ( w h i ch a r e l e g a l ) i n p l a c e o f t h e b l a nk s , l i k e   p r i c e _ a t _ o p e n i n g.•   using  camelcase : o m it t in g s p ac e s a n d u s in g a l l l ow er c as e , e x ce p t c a pi t al i zi n g a l l w or d s a f te r t h e

    fi r s t , l i ke   p r i c e A t O p e n i n g

    U s e t h e c ho i ce t h at fi t s y o u r t a s t e ( o r t h e t a s t e o r c o nv en ti o n o f t h e p e o p l e y o u a r e w o r ki n g w i t h) .

    1.7. Print Function, Part I

    I n i nt e ra c ti ve u s e o f t h e P y th o n i nt e rp r et e r, y ou c a n t yp e a n e x pr e ss i on a n d i mm e di a te l y s e e t h e r e su l to f i t s e v al u at i on . T hi s i s fi n e t o t e st o u t s y n ta x a n d m a y b e d o s i mp l e c a l c ul a to r c a lc u la t io n s. I n a p r og r am

  • 8/18/2019 Hands-On Python a Tutorial Introduction for Beginners

    17/139

  • 8/18/2019 Hands-On Python a Tutorial Introduction for Beginners

    18/139

    1.9. THE IDLE EDITOR AND EXECUTION 18

    I f t he S he ll w in do w d o es n ot a ut om at ic al ly c om e t o t he f or eg ro un d, s el ec t i t. Yo u s ho ul d s ee a l in es ay i ng “ R ES TA RT ” a n d t h en t h e s t a r t o f t h e e x e c ut i on o f t h e M a d L i b p r og r am w it h t h e c u rs o r w ai t in g f o ry ou r e nt r y a f t e r t h e fi r st p r om pt . F i ni s h e x e c ut i ng t h e p r o g ra m . B e s u re t o t yp e t h e fi n a l r e qu e st e d E n t er ,s o y ou g e t b a c k t o t h e i n t er p re t er p r om p t: > >>

    L o o k a t t h e e d i t or w in d ow a g ai n . Yo u s h ou l d s e e t h at d i ffe r en t p a r t s o f t h e c o d e h av e d i ff e re nt c o lo r s.S t ri n g l it e ra l s a r e l i ke l y g r ee n . T he r e se r ve d w or d s   d e f   a r e l i k el y o r an g e. L o o k a t t h e l a s t t wo l i ne s , w h e r e

    t h e i d e nt i fi e r   t e l l S t o r y   i s b l ac k, a n d t h e i d en ti fi er   i n p u t   i s l ik el y p ur pl e. O nl y i de nt ifi er s t ha t a re n otp r ed e fin e d b y P y th o n a r e b l ac k. I f           you    c r e at e a n i d en ti fi er n a me , m a ke s u re I d le s h ow s i t i n black .

    1.9.2. A Bug Possible When Restarting Program Execution in Idle.   W he n y ou e xe cu te ap r og r am f r om t h e I d le E d it o r, t h e i nt e rp r et e r g i ve s a b a nn e r s ay i ng “ R ES TA RT ” , m e an i ng t h at a l l t h et h in g s y o u d e fin e d i n a ny s h el l s e ss i on s o f a r a r e w ip e d c l ea n a n d t h e p r og r am y ou a r e r u n n in g s t ar t s f r e s h.T h er e i s o n e e g r e gi o us e x ce p ti o n t o t h at , t h at w as s t il l p r es e nt a t l e as t i n t h e v e r si o n o f I d le f o r P y t h on 3 . 1i n W i nd ow s . We w i ll t r y t o d e mo n st r at e t h e b u g . ( A bug    i s a n e r r o r i n a p r o g r a m . )

    S t ar t r u nn i ng t h e M a d L i b p r og r am a g ai n b y g o in g t o t h e E d it o r W i nd ow c o nt a in i ng m a dl i b. py, a n ds t ar t r u nn i ng t h e p r og r am a g ai n , b u t d o n o t c o nt i nu e .. . .

    Yo u s h ou l d s e e a p r om p t f o r u s er i n pu t g e ne r at e d b y t h e p r o gr a m. I g no r e t h i s p r o mp t a n d g o b a ck t ot h e E d i t W i nd ow a n d s t a r t t h e M a d L i b p r og r am a g ai n .

    I f t hi s b ug i s s ti ll p re se nt , y ou s ho ul d s ee a d iff er en ce i n t hi s r es ta rt : T hi s t im e a ft er t he R ES TA RT

    b a nn e r a n d t h e i nt e rp r et e r p r om p t: > >> , w h ic h l o ok s i n no c e nt e n ou g h, b u t t h is p r og r am s h ou l d s h ow t h eprogram’s    p r o mp t s t r i ng f o r i n p u t .T h e p r o b l e m o n l y c o m e s u p b e c a u s e y o u i n t e r r u p t e d t h e l a s t e x e c u t i o n w h e n u s e r i n p u t w a s b e i n g w a i t e d

    f o r. T h e r e s ta r t w a s n o t c o mp l et e h e r e : T h e s y s te m i s s t il l l o o k i ng f o r t h e p e n di n g u s e r i n p u t f r o m t h e l a stexecution.

    T h e fi x i s s i mp l e: M a ke s u re t h e I n t er p re t er W i nd ow i s t h e c u r r en tl y s e le c te d w i nd ow , a n d p r es s r e tu r nt o t e rm in a te t h e l o s t u s er i n pu t . I n s o me c i rc u ms t an c es , y ou m ay n e ed t o p r es s r e tu r n a s e co n d t i m e.

    A f te r t h at t h e p r og r am s h ou l d s t ar t u p n o rm a ll y w it h i t s p r om p t.Wa t ch o u t f o r t h is b e h av i or , a n d r e me mb e r t h e fi x .

    1.9.3. The Classic First Program .   M a k e s u re y ou h av e I d le s t ar t ed i n y ou r P y th o n d i re c to r y ( i nW i nd ow s w it h t h e p r ov i de d I d le s h or t cu t l i nk ) , w h er e y ou w il l s t or e p r og r am fi l es . ( Do  not   s t a rt I d le f r omt he W in do ws S ta rt M en u! ) I f y ou j us t s ta rt ed I dl e n ow , y ou m ay a lr ea dy h av e a b la nk E di t W in do w i n

    f ro nt o f y ou . I f n ot , o p en a n ew w in do w b y g oi ng t o t he F il e m en u a n d s el ec ti ng N ew W in dow . T hi s g ive sy ou a r a th e r c o nv en ti o na l t e xt e d it i ng w in d ow w i th t h e m o us e a va i la b le , a b il i ty t o c u t a n d p a st e , p l us a f e ws p e c i al o p t i on s f o r P y t h on .

    T yp e ( o r p a st e ) t h e f o ll ow i ng i nt o t h e editor   window:

    p r i n t( ’ H el l o w o r ld ! ’ )

    S ave t he fi le w it h t he F il e m en u - > S av e, a nd t he n e nt er t he fi le n am e   h e l l o . p y . P yt ho n p ro gr am fi le ss h ou l d a l w ay s b e g i ve n a n a me e n di n g i n " . py " , a n d y ou m us t e nt e r t h e . py e x te n si o n e x p l ic i tl y .

    I f y ou l o ok i n t he e di to r, y ou s ho ul d s ee t ha t y ou r t ex t i s c ol or c o de d. T he e di to r w il l c ol or d iff er entp a rt s o f P y th o n s y n ta x i n s p e ci a l c o l or s . ( I n v e r si o n 2 . 4 o f P y th o n, t h e c o l o ri n g o n l y h a pp e n s a f t e r y o u s av ey o ur fi l e w i t h t h e ’. p y’ e n d i ng . )

    N ow t h at y ou h av e a c o mp l et e , s a ve d p r og r am , c ho o s e R u n m e nu - > R un M o d ul e . Yo u s h ou l d s e e t h ep r og r am r u n i n t h e P y th o n S h el l w in d ow .

    Yo u j us t w ro te a nd e xe cu te d a p ro gr am . U nl ik e w h e n y o u u se t he s he ll , t hi s c o de i s s ave d t o a fi le i ny ou r P y th o n f o l d er . Yo u c a n o p e n a n d e x e c ut e t h e fi l e a ny t i me y ou w an t. ( I n I d l e , u s e F i le - >O p en . )

    To t h e i nt e rp r et e r, a p r og r am s o ur c e fi l e c o r r es p o nd s t o a P y th o n module . We w il l t e nd t o u s e t he m or eg e ne r al t e rm : a p r og r am fi l e i s a m o d ul e . N o te t h e t e r m f r o m t h e m e nu w h en r u nn i ng t h e p r o gr a m.

    Distinguish program code from Shell text : I t i s e a s y t o c on fu se t he S he ll a nd t he E di t w in dow s. M akes u re y ou k ee p t h em s t ra i gh t. T he h e ll o .p y p r og r am i s j u st t h e l i n e

    p r i n t( ’ H el l o w o r ld ! ’ )

    t ha t yo u ty p ed i nt o t he e di t w in do w a nd s av ed . W he n y ou r an t he p ro gr am i n I dl e, y ou s aw r es ul ts i n t heS h el l . F i rs t c a me t h e R es t ar t n o ti c e, t h e o n e- l in e o u tp u t f r o m t h e p r og r am s ay i ng h e ll o , a n d a f u rt h er S h el lprompt:

  • 8/18/2019 Hands-On Python a Tutorial Introduction for Beginners

    19/139

    1.10. INPUT AND OUTPUT 19

    > > > = = = == = = == = = == = = == = = = == = = == = = == = = R E S TA R T = = = == = = =

    > > >

    H e l l o w o r ld !

    > > >

    Yo u c o ul d a l so h av e t y p e d t h i s s i ng l e p r i nt i ng l i ne d i re c tl y i n t h e S h e l l i n r e sp o n se t o a S h el l p r om p t. W h eny ou s e e   > > >, y ou c o ul d e nt e r t h e p r in t f u n c ti o n a n d g e t t h e e x ch a ng e b e tw ee n y ou a n d t h e S h el l :

    > > > p r i nt ( ’ He l l o w o r ld ’ )

    H e l l o w o r ld !

    > > >

    T h e t h r e e l in e s a b ov e a r e not   a p ro gr am y ou c ou ld s av e i n a fi le a nd r un . T hi s i s j u s t a n e xch an ge i n t heShell , w i t h i t s   > >>   p r o mp t s, i n di v id u al l i ne t o e x ec u te a n d t h e r e s p o ns e . A g ai n , j u s t t h e s i ng l e l i n e , w i t h n o> >> ,

    p r i n t( ’ H el l o w o r ld ! ’ )

    e n t e r e d i n t o t h e  Edit   w i n do w f o r ms a p r og r am y ou c a n s a ve a n d r u n . We w i ll s h or t ly g e t t o m o re i nt e re s ti n gm a ny - st a te m en t p r og r am s , w h er e i t i s m uc h m o re c o nv en i en t t o u s e t h e E d it w in d ow t h an t h e S h el l !

    1.9.4. Program Documentation String.   T h e p r og r am a b ov e i s s e lf e v id e nt , a n d s h o ws h ow s h or ta n d d i re c t a p r og r am c a n b e ( u nl i ke o t he r l a ng u ag e s l i ke J ava ) . S t il l , r i gh t a wa y, g e t u s ed t o d o c um e nt i ng

    a p ro gr am . P yt ho n h as a s p ec ia l f ea tu re : I f t he b e gi nn in g o f a p ro gr am i s j us t a q uo te d s tr in g, t ha t s tr in gi s t a ke n t o b e t h e p r o g ra m’ s documentation string . O p en t he e xa mp le fi le   h e l l o 2 . p y   i n t h e E d it w in d ow :

    ’ ’ ’A v e ry s i mp l e p r og r am ,

    s ho wi ng h ow s ho rt a P yt ho n p ro gr am c an b e!

    A u th o rs : _ _ _, _ _ _

    ’ ’ ’

    p ri nt (’ He ll o w or ld !’ ) # Th is i s a s tu pi d c om me nt a ft er t he # m ar k

    M o st c o mm o nl y, t h e i n it i al d o c um en ta t io n g o e s o n f o r s e ve r al l i ne s , s o a m ul t i- li n e s t ri n g d e li mi t er i s u s ed( th e t ri pl e q uo te s) . J us t f or c om pl et en es s o f i ll us tr at io n i n t hi s p ro gr am , a no th er f or m o f c om me nt i sa ls o s ho wn , a c om me nt t ha t s ta rt s w it h t he s ymb o l # a nd e xt en ds t o t he e nd o f t he l in e. T he P yt ho ni nt e rp r et e r c o mp l et e ly i g no r es t h is f o rm o f c o mm e nt . S u ch a c o mm e nt s h ou l d o n ly b e i n cl u de d f o r b e t te r

    h um a n u n de r st a nd i ng . Av oi d m a ki n g c o mm e nt s t h at d o n o t r e al ly a i d h um a n u n de r st a nd i ng . ( D o w h at Is ay, n o t w h a t I d i d a b ov e. ) G o o d i nt r o du c to r y c o mm en t s t ri n gs a n d a p pr o pr i at e n a me s f o r t h e p a rt s o f y ou rp r o g ra m s m a ke f e we r # s y mb o l c o m me n ts n e e d ed .

    R un t h e p r og r am a n d s e e t h e d o c um en ta t io n a n d c o mm en t m a ke n o d i ffe r en c e i n t h e r e su l t.

    1.9.5. Screen Layout.   O f c o u r s e y o u c a n a r r a n g e t h e w i n d o w s o n y o u r c o m p u t e r s c r e e n a n y w a y t h a ty ou l ik e. A s ug ge st io n a s yo u s ta rt t o u se t he c omb in at io n o f t he e di to r t o w ri te , t he s he ll t o r un , a nd t het u to r ia l t o f o ll ow a l on g : M a ke a l l t h r e e m o st l y v i si b le y ou r c o mp u te r s c re e n a t o n ce . D r ag t h e e d it o r w i n d owt o t h e u p p er l e ft . P l ac e t h e S h e ll w i nd ow t o t h e l ow er l e ft , a n d p e r h ap s r e du c e i t s h e ig ht a b i t s o t h er e i s n o tm uc h o v er l ap . I f y o u a r e l o o k in g a t t h e w e b v e r si o n o f t h e t u t o ri a l o n t h e s c r e en , m a ke i t g o t o p t o b o t to mo n t h e r i g ht , b u t n o t o ve r la p t h e I d l e w in d ow s t o o m uc h. T h e w e b p a ge r e nd e ri n g s h o u ld g e ne r al ly a d ap tt o t h e w i dt h p r et ty w el l. Yo u c a n a l wa ys t e mp o r ar i ly m a xi m iz e t h e w in d ow . B e fo r e r e si z in g t h e b r ow s erw i n d o w , i t i s g o o d t o l o o k f o r a n u n u s u a l p h r a s e o n y o u r p a g e , a n d s e a r c h f o r i t a f t e r r e s i z i n g , s i n c e r e s i z i n g

    c a n t o t a ll y m e ss u p y ou r l o c at i on i n t h e w e b p a ge .T h er e i s a n a l te r na t iv e t o m a xi mi z at i on f o r t h e I d le e d it o r w i n do w: I t y ou w an t i t t o g o t o p t o b o t to m

    o f t h e s c r ee n b u t n o t w i de n , y o u c a n t o gg l e t h a t s t a t e w i t h A l t -2 . P l ay w it h a l l t h i s.

    1.10. Input and Output

    1.10.1. The input  Function.   T h e h e l lo p r og r am o f S e ct i on 1 . 9. 3 a l wa ys d o e s t h e s a me t h in g . T h is i sn o t v e ry i nt e re s ti n g. P r og r am s a r e o n ly g o in g t o b e r e us e d i f t h ey c a n a c t o n a va r ie ty o f d a t a . O ne w ay t og e t d a t a i s d i re c tl y f r om t h e u s e r . M o d if y t h e h e ll o .p y p r o g ra m a s f o ll ow s i n t h e e d i to r , a n d s av e i t f r om t h eF i le m e nu w it h S av e A s .. .. , u s in g t h e n a me   h e l l o _ y o u . p y .

  • 8/18/2019 Hands-On Python a Tutorial Introduction for Beginners

    20/139

    1.10. INPUT AND OUTPUT 20

    p er so n = i np ut (’ En te r y ou r n am e: ’ )

    p r i n t( ’ H el l o ’ , p e r so n )

    R un t h e p r o g ra m . I n t h e S h e ll y ou s h ou l d s e e

    E nt er y ou r n am e:

    Fo ll ow t he i ns tr uc ti on ( an d p re ss E nt er ). M ake s ur e t he ty pi ng c ur so r i s i n t he S he ll w in do w, a t t he e ndo f t hi s l in e. A ft er y ou t yp e y ou r r es p on se , yo u c an s ee t ha t t he p ro gr am h as t ake n i n t he l in e yo u t yp e d.T h a t i s w h a t t h e b u i l t - i n f u n c t i o n   i n p u t   d o e s: F i rs t i t p r i nt s t h e s t r in g y ou g i ve a s a p a ra me t er ( in t h is c a se’ En te r y ou r n ame : ’ ) , a nd t he n i t wa it s f or a l in e t o b e ty p ed i n, a nd r et ur ns t he s tr in g o f ch ar ac te rsy ou ty p ed . I n t he   hello_you.py   p r o gr a m t h is va l ue i s a s si g ne d t o t h e va r ia b le p e r so n , f o r u s e l a te r .

    T h e p a ra m et e r i n si d e t h e p a re nt h es e s a f te r   i n p u t   is i mp or ta nt . I t i s a   prompt , p ro mp ti ng y ou t ha tk ey b o ar d i n pu t i s e x p ec t ed a t t h at p o i nt , a n d h o p e f ul l y i n d i ca t in g w h at i s b e i ng r e qu e st e d. W i th o ut t h ep r om p t, t h e u s er w ou l d n o t k n ow w h at w as h a pp e n in g , a n d t h e c o mp u te r w ou l d j u st s i t t h er e w ai t in g !

    O p e n t h e e x a m p l e p r o g r a m ,   i n t e r v i e w . p y . B ef o re r u nn i ng i t ( w i t h a n y m a d e- u p d a t a) , s e e i f y o u c a nfi gu re o ut w ha t i t w il l d o:

    ’ ’ ’I l lu s tr a te i n pu t a n d p r in t .’ ’ ’

    a p pl i ca n t = i n pu t (" E nt e r t h e a p pl i ca n t’ s n a me : " )

    i n te r vi e we r = i n pu t (" E nt e r t h e i n te r vi e we r ’s n a me : " )

    t im e = i np ut (" En te r t he a pp oi nt me nt t im e: " )p r i n t( i n te r v i ew e r , " w i ll i n t er v i ew " , a p p li c a nt , " a t ", t i m e)

    T h e s t a t em e nt s a r e e x e c ut e d i n t h e o r d e r t h e y a p p ea r i n t h e t e x t o f t h e p r o g ra m :   sequentially . T hi s i s t h es i mp l es t w ay f o r t h e e x ec u ti o n o f t h e p r og r am t o fl ow . Yo u w il l s e e i n st r uc t io n s l a te r t h at a l te r t h at n a tu r alflow.

    I f we wa nt t o r el oa d a nd m o di fy t he   h e l l o _ y o u . p y   p r og ra m t o p ut a n e xc la ma ti on p o in t a t t he e nd ,y ou c o ul d t r y:

    p er so n = i np ut (’ En te r y ou r n am e: ’ )

    p r i n t( ’ H el l o ’ , p e r so n , ’ ! ’ )

    Ru n i t a n d yo u s ee t ha t i t i s n ot s pa ce d r ig ht . T he re s ho ul d b e n o s pa ce a ft er t he p e rs on ’s n am e, b ut t hed e fa u lt b e h av i or o f t h e p r i nt f u nc t io n i s t o h av e e a c h fi e l d p r in te d s e pa r at e d b y a s p ac e . T h er e a r e s e v er a lw ay s t o fi x t h i s . Yo u s h o u ld k n ow o n e. T h in k a b o u t i t b e f or e g o i n g o n t o t h e n e xt s e ct i on . H in t:   4

    1.10.2. Print with Keyword Parameter sep.   O n e w ay t o p u t p u n c tu a ti o n b u t n o s p ac e a f te r t h ep e r so n i n h e ll o _y ou . py i s t o u s e t h e p l u s o p e ra t or , + . A no t he r a p pr o ac h i s t o c ha n ge t h e d e fa u lt s e pa r at p rb e tw ee n fi e ld s i n t h e p r i nt f u nc t io n . T h is w i ll i nt r o du c e a n e w s y n ta x f e at u re ,  keyword parameters . T hep ri nt f un ct io n h as a ke ywo rd p ar am et er n am ed s ep . I f yo u l ea ve i t o ut o f a c al l t o p ri nt , a s we h av e s o f ar ,i t i s s et e qu al t o a s pa ce b y d ef au lt . I f y ou a dd a fi na l fi el d, s ep =” , i n t he p ri nt f un ct io n i n h el lo _yo u. py,y ou g e t t h e f o ll ow i ng e x am p le fi l e,   h e l l o _ y o u 2 . p y :

    p er so n = i np ut (’ En te r y ou r n am e: ’ )

    p r in t (’ H el l o ’ , p e rs o n, ’ ! ’, s e p= ’ ’)

    Tr y t h e p r o g ra m .K e yw or d p a ra m at e rs m us t b e l i st e d a t t h e e n d o f t h e p a ra m et e r l i st .

    1.10.3. Numbers and Strings of Digits.   C on s id e r t h e f o ll ow i ng p r ob l em : P r om p t t h e u s er f o r t wo

    n um b er s , a n d t h en p r in t o u t a s e nt e nc e s t a t in g t h e s u m . Fo r i n st a nc e i f t h e u s er e nt e re d 2 a n d 3 , y ou w ou l dp r i n t “ T h e s u m o f 2 a n d 3 i s 5 . ”Yo u m ig ht i ma g in e a s o lu t io n l i ke t h e e x am p le fi l e   a d d i t i o n 1 . p y, s how n b e lo w. T he re i s a p ro bl em .

    C an y ou fi gu re i t o ut b e fo re y ou t ry i t? H in t:   5 E n d u p r u n n i n g i t i n a n y c a s e .

    x = i np ut (" En te r a n i nt eg er : " )

    y = i np ut (" En te r a no th er i nt eg er : " )

    p r in t( ’T he s u m of ’ , x, ’ an d ’, y , ’ i s ’, x +y , ’ .’ , se p= ’ ’) # er ro r !

    4The + operation on strings adds no extra space.5The input function produces values of string type.

  • 8/18/2019 Hands-On Python a Tutorial Introduction for Beginners

    21/139

    1.10. INPUT AND OUTPUT 21

    We d o n o t w a n t s t r i n g c o n c a t e n at i o n , b u t i n te g e r a d d i t i on . We n e e d i n t e g er o p e r a nd s . B r i efl y m e nt i o ne d i nS e ct i on 1 . 3 w a s t h e f a ct t h at w e c a n u s e t y p e n a m e s a s f u nc t io n s t o c o nv er t t yp e s . O n e a p p r oa ch w ou l d b et o d o t h a t . Fu r t h er v ar i a b le n a m e s a r e a l s o i n tr o d u c ed i n t h e e x a m pl e   a d d i t i o n 2 . p y   fi l e b e l o w t o e m p h a s i z et h e d i s t in c ti o ns i n t yp e s . R e ad a n d r u n :

    ’ ’ ’C o nv e rs i on o f s t ri n gs t o i n t b e fo r e a d di t io n ’’ ’

    x St ri ng = i np ut (" En te r a n i nt eg er : " )x = i n t( x St r in g )

    y S tr i ng = i n pu t (" E nt e r a n ot h er i n te g er : " )

    y = i n t( y St r in g )

    p rin t( ’T he s um of ’ , x, ’ an d ’, y , ’ i s ’, x +y , ’ .’ , se p= ’’)

    N e ed i ng i t o c o nv er t s t ri n g i n pu t t o n um b er s is a c o mm o n s i tu a ti o n, b o t h w i th k ey b o ar d i n pu t a n d l a te r i nwe b p ag es . W hi le t he e xt ra va ri ab le s a b ov e e mp ha si ze d t he s t