Python Programming Exercises, Gently Explained
3
Special Cases and Gotchas – Describes common mistakes or surprising ―gotchas‖ you
may encounter when writing code for the solution. Some exercises have special
cases that
your solution will need to address.
Solution Template – A copy of my own solution for the exercise, with selected parts
replaced by blanks for you to fill in. Your solution can still be correct if it doesn’t match
mine. But if you’re having trouble knowing where to start with your program, these
templates provide some but not all of the solution code.
Many solution programs to the exercises in this book are only a few lines of code long, and none
of them are longer than 50 lines. If you find yourself writing
several hundred lines of code, you’re
probably overthinking the solution and should probably read the
Exercise Description section
again.
Each exercise has several assert statements that detail the expected results from your solution.
In Python, assert statements are the assert keyword followed by a condition. They stop the
program with an AssertionError if their condition is False. They are a basic sanity check and,
for this book, tell you the expected behavior of your solution. For example, Exercise #3, ―Odd &
Even‖ has assert isOdd(9999) == True, which tells you that the correct solution involves the
isOdd()
function returning True when passed an argument of 9999.
Examine all of the assert
statements for an exercise before writing your solution program.
Some exercises don’t have assert statements, but rather show you the output that your
solution should produce. You can compare this output to your solution’s output to verify that your
solution is correct.
I provide complete solutions in Appendix A. But there are many ways to solve any given
programming problem. You don’t need to produce an identical copy of my solutions; it just needs to
pass the assert statements. I wrote my solutions to be conceptually simple and easy for the
intended audience of this book to understand. They produce correct results but aren’t necessarily the
fastest or most efficient solutions. As you get
more experience programming, you can revisit the
exercises in this book and attempt to write high-performance solutions to them.
Python Programming Exercises, Gently Explained
4
Most of the solutions involve writing functions that return values based on the arguments passed
to the function call. In these cases, you can write your code assuming that the arguments are always of
the expected data type. So for example, if your function expects an integer,
it will have to handle
arguments like 42, 0, or -3 but doesn’t have to handle arguments like 3.14 or 'hello'.
Keep in mind that there is a difference between a
parameter and an
argument. When we
define a
function such as Exercise #3’s def isOdd(number):, the local variable number is a
parameter.
When we call this function with isOdd(42), the integer 42 is an
argument. The argument is passed to
the function and assigned as the value to the parameter. It’s easy to use ―parameter‖ and ―argument‖
interchangeably, but this book uses them in their correct technical sense.
Python is a practical language with many helpful functions in its standard library.
Some exercises
will explicitly forbid you from using these functions. For example, Exercise #34, ―Uppercase Letters‖
tasks you to write code to convert a string to uppercase letters. Using Python’s built-in upper()
string method to do this for you would defeat the purpose of the exercise, so the
Exercise
Dostları ilə paylaş: