12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

6 Chapter 1. The way of theprogramHerearesomesuggestionsforreadingprograms(andotherformallanguages). First,rememberthatformallanguagesaremuchmoredensethannaturallanguages,soittakeslongertoreadthem. Also,the structure is very important, so it is usually not a good idea to read from top to bottom, left toright. Instead, learn to parse the program in your head, identifying the tokens and interpreting thestructure. Finally, the details matter. Small errors in spelling and punctuation, which you can getaway withinnatural languages, can make abig difference inaformal language.1.5 Thefirst programTraditionally, the first program you write in a new language is called “Hello, World!” because all itdoes isdisplay thewords, “Hello, World!” In<strong>Python</strong>, itlooks like this:print 'Hello, World!'Thisisanexampleofaprintstatement 2 ,whichdoesn’tactuallyprintanythingonpaper. Itdisplaysavalue on thescreen. Inthiscase, theresult isthewordsHello, World!The quotation marks in the program mark the beginning and end of the text to be displayed; theydon’t appear inthe result.Some people judge the quality of a programming language by the simplicity of the “Hello, World!”program. By thisstandard, <strong>Python</strong> does about as well aspossible.1.6 DebuggingIt is a good idea to read this book in front of a computer so you can try out the examples as yougo. You can run most of the examples in interactive mode, but if you put the code into a script, it iseasier totryout variations.Wheneveryouareexperimentingwithanewfeature,youshouldtrytomakemistakes. Forexample,in the “Hello, world!” program, what happens if you leave out one of the quotation marks? What ifyou leave out both? What if you spellprintwrong?This kind of experiment helps you remember what you read; it also helps with debugging, becauseyou get to know what the error messages mean. It is better to make mistakes now and on purposethan later and accidentally.Programming, and especially debugging, sometimes brings out strong emotions. If you are strugglingwithadifficult bug, you might feel angry, despondent or embarrassed.There is evidence that people naturally respond to computers as if they were people 3 . When theyworkwell,wethinkofthemasteammates,andwhentheyareobstinateorrude,werespondtothemthesame way werespond torude, obstinate people.Preparing for these reactions might help you deal with them. One approach is to think of the computeras an employee with certain strengths, like speed and precision, and particular weaknesses,likelack of empathy and inabilitytograspthe bigpicture.2 In<strong>Python</strong>3.0,printisafunction,notastatement,sothesyntaxisprint(’Hello, World!’). Wewillgettofunctionssoon!3 SeeReevesandNass, TheMediaEquation: HowPeopleTreat Computers, Television, andNewMediaLike RealPeopleandPlaces.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!