Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $9.99/month after trial. Cancel anytime.

Python Workout: 50 ten-minute exercises
Python Workout: 50 ten-minute exercises
Python Workout: 50 ten-minute exercises
Ebook628 pages9 hours

Python Workout: 50 ten-minute exercises

Rating: 0 out of 5 stars

()

Read preview

About this ebook

The only way to master a skill is to practice. In Python Workout, author Reuven M. Lerner guides you through 50 carefully selected exercises that invite you to flex your programming muscles. As you take on each new challenge, you’ll build programming skill and confidence.

Summary
The only way to master a skill is to practice. In Python Workout, author Reuven M. Lerner guides you through 50 carefully selected exercises that invite you to flex your programming muscles. As you take on each new challenge, you’ll build programming skill and confidence. The thorough explanations help you lock in what you’ve learned and apply it to your own projects. Along the way, Python Workout provides over four hours of video instruction walking you through the solutions to each exercise and dozens of additional exercises for you to try on your own.

Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.

About the technology
To become a champion Python programmer you need to work out, building mental muscle with your hands on the keyboard. Each carefully selected exercise in this unique book adds to your Python prowess—one important skill at a time.

About the book
Python Workout presents 50 exercises that focus on key Python 3 features. In it, expert Python coach Reuven Lerner guides you through a series of small projects, practicing the skills you need to tackle everyday tasks. You’ll appreciate the clear explanations of each technique, and you can watch Reuven solve each exercise in the accompanying videos.

What's inside

    50 hands-on exercises and solutions
    Coverage of all Python data types
    Dozens more bonus exercises for extra practice

About the reader
For readers with basic Python knowledge.

About the author
Reuven M. Lerner teaches Python and data science to companies around the world.

Table of Contents

1 Numeric types

2 Strings

3 Lists and tuples

4 Dictionaries and sets

5 Files

6 Functions

7 Functional programming with comprehensions

8 Modules and packages

9 Objects

10 Iterators and generators
LanguageEnglish
PublisherManning
Release dateJul 2, 2020
ISBN9781638357223
Python Workout: 50 ten-minute exercises
Author

Reuven M. Lerner

Reuven M. Lerner, an independent consultant for more than two decades, teaches Python, data science, and Git to companies around the world. His Better developers newsletter and blog are read by thousands of Python developers each week. Reuven has written a monthly column, "At the Forge," for Linux Journal since 1996 and is a panelist on the weekly Freelancers Show podcast. Reuven lives with his wife and three children in Modi'in, Israel, and can be reached at https://lerner.co.il/ or on Twitter at @reuvenmlerner.

Related to Python Workout

Related ebooks

Programming For You

View More

Related articles

