Skip to main content
Top

2019 | Book

The Python Workbook

A Brief Introduction with Exercises and Solutions

insite
SEARCH

About this book

This student-friendly textbook encourages the development of programming skills through active practice by focusing on exercises that support hands-on learning. The Python Workbook provides a compendium of 186 exercises, spanning a variety of academic disciplines and everyday situations. Solutions to selected exercises are also provided, supported by brief annotations that explain the technique used to solve the problem, or highlight a specific point of Python syntax.

This enhanced new edition has been thoroughly updated and expanded with additional exercises, along with concise introductions that outline the core concepts needed to solve them. The exercises and solutions require no prior background knowledge, beyond the material covered in a typical introductory Python programming course.

Features: uses an accessible writing style and easy-to-follow structure; includes a mixture of classic exercises from the fields of computer science and mathematics, along with exercises that connect to other academic disciplines; presents the solutions to approximately half of the exercises; provides annotations alongside the solutions, which explain the approach taken to solve the problem and relevant aspects of Python syntax; offers a variety of exercises of different lengths and difficulties; contains exercises that encourage the development of programming skills using if statements, loops, basic functions, lists, dictionaries, files, and recursive functions.

Undergraduate students enrolled in their first programming course and wishing to enhance their programming abilities will find the exercises and solutions provided in this book to be ideal for their needs.

Table of Contents

Frontmatter

Exercises

Frontmatter
Chapter 1. Introduction to Programming
Abstract
Programming is a skill that is best learned through hands-on experience. This chapter concisely introduces the concepts that a new programmer will need to create their first programs in Python and includes 34 exercises that will let them put those concepts into practice. The particular concepts that this chapter introduces include:
  • Generating output with print statements,
  • Reading input values from the user,
  • Storing and manipulating integer, floating-point and string values,
  • Converting values from one data type to another,
  • Calling functions to perform a task, and
  • Formatting output.
Learning about these concepts and writing programs that use them effectively is an important step toward the creation of larger programs that solve more interesting problems.
Ben Stephenson
Chapter 2. Decision Making
Abstract
Most programs that solve interesting problems need to make decisions so that they can perform different actions in response to different input values. Python includes several decision making constructs, all of which evaluate a condition to determine whether or a statement (or group of statements) that is present in the program is executed or skipped. These decision making constructs include:
  • An if statement that allows a group of statements to be either executed or skipped
  • An if-else statement that causes exactly one of two groups of statements to execute
  • An if-elif-else statement that causes exactly one of several groups of statements to execute
  • An if-elif statement that causes at most one of several groups of statements to execute
All of these decision making constructs include a condition that is evaluate to determine which group of statements, if any, executes. The conditions can include relational operators and Boolean operators, both of which are discussed in this chapter. It ends with 28 exercises that allow the reader to put the concepts introduced in this chapter into practice.
Ben Stephenson
Chapter 3. Repetition
Abstract
Loops allow a program to perform a large number of calculations with only a few lines of code. Python includes two types of loops: while loops and for loops. Both while loops and for loops cause a program’s execution to return to an earlier point and make a decision about whether or not a group of statements should be executed again.
  • A while loop executes a group of statements as long as, or while, something is true. Like an if statement, a while loop has a condition which can include both relational operators and Boolean operators.
  • A for loop executes a group of statements once for each value in a collection. A collection of integers can be constructed using Python’s range function. For loops can also be used with collections of values like lists and dictionaries which are discussed in later chapters.
This chapter ends with 22 exercises that allow the reader to practice using both while loops and for loops. In some cases the exercises tell the reader which type of loop to use. Other exercises require the user to analyze the problem carefully and make a decision about which type of loop is most suitable for solving it.
Ben Stephenson
Chapter 4. Functions
Abstract
Using functions effectively makes larger programs easier to develop, test, debug, and enhance.
  • Functions let a programmer write code once and then call it from many locations.
  • Functions allow individual parts of a program to be tested separately.
  • Functions allow details to be set aside once part of a program is complete.
These benefits are achieved by allowing a programmer to name a collection of Python statements and set them aside for later use. Then, whenever those statements are needed, they can be executed. When the statements in a function finish executing, control returns to the location where the function was called and the program continues to execute from that point. A function can be called many times from many different locations within a program.
This chapter explores defining functions, calling functions, and returning values from functions. Each of its 25 exercises is an opportunity for the reader to put these concepts into practice.
Ben Stephenson
Chapter 5. Lists
Abstract
Lists help programmers manage larger amounts of data by allowing several (or even many) values to be stored in one variable. As a program executes it can transform a list (and the values within it) in numerous ways. Creatively applying these transformations, together with loops and decision making constructs, allows complex problems involving large amounts of data to be solved with a small number of variables. The specific list concepts explored in this chapter include:
  • Creating a new list,
  • Modifying a list by appending, inserting, updating and deleting elements,
  • Searching a list for a value,
  • Displaying some or all of the elements in a list, and
  • Writing a function that takes a list as an argument or returns a list as a result.
