fork download
  1. import re
  2. s = """
  3. This file contains the sample data
  4.  
  5.  
  6. Why should you learn to write programs?
  7.  
  8. Writing programs (or programming) is a very creative
  9. and rewarding activity. You can write programs for
  10. many reasons, ranging from making your living to solving
  11. a difficult data analysis problem to having fun to helping
  12. 1808 someone else solve a problem. This book assumes that 9086
  13. everyone needs to know how to program, and that once
  14. you know how to program you will figure out what you want
  15. to do with your newfound skills.
  16. 9586
  17. We are surrounded in our daily lives with computers ranging
  18. from 241 laptops 8452 to 8013 cell phones. We can think of these computers
  19. as our personal assistants who can take care of many things
  20. on our behalf. The hardware in our current-day computers
  21. is essentially built to continuously ask us the question,
  22. What would you like me to do next?
  23.  
  24. Programmers 8917 add 5472 an 4772 operating system and a set of applications
  25. to the hardware and we end up with a Personal Digital
  26. Assistant that is quite helpful and capable of helping
  27. us do many different things.
  28. 5659 4438
  29. Our computers are fast and have vast amounts of memory and
  30. could 1519 be 5566 very 3500 helpful to us if we only knew the language to
  31. speak to explain to the computer what we would like it to
  32. do next. If we knew this language, we could tell the
  33. computer to do tasks on our behalf that were repetitive.
  34. Interestingly, the kinds of things computers can do best
  35. are often the kinds of things that we humans find boring
  36. 6504 8211 2342
  37.  
  38. For example, look at the first three paragraphs of this
  39. chapter and tell me the most commonly used word and how
  40. many times the word is used. While you were able to read
  41. and understand the words in a few seconds, counting them
  42. is almost painful because it is not the kind of problem
  43. that human minds are designed to solve. For a computer
  44. the opposite is true, reading and understanding text
  45. from a piece of paper is hard for a computer to do
  46. but counting the words and telling you how many times
  47. the most used word was used is very easy for the
  48. computer:
  49.  
  50. Our personal information analysis assistant quickly
  51. told us that the word to was used sixteen times in the
  52. first three paragraphs of this chapter.
  53.  
  54. This very fact that computers are good at things
  55. that humans are not is why you need to become
  56. skilled at talking computer language. Once you learn
  57. 4175 this new language, you can delegate mundane tasks
  58. to your partner (the computer), leaving more time
  59. for you to do the
  60. things that you are uniquely suited for. You bring
  61. creativity, intuition, and inventiveness to this
  62. partnership.
  63.  
  64. Creativity and motivation
  65.  
  66. While this book is not intended for professional programmers, professional
  67. programming can be a very rewarding job both financially and personally.
  68. Building useful, elegant, and clever programs for others to use is a very
  69. creative activity. Your computer or Personal Digital Assistant (PDA)
  70. usually contains many different programs from many different groups of
  71. programmers, each competing for your attention and interest. They try
  72. their best to meet your needs and give you a great user experience in the
  73. process. In some situations, when you choose a piece of software, the
  74. programmers are directly compensated because of your choice.
  75.  
  76. If we think of programs as the creative output of groups of programmers,
  77. perhaps the following figure is a more sensible version of our PDA:
  78.  
  79. For now, our primary motivation is not to make money or please end users, but
  80. instead for us to be more productive in handling the data and
  81. information 3496 that 8538 we 9150 will encounter in our lives.
  82. When you first start, you will be both the programmer and the end user of
  83. your programs. As you gain skill as a programmer and
  84. programming feels more creative to you, your thoughts may turn
  85. 2808 toward developing programs for others. 2500
  86. 8790 4428
  87. Computer hardware architecture
  88.  
  89. Before we start learning the language we
  90. speak to give instructions to computers to
  91. develop software, we need to learn a small amount about
  92. how computers are built.
  93.  
  94. Central Processing Unit (or CPU) is
  95. the part of the computer that is built to be obsessed
  96. with what is next? If your computer is rated
  97. at three Gigahertz, it means that the CPU will ask What next?
  98. three billion times per second. You are going to have to
  99. learn how to talk fast to keep up with the CPU.
  100.  
  101. Main Memory is used to store information
  102. that the CPU needs in a hurry. The main memory is nearly as
  103. fast as the CPU. But the information stored in the main
  104. 7519 memory vanishes when the computer is turned off. 1107
  105.  
  106. Secondary Memory is also used to store
  107. information, but it is much slower than the main memory.
  108. The advantage of the secondary memory is that it can
  109. store information even when there is no power to the
  110. computer. Examples of secondary memory are disk drives
  111. or flash memory (typically found in USB sticks and portable
  112. music players).
  113.  
  114. Input and Output Devices are simply our
  115. screen, keyboard, mouse, microphone, speaker, touchpad, etc.
  116. They are all of the ways we interact with the computer.
  117.  
  118. These days, most computers also have a
  119. 5484 Network Connection to retrieve information over a network. 6115
  120. We can think of the network as a very slow place to store and
  121. 1226 retrieve data that might not always be up. So in a sense, 2443
  122. the network is a slower and at times unreliable form of
  123. Secondary Memory.
  124.  
  125. While most of the detail of how these components work is best left
  126. to computer builders, it helps to have some terminology
  127. so we can talk about these different parts as we write our programs.
  128.  
  129. As a programmer, your job is to use and orchestrate
  130. 9459 each of these resources to solve the problem that you need to solve 1530
  131. and analyze the data you get from the solution. As a programmer you will
  132. mostly be talking to the CPU and telling it what to
  133. do next. Sometimes you will tell the CPU to use the main memory,
  134. secondary memory, network, or the input/output devices.
  135.  
  136. You need to be the person who answers the CPU's What next?
  137. question. But it would be very uncomfortable to shrink you
  138. down to five mm tall and insert you into the computer just so you
  139. could issue a command three billion times per second. So instead,
  140. 8774 you must write down your instructions in advance.
  141. We call these stored instructions a program and the act
  142. of writing these instructions down and getting the instructions to
  143. be correct programming.
  144.  
  145. Understanding programming
  146. 1996 3739
  147. In the rest of this book, we will try to turn you into a person
  148. who is skilled in the art of programming. In the end you will be a
  149. programmer --- perhaps not a professional programmer, but
  150. at least you will have the skills to look at a data/information
  151. analysis problem and develop a program to solve the problem.
  152.  
  153. problem solving
  154.  
  155. In a sense, you need two skills to be a programmer:
  156. 2267
  157. First, you need to know the programming language (Python) -
  158. you need to know the vocabulary and the grammar. You need to be able
  159. to spell the words in this new language properly and know how to construct
  160. well-formed sentences in this new language.
  161.  
  162. Second, 3126 you 7845 need 4965 to tell a story. In writing a story,
  163. you combine words and sentences to convey an idea to the reader.
  164. There is a skill and art in constructing the story, and skill in
  165. story writing is improved by doing some writing and getting some
  166. feedback. In programming, our program is the story and the
  167. problem you are trying to solve is the idea.
  168. 7180 7085 9780
  169. itemize
  170.  
  171. Once you learn one programming language such as Python, you will
  172. find it much easier to learn a second programming language such
  173. as JavaScript or C++. The new programming language has very different
  174. vocabulary and grammar but the problem-solving skills
  175. will be the same across all programming languages.
  176.  
  177. You will learn the vocabulary and sentences of Python pretty quickly.
  178. It will take longer for you to be able to write a coherent program
  179. to solve a brand-new problem. We teach programming much like we teach
  180. writing. We start reading and explaining programs, then we write
  181. simple programs, and then we write increasingly complex programs over time.
  182. At some point you get your muse and see the patterns on your own
  183. and can see more naturally how to take a problem and
  184. write a program that solves that problem. And once you get
  185. to that point, programming becomes a very pleasant and creative process.
  186.  
  187. We start with the vocabulary and structure of Python programs. Be patient
  188. as the simple examples remind you of when you started reading for the first
  189. time.
  190.  
  191. Words and sentences
  192.  
  193. Unlike 8085 human 7661 languages, 1585 the Python vocabulary is actually pretty small.
  194. We call this vocabulary the reserved words. These are words that
  195. have very special meaning to Python. When Python sees these words in
  196. a Python program, they have one and only one meaning to Python. Later
  197. as you write programs you will make up your own words that have meaning to
  198. you called variables. You will have great latitude in choosing
  199. 310 your names for your variables, but you cannot use any of Python's 1458
  200. 6877 reserved words as a name for a variable. 2099
  201.  
  202. When we train a dog, we use special words like
  203. sit, stay, and fetch. When you talk to a dog and
  204. don't use any of the reserved words, they just look at you with a
  205. quizzical look on their face until you say a reserved word.
  206. For example, if you say,
  207. I wish more people would walk to improve their overall health,
  208. what most dogs likely hear is,
  209. blah blah blah walk blah blah blah blah.
  210. That is because walk is a reserved word in dog language.
  211. 1946 2895
  212. The reserved words in the language where humans talk to
  213. Python include the following:
  214. 5911 9533
  215. and del from not while
  216. as 7493 1812 7170 elif global or with
  217. assert 6584 828 4688 else if pass yield
  218. 6097 break except import print 7560
  219. class 6060 336 7199 exec in raise
  220. continue finally is return
  221. def for lambda try
  222.  
  223. That is it, and unlike a dog, Python is already completely trained.
  224. When you say try, Python will try every time you say it without
  225. fail.
  226.  
  227. We will learn these reserved words and how they are used in good time,
  228. but for now we will focus on the Python equivalent of speak (in
  229. human-to-dog language). The nice thing about telling Python to speak
  230. is that we can even tell it what to say by giving it a message in quotes:
  231.  
  232. And we have even written our first syntactically correct Python sentence.
  233. Our sentence starts with the reserved word print followed
  234. by a string of text of our choosing enclosed in single quotes.
  235.  
  236. Conversing with Python
  237. 1724 3537
  238. Now that we have a word and a simple sentence that we know in Python,
  239. we need to know how to start a conversation with Python to test
  240. our new language skills.
  241.  
  242. Before you can converse with Python, you must first install the Python
  243. software on your computer and learn how to start Python on your
  244. computer. That is too much detail for this chapter so I suggest
  245. that you consult www.pythonlearn.com where I have detailed
  246. instructions and screencasts of setting up and starting Python
  247. 1608 on Macintosh and Windows systems. At some point, you will be in
  248. a terminal or command window and you will type python and
  249. the Python interpreter will start executing in interactive mode
  250. and appear somewhat as follows:
  251. interactive mode
  252.  
  253. The >>> prompt is the Python interpreter's way of asking you, What
  254. do you want me to do next? Python is ready to have a conversation with
  255. you. All you have to know is how to speak the Python language.
  256.  
  257. Let's say for example that you did not know even the simplest Python language
  258. words or sentences. You might want to use the standard line that astronauts
  259. use when they land on a faraway planet and try to speak with the inhabitants
  260. of the planet:
  261.  
  262. This is not going so well. Unless you think of something quickly,
  263. 6211 the inhabitants of the planet are likely to stab you with their spears, 4638
  264. put you on a spit, roast you over a fire, and eat you for dinner.
  265.  
  266. At this point, you should also realize that while Python
  267. is amazingly complex and powerful and very picky about
  268. the syntax you use to communicate with it, Python is
  269. not 1865 intelligent. 1759 9584 You are really just having a conversation
  270. with yourself, but using proper syntax.
  271.  
  272. 3208 In a sense, when you use a program written by someone else 5376
  273. the conversation is between you and those other
  274. programmers 2762 with 2485 Python 6753 acting as an intermediary. Python
  275. is a way for the creators of programs to express how the
  276. conversation is supposed to proceed. And
  277. in 5547 just 9481 a 4732 few more chapters, you will be one of those
  278. programmers using Python to talk to the users of your program.
  279. 7001 6889 6393
  280. Before we leave our first conversation with the Python
  281. interpreter, you should probably know the proper way
  282. to say good-bye when interacting with the inhabitants
  283. of Planet Python:
  284.  
  285. You will notice that the error is different for the first two
  286. incorrect attempts. The second error is different because
  287. if is a reserved word and Python saw the reserved word
  288. and thought we were trying to say something but got the syntax
  289. of the sentence wrong.
  290.  
  291. Terminology: interpreter and compiler
  292.  
  293. Python 1630 is 1704 a 9411 high-level language intended to be relatively
  294. 5079 straightforward for humans to read and write and for computers 8271
  295. to read and process. Other high-level languages include Java, C++,
  296. PHP, Ruby, Basic, Perl, JavaScript, and many more. The actual hardware
  297. inside the Central Processing Unit (CPU) does not understand any
  298. 8858 of these high-level languages.
  299.  
  300. 1091 The CPU understands a language we call machine language. Machine
  301. language is very simple and frankly very tiresome to write because it
  302. is represented all in zeros and ones.
  303.  
  304. Machine language seems quite simple on the surface, given that there
  305. are only zeros and ones, but its syntax is even more complex
  306. and far more intricate than Python. So very few programmers ever write
  307. machine language. Instead we build various translators to allow
  308. programmers to write in high-level languages like Python or JavaScript
  309. and these translators convert the programs to machine language for actual
  310. execution by the CPU.
  311.  
  312. Since 6947 machine 4574 language 4810 is tied to the computer hardware, machine language
  313. is not portable across different types of hardware. Programs written in
  314. high-level languages can be moved between different computers by using a
  315. different interpreter on the new machine or recompiling the code to create
  316. a machine language version of the program for the new machine.
  317.  
  318. These programming language translators fall into two general categories:
  319. 8893 (one) interpreters and (two) compilers.
  320.  
  321. An interpreter reads the source code of the program as written by the
  322. programmer, parses the source code, and interprets the instructions on the fly.
  323. 3192 Python is an interpreter and when we are running Python interactively, 5616
  324. we can type a line of Python (a sentence) and Python processes it immediately
  325. 9390 and is ready for us to type another line of Python.
  326.  
  327. Some of the lines of Python tell Python that you want it to remember some
  328. value for later. We need to pick a name for that value to be remembered and
  329. we can use that symbolic name to retrieve the value later. We use the
  330. term variable to refer to the labels we use to refer to this stored data.
  331.  
  332. In this example, we ask Python to remember the value six and use the label x
  333. so we can retrieve the value later. We verify that Python has actually remembered
  334. the value using x and multiply
  335. it by seven and put the newly computed value in y. Then we ask Python to print out
  336. the value currently in y.
  337.  
  338. 4229 Even though we are typing these commands into Python one line at a time, Python 1444
  339. is treating them as an ordered sequence of statements with later statements able
  340. to retrieve data created in earlier statements. We are writing our first
  341. 9425 simple paragraph with four sentences in a logical and meaningful order. 3742
  342.  
  343. It is the nature of an interpreter to be able to have an interactive conversation
  344. as shown above. A compiler needs to be handed the entire program in a file, and then
  345. it runs a process to translate the high-level source code into machine language
  346. and then the compiler puts the resulting machine language into a file for later
  347. execution.
  348.  
  349. If you have a Windows system, often these executable machine language programs have a
  350. suffix 9407 of 9433 .exe 3920 or .dll which stand for executable and dynamic link
  351. library respectively. In Linux and Macintosh, there is no suffix that uniquely marks
  352. a file as executable.
  353. 676 5628 5224
  354. If you were to open an executable file in a text editor, it would look
  355. completely crazy and be unreadable:
  356.  
  357. It is not easy to read or write machine language, so it is nice that we have
  358. compilers that allow us to write in high-level
  359. 4603 languages like Python or C. 2691
  360.  
  361. Now at this point in our discussion of compilers and interpreters, you should
  362. be wondering a bit about the Python interpreter itself. What language is
  363. it written in? Is it written in a compiled language? When we type
  364. python, what exactly is happening?
  365.  
  366. The Python interpreter is written in a high-level language called C.
  367. You can look at the actual source code for the Python interpreter by
  368. 9652 going to www.python.org and working your way to their source code. 8400
  369. So Python is a program itself and it is compiled into machine code.
  370. When you installed Python on your computer (or the vendor installed it),
  371. you copied a machine-code copy of the translated Python program onto your
  372. system. In Windows, the executable machine code for Python itself is likely
  373. in a file.
  374.  
  375. That is more than you really need to know to be a Python programmer, but
  376. sometimes it pays to answer those little nagging questions right at
  377. the beginning.
  378.  
  379. Writing a program
  380.  
  381. Typing commands into the Python interpreter is a great way to experiment
  382. with Python's features, but it is not recommended for solving more complex problems.
  383.  
  384. When we want to write a program,
  385. we use a text editor to write the Python instructions into a file,
  386. which is called a script. By
  387. convention, 3401 Python 9182 scripts 1957 have names that end with .py.
  388.  
  389. script
  390.  
  391. To execute the script, you have to tell the Python interpreter
  392. the name of the file. In a Unix or Windows command window,
  393. you would type python hello.py as follows:
  394.  
  395. We call the Python interpreter and tell it to read its source code from
  396. the file hello.py instead of prompting us for lines of Python code
  397. interactively.
  398.  
  399. You will notice that there was no need to have quit() at the end of
  400. the Python program in the file. When Python is reading your source code
  401. from a file, it knows to stop when it reaches the end of the file.
  402.  
  403. What is a program?
  404.  
  405. The definition of a program at its most basic is a sequence
  406. of Python statements that have been crafted to do something.
  407. Even our simple hello.py script is a program. It is a one-line
  408. program and is not particularly useful, but in the strictest definition,
  409. it is a Python program.
  410.  
  411. It might be easiest to understand what a program is by thinking about a problem
  412. that a program might be built to solve, and then looking at a program
  413. that would solve that problem.
  414.  
  415. Lets say you are doing Social Computing research on Facebook posts and
  416. you are interested in the most frequently used word in a series of posts.
  417. You could print out the stream of Facebook posts and pore over the text
  418. looking for the most common word, but that would take a long time and be very
  419. mistake prone. You would be smart to write a Python program to handle the
  420. task quickly and accurately so you can spend the weekend doing something
  421. fun.
  422.  
  423. For example, look at the following text about a clown and a car. Look at the
  424. text and figure out the most common word and how many times it occurs.
  425.  
  426. Then imagine that you are doing this task looking at millions of lines of
  427. text. Frankly it would be quicker for you to learn Python and write a
  428. Python program to count the words than it would be to manually
  429. scan the words.
  430.  
  431. The even better news is that I already came up with a simple program to
  432. find the most common word in a text file. I wrote it,
  433. tested it, and now I am giving it to you to use so you can save some time.
  434.  
  435. You don't even need to know Python to use this program. You will need to get through
  436. Chapter ten of this book to fully understand the awesome Python techniques that were
  437. used to make the program. You are the end user, you simply use the program and marvel
  438. at its cleverness and how it saved you so much manual effort.
  439. You simply type the code
  440. into a file called words.py and run it or you download the source
  441. code from http://w...content-available-to-author-only...n.com/code/ and run it.
  442.  
  443. This is a good example of how Python and the Python language are acting as an intermediary
  444. between you (the end user) and me (the programmer). Python is a way for us to exchange useful
  445. instruction sequences (i.e., programs) in a common language that can be used by anyone who
  446. installs Python on their computer. So neither of us are talking to Python,
  447. instead we are communicating with each other through Python.
  448.  
  449. The building blocks of programs
  450.  
  451. In the next few chapters, we will learn more about the vocabulary, sentence structure,
  452. paragraph structure, and story structure of Python. We will learn about the powerful
  453. capabilities of Python and how to compose those capabilities together to create useful
  454. programs.
  455.  
  456. There are some low-level conceptual patterns that we use to construct programs. These
  457. constructs are not just for Python programs, they are part of every programming language
  458. from machine language up to the high-level languages.
  459.  
  460. description
  461.  
  462. Get data from the outside world. This might be
  463. reading data from a file, or even some kind of sensor like
  464. a microphone or GPS. In our initial programs, our input will come from the user
  465. typing data on the keyboard.
  466.  
  467. Display the results of the program on a screen
  468. or store them in a file or perhaps write them to a device like a
  469. speaker to play music or speak text.
  470.  
  471. Perform statements one after
  472. another in the order they are encountered in the script.
  473.  
  474. Check for certain conditions and
  475. then execute or skip a sequence of statements.
  476.  
  477. Perform some set of statements
  478. repeatedly, usually with
  479. some variation.
  480.  
  481. Write a set of instructions once and give them a name
  482. and then reuse those instructions as needed throughout your program.
  483.  
  484. description
  485.  
  486. It sounds almost too simple to be true, and of course it is never
  487. so simple. It is like saying that walking is simply
  488. putting one foot in front of the other. The art
  489. of writing a program is composing and weaving these
  490. basic elements together many times over to produce something
  491. that is useful to its users.
  492.  
  493. The word counting program above directly uses all of
  494. these patterns except for one.
  495.  
  496. What could possibly go wrong?
  497.  
  498. As we saw in our earliest conversations with Python, we must
  499. communicate very precisely when we write Python code. The smallest
  500. deviation or mistake will cause Python to give up looking at your
  501. program.
  502.  
  503. Beginning programmers often take the fact that Python leaves no
  504. room for errors as evidence that Python is mean, hateful, and cruel.
  505. While Python seems to like everyone else, Python knows them
  506. personally and holds a grudge against them. Because of this grudge,
  507. Python takes our perfectly written programs and rejects them as
  508. unfit just to torment us.
  509.  
  510. There is little to be gained by arguing with Python. It is just a tool.
  511. It has no emotions and it is happy and ready to serve you whenever you
  512. need it. Its error messages sound harsh, but they are just Python's
  513. call for help. It has looked at what you typed, and it simply cannot
  514. understand what you have entered.
  515.  
  516. Python is much more like a dog, loving you unconditionally, having a few
  517. key words that it understands, looking you with a sweet look on its
  518. face (>>>), and waiting for you to say something it understands.
  519. When Python says SyntaxError: invalid syntax, it is simply wagging
  520. its tail and saying, You seemed to say something but I just don't
  521. understand what you meant, but please keep talking to me (>>>).
  522.  
  523. As your programs become increasingly sophisticated, you will encounter three
  524. general types of errors:
  525.  
  526. description
  527.  
  528. These are the first errors you will make and the easiest
  529. to fix. A syntax error means that you have violated the grammar rules of Python.
  530. Python does its best to point right at the line and character where
  531. it noticed it was confused. The only tricky bit of syntax errors is that sometimes
  532. the mistake that needs fixing is actually earlier in the program than where Python
  533. noticed it was confused. So the line and character that Python indicates in
  534. a syntax error may just be a starting point for your investigation.
  535.  
  536. A logic error is when your program has good syntax but there is a mistake
  537. in the order of the statements or perhaps a mistake in how the statements relate to one another.
  538. A good example of a logic error might be, take a drink from your water bottle, put it
  539. in your backpack, walk to the library, and then put the top back on the bottle.
  540.  
  541. A semantic error is when your description of the steps to take
  542. is syntactically perfect and in the right order, but there is simply a mistake in
  543. the program. The program is perfectly correct but it does not do what
  544. you intended for it to do. A simple example would
  545. be if you were giving a person directions to a restaurant and said, ...when you reach
  546. the intersection with the gas station, turn left and go one mile and the restaurant
  547. is a red building on your left. Your friend is very late and calls you to tell you that
  548. they are on a farm and walking around behind a barn, with no sign of a restaurant.
  549. Then you say did you turn left or right at the gas station? and
  550. they say, I followed your directions perfectly, I have
  551. them written down, it says turn left and go one mile at the gas station. Then you say,
  552. I am very sorry, because while my instructions were syntactically correct, they
  553. sadly contained a small but undetected semantic error..
  554.  
  555. description
  556.  
  557. Again in all three types of errors, Python is merely trying its hardest to
  558. do exactly what you have asked.
  559.  
  560. The learning journey
  561.  
  562. As you progress through the rest of the book, don't be afraid if the concepts
  563. don't seem to fit together well the first time. When you were learning to speak,
  564. it was not a problem for your first few years that you just made cute gurgling noises.
  565. And it was OK if it took six months for you to move from simple vocabulary to
  566. simple sentences and took five or six more years to move from sentences to paragraphs, and a
  567. few more years to be able to write an interesting complete short story on your own.
  568.  
  569. We want you to learn Python much more rapidly, so we teach it all at the same time
  570. over the next few chapters.
  571. But it is like learning a new language that takes time to absorb and understand
  572. before it feels natural.
  573. That leads to some confusion as we visit and revisit
  574. topics to try to get you to see the big picture while we are defining the tiny
  575. fragments that make up that big picture. While the book is written linearly, and
  576. if you are taking a course it will progress in a linear fashion, don't hesitate
  577. to be very nonlinear in how you approach the material. Look forwards and backwards
  578. and read with a light touch. By skimming more advanced material without
  579. fully understanding the details, you can get a better understanding of the why?
  580. of programming. By reviewing previous material and even redoing earlier
  581. exercises, you will realize that you actually learned a lot of material even
  582. if the material you are currently staring at seems a bit impenetrable.
  583.  
  584. Usually when you are learning your first programming language, there are a few
  585. wonderful Ah Hah! moments where you can look up from pounding away at some rock
  586. with a hammer and chisel and step away and see that you are indeed building
  587. a beautiful sculpture.
  588.  
  589. If something seems particularly hard, there is usually no value in staying up all
  590. night and staring at it. Take a break, take a nap, have a snack, explain what you
  591. are having a problem with to someone (or perhaps your dog), and then come back to it with
  592. fresh eyes. I assure you that once you learn the programming concepts in the book
  593. you will look back and see that it was all really easy and elegant and it simply
  594. took you a bit of time to absorb it.
  595. 42
  596. The end
  597. """
  598.  
  599. lst = []
  600. for line in s.split("\n"):
  601. line = line.strip()
  602. stuff = [lst.append(int(x)) for x in re.findall("[0-9]+", line)]
  603.  
  604. print sum(lst)
Success #stdin #stdout 0.01s 9072KB
stdin
Standard input is empty
stdout
597873