Most chapters have small exercises sprinkled throughout the text. These exercises are there to enlighten a point or for you to do a quick check if you understood the material up to that moment. You should try to do these exercises immediately when you encounter them. Answers to these exercises are seldom provided, because if you understood the material, they should be really easy to do, while if you did not understand the material, you should either re-read the chapter until you do, or ask someone for assistance.
At the end of most chapters, a separate “Exercises” section is given, with one or more numbered exercises. You are supposed to do all of these exercises, and you should be able to do them independently (i.e., without help of other people and without looking up solutions from outside sources). Answers to these numbered exercises are provided in the back of the book, in Appendix 34, and can also be downloaded from the website associated with this book (http://www.spronck.net/pythonbook1). I wish to stress the following points:
You should work on the numbered exercises until you have solved them. Do not dabble a bit and then look up the answer. Such an approach is utterly useless. There is no way that you are going to learn programming if you do not think about algorithms, write code, and test code. If you cannot solve an exercise even after you have worked on it for quite some time, it is better to ask for assistance than to just look up the answer. Being unable to solve an exercise means that there is something in the material that you have not grasped yet, and it is important that you identify what that is, and get to grips with it.
You should do all of the exercises. The only way to learn programming is by practicing. You will have to write lots and lots of code before you have internalized the practice of programming. The few exercises that I place at the end of the chapters are not enough to accomplish that, but at least they are a start. If you cannot even bother to do all of those, you should not bother to try to learn programming.
You should try to do the exercises independently. Working in teams on the exercises will allow one member of the team to learn, while the rest sits by and learns nothing. Students often say that they have a method of learning from assignments that involves working on them in small groups and discussing. That may be fine for analyzing texts or setting up an experiment, but in general does not work for coding. Watching someone write code teaches you very little about writing code. You have to write code by yourself.
For none of the exercises you need information that was not discussed in the book up to that point. While there definitely may be easier ways to do some of the exercises if you would use Python constructs that I did not discuss yet at the time you get to the exercises, you do not need them. The purpose of the exercises is to practice with the discussed material. They are not meant to let you investigate future material. Even if you are aware of different ways to solve an exercise, try to do it with only the material discussed. Once you have done that, if you want to return to an exercise later and solve it in a different way, that is, of course, fine.
Once you have solved an exercise by yourself and have tested it extensively, then and only then you should compare it to the answer that I provided. You may find that it is different from yours. That does not mean that your answer is wrong! There are usually very many ways to solve a programming problem. Some might be “better” in some way than others. But there are many answers that are equally correct. Moreover, in this book it is important that you learn to solve a problem by coding, not that you learn to code the most efficient solution to a problem. Just being able to solve the problem suffices, making solutions more efficient is of much lesser importance. For instance, “being efficient” is less important than “being easy to understand” and “being easy to maintain.”
For starters, here are two numbered exercises for the first chapter. Learn from them.