Reviews for Python Workout

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Python Workout - Reuven M. Lerner

    Python Workout

    50 ten-minute exercises

    Reuven Lerner

    To comment go to liveBook

    Manning

    Shelter Island

    For more information on this and other Manning titles go to

    manning.com

    Copyright

    For online information and ordering of these  and other Manning books, please visit manning.com. The publisher offers discounts on these books when ordered in quantity.

    For more information, please contact

    Special Sales Department

    Manning Publications Co.

    20 Baldwin Road

    PO Box 761

    Shelter Island, NY 11964

    Email: [email protected]

    ©2020 by Manning Publications Co. All rights reserved.

    No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.

    Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.

    ♾ Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine.

    ISBN: 9781617295508

    dedication

    Dedicated to my three children, who are also my best teachers— 

    Atara Margalit, Shikma Bruria, and Amotz David.

    brief contents

      1  Numeric types

      2  Strings

      3  Lists and tuples

      4  Dictionaries and sets

      5  Files

      6  Functions

      7  Functional programming with comprehensions

      8  Modules and packages

      9  Objects

    10  Iterators and generators

    contents

    preface

    acknowledgments

    about this book

    about the author

    about the cover illustration

    1 Numeric types

    Useful references

    Exercise 1 ■ Number guessing game

    Exercise 2 ■ Summing numbers

    Exercise 3 ■ Run timing

    Exercise 4 ■ Hexadecimal output

    2 Strings

    Useful references

    Exercise 5 ■ Pig Latin

    Exercise 6 ■ Pig Latin sentence

    Exercise 7 ■ Ubbi Dubbi

    Exercise 8 ■ Sorting a string

    3 Lists and tuples

    Exercise 9 ■ First-last

    Exercise 10 ■ Summing anything

    Exercise 11 ■ Alphabetizing names

    Exercise 12 ■ Word with most repeated letters

    Exercise 13 ■ Printing tuple records

    4 Dictionaries and sets

    Hashing and dicts

    Sets

    Exercise 14 ■ Restaurant

    Exercise 15 ■ Rainfall

    Exercise 16 ■ Dictdiff

    Exercise 17 ■ How many different numbers?

    5 Files

    Exercise 18 ■ Final line

    Exercise 19 ■ /etc/passwd to dict

    Exercise 20 ■ Word count

    Exercise 21 ■ Longest word per file

    Exercise 22 ■ Reading and writing CSV

    Exercise 23 ■ JSON

    Exercise 24 ■ Reverse lines

    6 Functions

    Exercise 25 ■ XML generator

    Exercise 26 ■ Prefix notation calculator

    Exercise 27 ■ Password generator

    7 Functional programming with comprehensions

    Exercise 28 ■ Join numbers

    Exercise 29 ■ Add numbers

    Exercise 30 ■ Flatten a list

    Exercise 31 ■ Pig Latin translation of a file

    Exercise 32 ■ Flip a dict

    Exercise 33 ■ Transform values

    Exercise 34 ■ (Almost) supervocalic words

    Exercise 35a ■ Gematria, part 1

    Exercise 35b ■ Gematria, part 2

    8 Modules and packages

    Exercise 36 ■ Sales tax

    Exercise 37 ■ Menu

    9 Objects

    Exercise 38 ■ Ice cream scoop

    Exercise 39 ■ Ice cream bowl

    Exercise 40 ■ Bowl limits

    Exercise 41 ■ A bigger bowl

    Exercise 42 ■ FlexibleDict

    Exercise 43 ■ Animals

    Exercise 44 ■ Cages

    Exercise 45 ■ Zoo

    10 Iterators and generators

    Exercise 46 ■ MyEnumerate

    Exercise 47 ■ Circle

    Exercise 48 ■ All lines, all files

    Exercise 49 ■ Elapsed since

    Exercise 50 ■ MyChain

    index

    front matter

    preface

    In many ways, learning a programming language is like learning a foreign (human) language. You can take a course, understand the subject, and even do well on the final exam. But when it comes time to actually use the language, you can find yourself flustered, unsure of just what syntax to use, or what’s the most appropriate way to phrase something—let alone be unable to understand native speakers.

    That’s where practice comes in. Practicing a foreign language gives you greater fluency and confidence, allowing you to engage in deeper and more interesting conversations. Practicing Python allows you to solve problems more quickly and easily, while simultaneously writing more readable and maintainable code. The improvement happens over time, as you use the language in new and varied situations. It often isn’t obvious that you have improved. And yet, when you look back to how you were using the language just a few months before, the difference is stark.

    This book isn’t meant to teach you Python. Rather, it’s meant to give you the practice you need to achieve greater fluency. After going through the exercises in this book—not just skimming through the questions and peeking at the answers—you will write more readable, more idiomatic, and more maintainable Python code.

    Python Workout is the result of conversations with students in my corporate Python training classes. Once the course was over, they often asked where they could get additional practice, to continue improving their skills. This book draws upon the hands-on labs that I give my students, as well as discussions that I have had with them during and after class.

    The exercises are designed to help you internalize some of the core ideas in Python: core data structures, functions, comprehensions, object-oriented programming, and iterators. These might seem like simple topics, perhaps even too simple for a book of exercises. But all of Python, from the largest application to the smallest script, is based on these building blocks. Knowing them well is a crucial part of being a fluent Python developer. I often say that ignoring these building blocks in favor of more complex topics is akin to a chemistry student ignoring the elements in favor of real chemicals.

    I can personally attest to the power of practice, not just as a Python instructor, but also as a student. For several years, I’ve been learning Chinese, in no small part because I travel to China every few months to teach Python courses there. Each lesson that I take, and every exercise that I do, doesn’t seem to advance my fluency very much. But when I return to China after an absence of several months, I find that the practice has indeed helped, and that I’m able to communicate more easily with the locals.

    I’m still far from fluent in Chinese, but I’m making progress, and I delight in looking back and seeing how far I’ve come. I hope and expect that Python Workout will do the same for you, advancing your understanding and fluency with each passing day.

    acknowledgments

    It might be a cliché that writing a book is a cooperative endeavor, but it also happens to be true. I thus want to thank and acknowledge a number of people, without whom this book wouldn’t be possible.

    First and foremost, I want to thank the thousands of students I’ve had the privilege of teaching over the years in my corporate Python training courses. It is thanks to their questions, suggestions, insights, and corrections that the exercises, solutions, and explanations are in their current state.

    Thanks also to the many subscribers to my weekly Better developers newsletter (https://BetterDevelopersWeekly.com/), who often take the time to comment on and correct topics about which I’ve written. I’ve learned a great deal from them, and often put such insights to use in my teaching.

    Next, Philip Guo (http://pgbovine.net/) is an assistant professor of Cognitive Science at UC San Diego. He’s also the author and maintainer of the Python Tutor site, an invaluable tool that I often use in my courses, and that I encourage my students to use as they puzzle through their Python code. I’ve used many screenshots from the Python Tutor in this book, and almost every exercise solution includes a link to that site, so that you can walk through the code yourself.

    Thanks to everyone who works on Python, from the core developers, to those who write and blog about the language, to those who contribute packages. The Python ecosystem is an impressive technical accomplishment, but I have also been impressed by the number of truly helpful, decent, and warm people I’ve met who are responsible for those accomplishments.

    A host of people at Manning have contributed to the book, making it far better than anything I could have done on my own. (And there’s proof; the self-published predecessor to this book wasn’t nearly as good as what you’re reading!) I’ve worked closely with a few of them, all of whom have combined skill and patience in helping this book come to life. Michael Stephens saw the promise of such an exercise-centric book and encouraged me to work with Manning. Frances Lefkowitz was not only skilled at editing the text and pointing out where it could be improved, broken up, or illustrated; she also shepherded me through the book-writing process. Gary Hubbard and Ignacio Beltran Torres both provided countless technical insights and edits, finding bugs and helping me to tighten up bad explanations. And Carl Quesnel impressed me to no end with his detailed edits of the final text.

    To all the reviewers: Annette Dewind, Bill Bailey, Charles Daniels, Christoffer Fink, David Krief, David Moravec, David R. Snyder, Gary Hubbard, Geoff Craig, Glen Sirakavit, Jean-François Morin, Jeff Smith, Jens Christian B. Madsen, Jim Amrhein, Joe Justesen, Kieran Coote-Dinh, Mark Elston, Mayur Patil, Meredith Godar, Stefan Trost, Steve Love, Sushant Bhosale, Tamara L. Fultz, Tony Holdroyd, and Warren Myers, your suggestions helped make this a better book.

    Finally, my family has been patient throughout my business and academic career. They were helpful and understanding as I grew my training practice, completed a PhD, and then began to travel the world teaching Python. When it comes to this book, they were actually patient twice: first when I self-published it on my website, and then again when it was upgraded, expanded, and improved (rather dramatically) to become what you’re now reading. Thanks to my wife, Shira, and to my children, Atara, Shikma, and Amotz, for their understanding and appreciation.

    about this book

    Python Workout isn’t designed to teach you Python, although I hope and expect that you’ll learn quite a bit along the way. It is meant to help you improve your understanding of Python and how to use it to solve problems. You can think of it as a workbook, one whose power and learning potential depends on you. The more effort you put into this book, the more you’ll get out of it.

    In other words, this is a book that you should not just read or page through. For the learning to happen, you’ll have to spend time answering the questions and making inevitable mistakes. There’s a world of difference between reading a solution and writing the solution yourself. I hope that you’ll invest time in answering the problems; I promise that it’s an investment that’ll repay you handily in the future.

    By the time you finish Python Workout, you will have solved many problems having to do with core data structures, functions, comprehensions, modules, objects, and iterators. You will understand how to use them effectively and will know how to use them in various idiomatic ways. Once you’ve finished with these exercises, you’ll find it easier to design and write Python programs for work and pleasure.

    Note that it’s not cheating to look for help in the Python documentation, or even on such sites as Stack Overflow (https://StackOverflow.com/). No developer can possibly remember everything that they need in their day-to-day work. I do hope that as you progress through the book, and then use Python in your career, you’ll find yourself consulting such documentation less, or only for more advanced topics.

    Who should read this book

    This book is aimed at developers who have taken a Python course, or perhaps read an introductory book on the language. Indeed, the bulk of these exercises are aimed at people who are in my intro Python course, or who have recently finished taking it. You should already have an understanding of basic constructs, such as if and for, as well as the core data structures, such as strings, lists, tuples, and dictionaries.

    But there’s a difference between having a passing familiarity with these topics and knowing how to apply them to actual problems. If you can get by with Python but find yourself going to Stack Overflow many times each day, then this book will help you to become more confident and independent as you write Python code. I’d argue that if you have been using Python regularly for less than six months, then you’ll gain from this book.

    How this book is organized: a roadmap

    This book has ten chapters, each focusing on a different aspect of Python. However, the exercises in each chapter will use techniques from other chapters. For example, nearly every exercise asks you to write a function or a class, even though functions are introduced in chapter 6 and classes are introduced in chapter 9. Think of the names as general guidelines, rather than strict rules, for what you’ll be practicing and learning in each chapter.

    The chapters are

    Numeric types: Integers and floats—and converting between numbers and strings.

    Strings: Working with strings, and seeing them not just as text, but also as sequences over which you can iterate.

    Lists and tuples: Creating, modifying (in the case of lists), and retrieving from lists and tuples.

    Dictionaries and sets: Exploring the different ways you can use dicts, and some of their useful methods. Also, some uses for sets, which are related to dicts.

    Files: Reading from and writing to files.

    Functions: Writing functions, including nested functions. Exploring Python’s scoping rules.

    Functional programming with comprehensions: Solving problems with list, set, and dict comprehensions.

    Modules and packages: Writing and using modules in a Python program.

    Objects: Creating classes, writing methods, using attributes, and understanding inheritance.

    Iterators and generators: Adding the iterator protocol to classes, writing generator functions, and writing generator comprehensions.

    Exercises form the main part of each chapter. For each exercise, you’ll find five components:

    Exercise: A problem statement for you to tackle.

    Working it out: A detailed discussion of the problem and how to solve it.

    Solution: The solution code, along with a link to the code on the Python Tutor (pythontutor.com) site so you can execute it. Solution code, along with test code for each solution, is also available on GitHub at https://github.com/ reuven/python-workout

    Screencast solution: A short video demonstration, in which I walk you through the solution in a screencast. You can watch the video to see not just the answer, but the process I go through in trying to get to that answer. If you read this book in Manning’s liveBook platform, the screencast videos appear just after each solution. In the print and ebook, you’ll use a link to a navigation page (https://livebook.manning.com/video/python-workout), then select the exercise by number and name.

    Beyond the exercise: Three additional, related exercises. These questions are neither answered nor discussed in the book—but the code is downloadable, along with all other solution code from the book. (See the next section for details.) And you can discuss these additional exercises—and compare solutions—with other Python Workout readers in the book’s online forum in Manning’s liveBook platform.

    Alongside the exercises are numerous sidebars, each explaining a topic that often confuses Python developers. For example, there are sidebars on f-strings, variable scoping, and what happens when you create a new object. The book also contains numerous hints, tips, and notes—all pointers meant to help you improve your Python coding fluency and to warn you away from repeating mistakes that I have made many times over the years.

    About the code

    This book contains a great deal of Python code. Unlike most books, the code reflects what you are supposed to write, rather than what you’re supposed to read. If experience is any guide, some readers (maybe you!) will have better, more elegant, or more correct solutions than mine. If this is the case, then don’t hesitate to contact me.

    Solutions to all exercises, including the beyond the exercise questions, are available in two places: on Python Tutor (pythontutor.com), which provides an environment for you to execute the code, or on GitHub at https://github.com/reuven/python-workout, which allows you to download the code. Not only does this repository contain all the solutions, but it also includes pytest tests for each of them. (Unfamiliar with pytest? I strongly encourage you to read about it at https://pytest.org/, and to use it to check your code.)

    There are some small differences between the code in the GitHub repository and what is published in the book. In particular, the solutions in the book don’t include docstrings for functions, classes, and modules; the docstrings are included in the downloadable repository.

    This book contains many examples of source code both in numbered listings and in line with normal text. In both cases, source code is formatted in a fixed-width font like this to separate it from ordinary text. Sometimes code is also in bold to highlight code that has changed from previous steps in the chapter, such as when a new feature adds to an existing line of code.

    In many cases, the original source code has been reformatted; we’ve added line breaks and reworked indentation to accommodate the available page space in the book. In rare cases, even this was not enough, and listings include line-continuation markers (). Additionally, comments in the source code have often been removed from the listings when the code is described in the text. Code annotations accompany many of the listings, highlighting important concepts.

    As I mentioned previously, purchasing this book also gives you access to screencasts of me solving each of the exercises. I hope that the combination of the solution code (in print), explanation, Python Tutor link, downloadable code, pytest tests, and screencasts will help you to fully understand each solution and apply its lessons to your own code.

    Software/hardware requirements

    First and foremost, this book requires that you have a copy of Python installed. You can download and install it most easily from https://python.org/. I suggest installing the latest version available. There are also alternative ways to install Python, including the Windows Store or Homebrew for the Mac.

    This book will work with any version of Python from 3.6 and up. In a handful of places, the text describes features that are new in Python 3.7 and 3.8, but the solutions all use techniques that work with 3.6. The programs all work across operating systems, so no matter what platform you’re using, the exercises in this book will work.

    You don’t technically need to install an editor or IDE (integrated development environment) for Python, but it’ll certainly come in handy. Two of the most popular IDEs are PyCharm (from JetBrains) and VSCode (from Microsoft). Older and/or more traditional Python developers use vim or Emacs (my personal favorite). But at the end of the day, you can and should use whichever editor works best for you. Python doesn’t really care what version you’re using.

    liveBook discussion forum

    Purchase of Python Workout includes free access to a private web forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the author and from other users. To access the forum, go to https://livebook.manning.com/#!/book/python-workout/discussion. You can also learn more about Manning’s forums and the rules of conduct at https://livebook .manning.com/#!/discussion.

    Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the author can take place. It is not a commitment to any specific amount of participation on the part of the author, whose contribution to the forum remains voluntary (and unpaid). We suggest you try asking the author some challenging questions lest his interest stray! The forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.

    about the author

    Reuven M. Lerner is a full-time Python trainer. In a given year, he teaches courses at companies in the United States, Europe, Israel, India, and China, as well as to individuals around the world, via online courses. He blogs and tweets (@reuvenmlerner) frequently about Python and is a panelist on the Business of Freelancing podcast. Reuven lives in Modi’in, Israel with his wife and three children. You can learn more about Reuven at https://lerner.co.il/.

    about the cover illustration

    The figure on the cover of Python Workout is captioned Homme de la Terre de Feu, or A man from the Tierra del Fuego. The illustration is taken from a collection of dress costumes from various countries by Jacques Grasset de Saint-Sauveur (1757–1810), titled Costumes civils actuel de tous les peoples connus, published in France in 1784. Each illustration is finely drawn and colored by hand. The rich variety of Grasset de Saint-Sauveur’s collection reminds us vividly of how culturally apart the world’s towns and regions were just 200 years ago. Isolated from each other, people spoke different dialects and languages. In the streets or in the countryside, it was easy to identify where they lived and what their trade or station in life was just by their dress.

    The way we dress has changed since then and the diversity by region, once so rich, has faded away. It’s now hard to distinguish the inhabitants of different continents, let alone different towns, regions, or countries. Perhaps we have traded cultural diversity for a more varied personal life—certainly for a more varied and fast-paced technological life.

    At a time when it is hard to tell one computer book from another, Manning celebrates the inventiveness and initiative of the computer business with book covers based on the rich diversity of regional life of two centuries ago, brought back to life by Grasset de Saint-Sauveur’s pictures.

    1 Numeric types

    Whether you’re calculating salaries, bank interest, or cellular frequencies, it’s hard to imagine a program that doesn’t use numbers in one way or another. Python has three different numeric types: int, float, and complex. For most of us, it’s enough to know about (and work with) int (for whole numbers) and float (for numbers with a fractional component).

    Numbers are not only fundamental to programming, but also give us a good introduction to how a programming language operates. Understanding how variable assignment and function arguments work with integers and floats will help you to reason about more complex types, such as strings, tuples, and dicts.

    This chapter contains exercises that work with numbers, as inputs and as outputs. Although working with numbers can be fairly basic and straightforward, converting between them, and integrating them with other data types, can sometimes take time to get used to.

    Useful references

    Table 1.1 What you need to know

    Exercise 1 Number guessing game

    This first exercise is designed to get your fingers warmed up for the rest of the book. It also introduces a number of topics that will repeat themselves over your Python career: loops, user input, converting types, and comparing values.

    More specifically, programs all have to get input to do something interesting, and that input often comes from the user. Knowing how to ask the user for input not only is useful, but allows us to think about the type of data we’re getting, how to convert it into a format we can use, and what the format would be.

    As you might know, Python only provides two kinds of loops: for and while. Knowing how to write and use them will serve you well throughout your Python career. The fact that nearly every type of data knows how to work inside of a for loop makes such loops common and useful. If you’re working with database records, elements in an XML file, or the results from searching for text using regular expressions, you’ll be using for loops quite a bit.

    For this exercise

    Write a function (guessing_game) that takes no arguments.

    When run, the function chooses a random integer between 0 and 100 (inclusive).

    Then ask the user to guess what number has been chosen.

    Each time the user enters a guess, the program indicates one of the following:

    Too high

    Too low

    Just right

    If the user guesses correctly, the program exits. Otherwise, the user is asked to try again.

    The program only exits after the user guesses correctly.

    We’ll use the randint (http://mng.bz/mBEn) function in

    Enjoying the preview?
    Page 1 of 1