Think Python: How to Think Like a Computer Scientist
Format: PDF / Kindle (mobi) / ePub
If you want to learn how to program, working with Python is an excellent way to start. This hands-on guide takes you through the language a step at a time, beginning with basic programming concepts before moving on to functions, recursion, data structures, and object-oriented design. This second edition and its supporting code have been updated for Python 3.
Through exercises in each chapter, you’ll try out programming concepts as you learn them. Think Python is ideal for students at the high school or college level, as well as self-learners, home-schooled students, and professionals who need to learn programming basics. Beginners just getting their feet wet will learn how to start with Python in a browser.
- Start with the basics, including language syntax and semantics
- Get a clear definition of each programming concept
- Learn about values, variables, statements, functions, and data structures in a logical progression
- Discover how to work with files and databases
- Understand objects, methods, and object-oriented programming
- Use debugging techniques to fix syntax, runtime, and semantic errors
- Explore interface design, data structures, and GUI-based programs through case studies
>>> x = x+1 Updating a variable by adding 1 is called an increment; subtracting 1 is called a decrement. The while Statement Computers are often used to automate repetitive tasks. Repeating identical or similar tasks without making errors is something that computers do well and people do poorly. We have seen two programs, countdown and print_n, that use recursion to perform repetition, which is also called iteration. Because iteration is so common, Python provides several language
A reverse lookup is much slower than a forward lookup; if you have to do it often, or if the dictionary gets big, the performance of your program will suffer. Exercise 11-4. Modify reverse_lookup so that it builds and returns a list of all keys that map to v, or an empty list if there are none. Dictionaries and Lists Lists can appear as values in a dictionary. For example, if you were given a dictionary that maps from letters to frequencies, you might want to invert it; that is,
t1.minute + t2.minute sum.second = t1.second + t2.second return sum The function creates a new Time object, initializes its attributes, and returns a reference to the new object. This is called a pure function because it does not modify any of the objects passed to it as arguments and it has no effect, like displaying a value or getting user input, other than returning a value. To test this function, I’ll create two Time objects: start contains the start time of a movie, like Monty Python and
Widgets, Binding Canvas item, Canvas Widgets Canvas object, Exercises Canvas widget, Canvas Widgets Car Talk, Exercises, Exercises, Exercises, Exercises, Exercises Card class, Card Objects, Class Diagrams card, playing, Inheritance carrying, addition with, Algorithms, Pure Functions, Prototyping Versus Planning case-sensitivity, variable names, Debugging catch, Glossary chained conditional, Chained Conditionals, Glossary character, A String Is a Sequence checksum, Pipes, Pipes child
informative. The runtime error you are most likely to make is a “use before def;” that is, trying to use a variable before you have assigned a value. This can happen if you spell a variable name wrong: >>> principal = 327.68 >>> interest = principle * rate NameError: name 'principle' is not defined Variables names are case sensitive, so LaTeX is not the same as latex. At this point the most likely cause of a semantic error is the order of operations. For example, to evaluate , you might be