Think Python: How to Think Like a Computer Scientist
Keyboard Input The programs we have written so far accept no input from the user. They just do the same ...
Debugging When a syntax or runtime error occurs, the error message contains a lot of information, but it can b ...
Glossary floor division: An operator, denoted //, that divides two numbers and rounds down (toward zero) to an in ...
infinite recursion: A recursion that doesn’t have a base case, or never reaches it. Eventually, an infin ...
Exercises Exercise 5-1. The time module provides a function, also named time, that returns the current G ...
integers, and uses is_triangle to check whether sticks with the given lengths can form a triangle. Exercise ...
2 . Turn left 60 degrees. 3 . Draw a Koch curve with length x/3. 4 . Turn right 120 degrees. 5 . Draw ...
...
Chapter 6. Fruitful Functions Many of the Python functions we have used, such as the math functions, ...
Return Values Calling the function generates a return value, which we usually assign to a variable or use ...
This function is incorrect because if x happens to be 0, neither condition is true, and the function ends ...
Incremental Development As you write larger functions, you might find yourself spending more time debuggi ...
not, there are only a few lines to check. Next we compute the sum of squares of dx and dy: def distance(x1, ...
Composition As you should expect by now, you can call one function from within another. As an example, w ...
Boolean Functions Functions can return booleans, which is often convenient for hiding complicated tests inside fun ...
More Recursion We have only covered a small subset of Python, but you might be interested to know that this ...
if n == 0: return 1 Otherwise, and this is the interesting part, we ...
In the last frame, the local variables recurse and result do not exist, because the branch that creates them d ...
Leap of Faith Following the flow of execution is one way to read programs, but it can quickly become overwhelmi ...
One More Example After factorial, the most common example of a recursively defined mathematical function is fi ...
«
1
2
3
4
5
6
7
8
9
10
»
Free download pdf