The chapter concludes with 26 exercises that allow the reader to use these concepts to solve a variety of problems.
Ben Stephenson
Chapter 6. Dictionaries
Abstract
A dictionary is a data structure that stores pairs, each of which consists of a key and its associated value. A dictionary may contain one key-value pair, many key-value pairs, or no key-value pairs. As a program runs:
  • New dictionaries can be created,
  • Keys can be used to retrieve values from a dictionary,
  • New key-value pairs can be added to a dictionary,
  • The presence or absence of a particular key or value can be determined,
  • Existing key-value pairs can be removed from a dictionary, and
  • The value associated with a key can be changed.
There is no way to add a key to a dictionary without an associated value, and similarly, there is no way to add a value to a dictionary without a key.
Some association problems can be solved with if statements, but using a dictionary for such problems often results in a solution that is more concise, elegant and flexible. The 13 exercises at the conclusion of this chapter provide ample opportunity for the reader to solve problems with dictionaries.
Ben Stephenson
Chapter 7. Files and Exceptions
Abstract
When files are used effectively programs can read the data that they need from disk instead of requiring the user to enter it each time the program runs. Similarly, files allow the results generated by a program to be stored in a more permanent manner than printing the output on the screen. Both file input and file output are commonly used by programs that operate on larger amounts of data. This chapter explores the following topics related to files:
  • Opening a file for reading, writing or appending,
  • Reading data from a file,
  • Writing data to a file, and
  • Closing a file.
In addition, this chapter also explores the use of command line arguments and exceptions, both in programs that make use of files and in those that do not. The chapter concludes with 24 exercises that allow the reader to put all of these concepts into practice.
Ben Stephenson
Chapter 8. Recursion
Abstract
A recursive function is a function that calls itself. Such functions normally include one or more base cases and one or more recursive cases. The base case is a version of the problem that has a known solution, or a solution that can be computed without using a smaller version of the same problem. When the base case for a recursive function executes the function’s result is computed without making a recursive call. In contrast, a recursive case uses one or more other versions of the same problem to compute its result. This results in one or more recursive calls when the program runs, typically to smaller or simpler versions of the problem. The recursive calls end when they reach the base case.
This chapter includes 14 exercise, all of which are well suited to being solved with one or more recursive functions. While some of the exercises involve only small recursive functions, others require recursion to be used together with a variety of Python language features that were introduced in previous chapters.
Ben Stephenson

Solutions

Frontmatter
Chapter 9. Solutions to the Introduction to Programming Exercises
Abstract
This chapter contains the solutions to selected exercises from Chap. 1, “Introduction to Programming”.
Ben Stephenson
Chapter 10. Solutions to the Decision Making Exercises
Abstract
This chapter contains the solutions to selected exercises from Chap. 2, “Decision Making”.
Ben Stephenson
Chapter 11. Solutions to the Repetition Exercises
Abstract
This chapter contains the solutions to selected exercises from Chap. 3, “Repetition”.
Ben Stephenson
Chapter 12. Solutions to the Function Exercises
Abstract
This chapter contains the solutions to selected exercises from Chap. 4, “Functions”.
Ben Stephenson
Chapter 13. Solutions to the List Exercises
Abstract
This chapter contains the solutions to selected exercises from Chap. 5, “Lists”.
Ben Stephenson
Chapter 14. Solutions to the Dictionary Exercises
Abstract
This chapter contains the solutions to selected exercises from Chap. 6, “Dictionaries”.
Ben Stephenson
Chapter 15. Solutions to the File and Exception Exercises
Abstract
This chapter contains the solutions to selected exercises from Chap. 7, “Files and Exceptions”.
Ben Stephenson
Chapter 16. Solutions to the Recursion Exercises
Abstract
This chapter contains the solutions to selected exercises from Chap. 8, “Recursion”.
Ben Stephenson
Backmatter
Metadata
Title
The Python Workbook
Author
Dr. Ben Stephenson
Copyright Year
2019
Electronic ISBN
978-3-030-18873-3
Print ISBN
978-3-030-18872-6
DOI
https://doi.org/10.1007/978-3-030-18873-3

Premium Partner