Prev - Forward | Table of Contents | Next - #1 Hello, World!

Introduction

 

“How can I get better at programming?” I see this common question often from those who have started their programming journey. Of course, there are plenty of Python programming tutorials for total beginners. However, these tutorials can carry the reader so far. After finishing these lessons, readers often find their skills more than capable for yet another “Hello, world!” tutorial but not advanced enough to begin writing their own programs. They find themselves in the so-called “tutorial hell.” They learn the basic syntax of a programming language, but wonder where to begin when it comes to applying them to their own programs.

Programming is like any other skill: it gets better with practice. I’ve chosen the exercises in this book because they are short and straightforward. Each exercise involves only a handful of programming concepts and you can solve them in a single session at the computer. If you’ve been intimidated by “competitive programming” or “hacker challenge” websites, you’ll find these to be an instructive and gentler way to level up your coding skills.

When I wrote Automate the Boring Stuff with Python, I wanted to teach programming to as many non-programmers as possible. So, I released that book (and all of my other programming books) under a Creative Commons license so they could be downloaded and shared for free from my website https://inventwithpython.com. You can pay for a physical print book but it’s important to me to lower all barriers to access, so the books are available online for free. I’ll cite some of these books in the Further Reading section of the exercises in this book.

What Will This Book Do For You?

This book offers 42 programming exercises for inexperienced Python programmers. I’ve gathered them into this book and combined them with plain-English explanations. You can read the description for each exercise and start on the solution immediately. If you need further help, you can read about the programming concepts you’ll need to know for the solution. You can also find out about any surprising “gotchas” you might encounter while writing your solution. Finally, if you still need help, I provide a fill-in-the-blank template of the solution. Try to resist the temptation to immediately jump to the hints; try to solve these exercises yourself first.

As you work through these exercises, you’ll find that some use the same coding techniques as other problems. A lot of programming expertise develops this way: being able to solve a problem isn’t about how smart you are but if you’ve seen similar problems before. My aim isn’t to stump you with complex, contrived programming challenges but help you explore simple problems with gentle explanations.

Prerequisites

While this isn’t a book to teach programming to complete beginners, you don’t need to be a programming expert before tackling the challenges here. Any beginner’s resource, such as one of my free books, Automate the Boring Stuff with Python or Invent Your Own Computer Games with Python, is more than enough to prepare you for these exercises. You can find these books for free at https://inventwithpython.com. I also recommend Python Crash Course by Eric Matthes as an excellent book for people with no programming experience. These books are published by No Starch Press, which has an extensive library of high-quality Python books at https://nostarch.com/catalog/python.

To solve the exercises in this book, you should already know, or at least be familiar with, the following Python programming concepts:

·       Downloading and installing the Python interpreter

·       Entering Python code into the interactive shell and into .py source code files

·       Storing values in variables with the = assignment operator

·       Knowing the difference between data types such as integers, strings, and floats

·       Math, comparison, and Boolean operators such as +, <=, and not.

·       How Python evaluates expressions such as (2 * 3) + 4 or 'Sun' + 'day'

·       Getting and displaying text with the input() and print() functions

·       Working with strings and string methods such as upper() or startswith()

·       Using if, elif, and else flow control statements

·       Using for loops and while loops, along with break and continue statements

·       Defining your own functions with parameters and returning values

·       Using data structures such as lists, tuples, and dictionaries

·       Importing modules such as math or random to use their functions

You don’t need a mastery of classes and object-oriented programming. You don’t need to know advanced concepts such as machine learning or data science. But if you’d like to eventually build your skills to advanced topics like these, this book can help you start along that path.

Even if you’ve moved past the beginner stage, this book can help you assess your programming ability. By the time you’re ready to start applying to junior software developer positions, you should be able to solve all of the exercises in this book easily.

About the Exercises

The exercises are generally ordered from least to most difficult. But you don’t have to solve them in order, so feel free to jump around to any exercises that you find interesting.

Each exercise has the following sections:

·       Exercise Description – A description of the exercise, followed by a list of assert statements that specify the results it expects from your solution program. There is also a list of prerequisite concepts you’ll need to understand to solve this exercise. If you don’t understand any of them, you can do an internet search of these terms along with “Python” to find explanations of them. This is the only section you need to read to solve the exercise. The later sections provide additional hints if you need them.

·       Solution Design – Additional information about concepts you’ll need to know to write a solution, along with a brief, gentle explanation of them.

·       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.


 

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 Description section points out that your solution shouldn’t call it.

I recommend solving the exercises in this book repeatedly until it becomes effortless. If you can solve an exercise once, try solving it again a couple of weeks later or without looking at the hints in the later sections. After a while, you’ll find yourself quickly being able to come up with strategies to solve these exercises.

Let’s begin!

Prev - Forward | Table of Contents | Next - #1 Hello, World!