Coffee Break Python Workbook
Coffee Break Python Workbook
127 Python Puzzles to Push You from Zero to Hero in Your Coffee Breaks
About the Book
Puzzle-based Learning Python
This book is the chess grandmaster way of learning Python. It offers you 127 unique and brand-new Python puzzles. Every puzzle points to gaps in your knowledge, challenges you to guess a solution, and then explains potential solutions, in an easy-to-understand manner:
~~~
# Here’s one example puzzle:
my_list = [1, 1, 1, 1]
my_list[1::2] = [2, 3]
print(my_list)
# What’s the output of this code snippet?
~~~
An easy, fun, and effective way of learning Python -- day after day -- in your COFFEE BREAK PYTHON!
Here's what research says about puzzle-based learning:
"Students who were quizzed after studying a short text could recall significantly more information than students who were asked to reread it"
-- Karpicke, 2007, Elsevier Journal of Memory and Language
Practice testing is scientifically proven to generate up to 44% better learning retention and efficiency.
Simply put: quizzes and puzzles work! More than 100,000 Finxters and thousands of "Coffee Break Python" book customers have already successfully improved their skills with code puzzles.
Learning does not happen in a linear and orderly manner. It's a probabilistic, chaotic, and iterative process of creating knowledge gaps in your brain -- and filling them with just the right information you need.
That's the premise of the "Coffee Break Python" textbook series.
What will you get out of the book?
- Improve your level of deep Python code understanding.
- Surprise your peers with your newly acquired code speed reading skills.
- Enjoy the small daily doses of intellectual challenges. A Finxter once called it "Sudoku for coders"! ;)
- Improve your brain's working memory by hammering down the most important concepts.
- Learn all the basic Python syntax elements.
- Discover your own skill level by tracking your puzzle-solving performance.
- Compare your skill level against other coders: are you a grandmaster of code?
- Enjoy the fun of rushing over Python -- from "hello world" to "recursive Quicksort".
- Get the best of all Finxter Python cheat sheets to revive 80% of the Python features in 20% of the time.
- Get your dream job and rock future code interviews!
And take one step forward mastering the most popular programming language ON THE PLANET!
Bundles that include this book
Reader Testimonials

Vlad Bezden, Software engineer
I love it!
Thank you. I love it :) I enjoy reading this book! Keep up the good work.

Albrecht, German Workbook Reader
Great Book!
"A great book, I've learned a lot and marked every knowledge gap [...] Thank you very much!"
Table of Contents
1 Introduction 1
2 A Case for Puzzle-based Learning 5
2.1 Overcome the Knowledge Gap . . . . . . . 6
2.2 Embrace the Eureka Moment . . . . . . . 7
2.3 Divide and Conquer . . . . . . . . . . . . 8
2.4 Improve From Immediate Feedback . . . . 9
2.5 Measure Your Skills . . . . . . . . . . . . . 10
2.6 Individualized Learning . . . . . . . . . . . 12
2.7 Small is Beautiful . . . . . . . . . . . . . . 12
2.8 Active Beats Passive Learning . . . . . . . 14
2.9 Make Code a First-class Citizen . . . . . . 16
2.10 What You See is All There is . . . . . . . 18
3 The Elo Rating for Python 21
3.1 How to Use This Book . . . . . . . . . . . 21
3.2 How to Test and Train Your Skills? . . . . 23
3.3 What Can This Book Do For You? . . . . 27
4 A Quick Overview of the Python Language 31
4.1 Keywords . . . . . . . . . . . . . . . . . . 32
4.2 Basic Data Types . . . . . . . . . . . . . . 35
4.3 Complex Data Types . . . . . . . . . . . . 38
4.4 Classes . . . . . . . . . . . . . . . . . . . . 43
4.5 Functions and Tricks . . . . . . . . . . . . 46
5 Puzzles: Basic to Scholar 51
5.1 Printing values . . . . . . . . . . . . . . . 51
5.2 Basics of variables . . . . . . . . . . . . . . 53
5.3 Getting started with strings . . . . . . . . 54
5.4 Types of variables I . . . . . . . . . . . . . 55
5.5 Types of Variables II . . . . . . . . . . . . 56
5.6 Minimum . . . . . . . . . . . . . . . . . . 57
5.7 String Concatenation . . . . . . . . . . . . 58
5.8 Line Breaks I . . . . . . . . . . . . . . . . 59
5.9 Line Breaks II . . . . . . . . . . . . . . . . 60
5.10 List Length . . . . . . . . . . . . . . . . . 61
5.11 Comparison Operators I . . . . . . . . . . 62
5.12 Comparison Operators II . . . . . . . . . . 63
5.13 Multiple Initializations . . . . . . . . . . . 64
6 Puzzles: Scholar to Intermediate 67
6.1 Maximum . . . . . . . . . . . . . . . . . . 67
6.2 Memory addresses . . . . . . . . . . . . . . 68
6.3 Swapping Values . . . . . . . . . . . . . . 69
6.4 The Boolean Operator AND . . . . . . . . 70
6.5 The Boolean Operator OR . . . . . . . . . 72
6.6 Boolean Operators . . . . . . . . . . . . . 74
6.7 Arithmetic Expressions . . . . . . . . . . . 76
6.8 Integer Division and Modulo . . . . . . . . 78
6.9 Building Strings . . . . . . . . . . . . . . . 80
6.10 The len() Function . . . . . . . . . . . . 81
6.11 String Indices . . . . . . . . . . . . . . . . 82
6.12 The upper() Function . . . . . . . . . . . . 83
6.13 The lower() Function . . . . . . . . . . . . 84
6.14 Somebody Is Shouting . . . . . . . . . . . 85
6.15 Counting Characters . . . . . . . . . . . . 86
6.16 String Lengths . . . . . . . . . . . . . . . 87
6.17 Finding Characters in Strings . . . . . . . 88
6.18 Not Finding Characters in Strings . . . . . 89
6.19 Counting Letters . . . . . . . . . . . . . . 90
6.20 Min() and Max() of a String . . . . . . . . 91
6.21 Reversed Strings . . . . . . . . . . . . . . 92
6.22 String Equality . . . . . . . . . . . . . . . 93
6.23 Slicing I . . . . . . . . . . . . . . . . . . . 95
6.24 Slicing II . . . . . . . . . . . . . . . . . . . 96
6.25 Slicing III . . . . . . . . . . . . . . . . . . 97
6.26 Slicing IV . . . . . . . . . . . . . . . . . . 98
6.27 Slicing V . . . . . . . . . . . . . . . . . . . 99
6.28 Memory Addresses and Slicing . . . . . . . 100
6.29 Accessing List Items I . . . . . . . . . . . 101
6.30 Accessing List Items II . . . . . . . . . . . 103
6.31 List as Stack . . . . . . . . . . . . . . . . . 104
6.32 More String Operations . . . . . . . . . . 105
6.33 Checking for Substrings . . . . . . . . . . 106
6.34 Stripping String Boundaries . . . . . . . . 107
6.35 Strings: Stripping vs. Replacement . . . . 108
6.36 Glueing Strings Together . . . . . . . . . . 109
6.37 The Copy Operation . . . . . . . . . . . . 110
6.38 Growing List Contents I . . . . . . . . . . 111
6.39 Growing List Contents II . . . . . . . . . . 112
6.40 List Operations I . . . . . . . . . . . . . . 114
6.41 List Operations II . . . . . . . . . . . . . . 115
6.42 List Operations III . . . . . . . . . . . . . 116
6.43 List Operations IV . . . . . . . . . . . . . 117
6.44 List Operations V . . . . . . . . . . . . . . 117
6.45 List Operations VI . . . . . . . . . . . . . 118
6.46 List Operations VII . . . . . . . . . . . . . 119
6.47 List Operations VIII . . . . . . . . . . . . 120
6.48 List Operations IX . . . . . . . . . . . . . 121
6.49 List Operations X . . . . . . . . . . . . . . 122
6.50 Lists and the Range Function I . . . . . . 123
6.51 Lists and the Range Function II . . . . . . 124
6.52 Lists and the Range Function III . . . . . 125
6.53 Python’s Multiple Assignment I . . . . . . 126
6.54 Slice Assignments . . . . . . . . . . . . . . 127
6.55 Strings and Lists II . . . . . . . . . . . . . 128
6.56 String Comparisons . . . . . . . . . . . . . 129
6.57 From Booleans to Strings . . . . . . . . . . 130
6.58 Boolean Trickery I . . . . . . . . . . . . . 131
6.59 Boolean Trickery II . . . . . . . . . . . . . 132
6.60 Boolean Trickery III . . . . . . . . . . . . 133
6.61 Looping over Ranges . . . . . . . . . . . . 134
6.62 Reversed Loops . . . . . . . . . . . . . . . 135
6.63 Boolean Trickery IV . . . . . . . . . . . . 136
6.64 Lists and Memory Addresses . . . . . . . . 137
6.65 List Objects . . . . . . . . . . . . . . . . . 138
6.66 Boolean Tricks . . . . . . . . . . . . . . . 139
6.67 Complex Numbers . . . . . . . . . . . . . 140
6.68 Tuples . . . . . . . . . . . . . . . . . . . . 141
6.69 Multiple Assignments . . . . . . . . . . . . 142
6.70 Boolean Integer Conversion . . . . . . . . 143
6.71 The any() Function . . . . . . . . . . . . 144
6.72 The sum() Function . . . . . . . . . . . . 146
6.73 Accessing Complex Numbers . . . . . . . . 148
6.74 Tuple Confusion . . . . . . . . . . . . . . . 149
6.75 Understanding While ... Else (1/3) . . . . 150
6.76 Understanding While ... Else (2/3) . . . . 151
6.77 Understanding While ... Else (3/3) . . . . 153
6.78 Basic Arithmetic . . . . . . . . . . . . . . 155
6.79 Dictionary . . . . . . . . . . . . . . . . . . 156
6.80 Dictionary of Dictionaries . . . . . . . . . 158
6.81 Reverse Dictionary Index . . . . . . . . . . 160
6.82 Default Arguments . . . . . . . . . . . . . 162
7 Puzzles: Intermediate to Professional 165
7.1 Building Strings II . . . . . . . . . . . . . 165
7.2 String: Slicing and Indexing . . . . . . . . 167
7.3 Built-in Python Operations . . . . . . . . 168
7.4 Strings and Lists I . . . . . . . . . . . . . 169
7.5 Formatting Printouts . . . . . . . . . . . . 170
7.6 Floating Point Comparisons . . . . . . . . 172
7.7 Python’s Multiple Assignment II . . . . . 174
7.8 The Not-So-Obvious Case . . . . . . . . . 175
7.9 Rounding Values . . . . . . . . . . . . . . 176
7.10 Initializing Integers . . . . . . . . . . . . . 177
7.11 Basic Typing . . . . . . . . . . . . . . . . 178
7.12 Short Circuiting . . . . . . . . . . . . . . . 179
7.13 While Arithmetic . . . . . . . . . . . . . . 182
7.14 The Lambda Function . . . . . . . . . . . 183
7.15 Zip . . . . . . . . . . . . . . . . . . . . . . 184
7.16 Basic Filtering . . . . . . . . . . . . . . . . 186
7.17 List Comprehension . . . . . . . . . . . . . 188
7.18 Encryption by Obfuscation . . . . . . . . . 190
7.19 String Dictionary . . . . . . . . . . . . . . 193
7.20 Lambda Function . . . . . . . . . . . . . . 194
7.21 Dictionary of Dictionaries . . . . . . . . . 196
7.22 Sorting Dictionary Keys . . . . . . . . . . 198
7.23 Pythonic Loop Iteration . . . . . . . . . . 200
7.24 Filtering with List Comprehension . . . . . 202
7.25 Aggregating with List Comprehension . . . 204
7.26 Maximum of Tuples . . . . . . . . . . . . . 206
7.27 The Key Argument . . . . . . . . . . . . . 208
7.28 Puzzle 123 . . . . . . . . . . . . . . . . . . 210
7.29 Set Operations (1/2) . . . . . . . . . . . . 211
7.30 Set Operations (2/2) . . . . . . . . . . . . 212
7.31 Recursive Algorithm . . . . . . . . . . . . 214
7.32 Fibonacci . . . . . . . . . . . . . . . . . . 218
8 Final Remarks 221
Your skill level . . . . . . . . . . . . . . . 221
Where to go from here? . . . . . . . . . . 222
Other books by these authors
The Leanpub 60-day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms
Do Well. Do Good.
Authors have earned$11,577,045writing, publishing and selling on Leanpub, earning 80% royalties while saving up to 25 million pounds of CO2 and up to 46,000 trees.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers), EPUB (for phones and tablets) and MOBI (for Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them
Top Books
Recipes for Decoupling
Matthias NobackSignalR on .NET 6 - the Complete Guide
Fiodar SazanavetsLearn everything there is to learn about SignalR and how to integrate it with the latest .NET 6 and C# 10 features. Learn how to connect any type of client to SignalR, including plain WebSocket client. Learn how to build interactive applications that can communicate with each other in real time without making excessive calls.
The BDD Books - Discovery (Japanese Edition)
Gáspár Nagy, Seb Rose, and Yuya Kazamaウクライナ難民を支援 - 2022年5月末まで延長!
この本の売り上げの50%は、 https://unicef.hu/veszhelyzet-ukrajnaban と https://int.depaulcharity.org/fundraising-for-depaul-ukraine/ に寄付されます。
本書籍は、振る舞い駆動開発(Behavior Driven Development, BDD)や受け入れテスト駆動開発(Acceptance Test-Driven Development, ATDD)の発見フェーズを最大限に活用する方法を提供します。
The easiest way to learn design patterns
Fiodar SazanavetsLearn design patterns in the easiest way possible. You will no longer have to brute-force your way through each one of them while trying to figure out how it works. The book provides a unique methodology that will make your understanding of design patterns stick. It can also be used as a reference book where you can find design patterns in seconds.
Agile Testing Condensed Japanese Edition
Yuya Kazama, Janet Gregory, and Lisa CrispinJanet GregoryとLisa Crispinによる2019年9月発行の書籍『Agile Testing Condensed』の日本語翻訳版です。アジャイルにおいてどのような考えでテストを行うべきなのか簡潔に書かれています!
OpenIntro Statistics
David Diez, Christopher Barr, Mine Cetinkaya-Rundel, and OpenIntroA complete foundation for Statistics, also serving as a foundation for Data Science.
Leanpub revenue supports OpenIntro (US-based nonprofit) so we can provide free desk copies to teachers interested in using OpenIntro Statistics in the classroom and expand the project to support free textbooks in other subjects.
More resources: openintro.org.
Tech Giants in Healthcare
Dr. Bertalan MeskoThis comprehensive guide, Tech Giants in Healthcare, clarifies how and why big tech companies step into healthcare, and breaks it down from one market player to the other in what direction they are going, what tools they are using and what horizons they have in front of them.
Functional event-driven architecture: Powered by Scala 3
Gabriel VolpeExplore the event-driven architecture (EDA) in a purely functional way, mainly powered by Fs2 streams in Scala 3!
Leverage your functional programming skills by designing and writing stateless microservices that scale, powered by stateful message brokers.
CCIE Service Provider Version 4 Written and Lab Exam Comprehensive Guide
Nicholas RussoThe service provider landscape has changed rapidly over the past several years. Networking vendors are continuing to propose new standards, techniques, and procedures for overcoming new challenges while concurrently reducing costs and delivering new services. Cisco has recently updated the CCIE Service Provider track to reflect these changes; this book represents the author's personal journey in achieving that certification.
Ansible for DevOps
Jeff GeerlingAnsible is a simple, but powerful, server and configuration management tool. Learn to use Ansible effectively, whether you manage one server—or thousands.
Top Bundles
- #1
All the Books of The Medical Futurist
6 Books
We put together the most popular books from The Medical Futurist to provide a clear picture about the major trends shaping the future of medicine and healthcare. Digital health technologies, artificial intelligence, the future of 20 medical specialties, big pharma, data privacy, digital health investments and how technology giants such as Amazon... - #2
Practical FP in Scala + Functional event-driven architecture
2 Books
Practical FP in Scala (A hands-on approach) & Functional event-driven architecture, aka FEDA, (Powered by Scala 3), together as a bundle! The content of PFP in Scala is a requirement to understand FEDA so why not take advantage of this bundle!? - #3
Software Architecture for Developers: Volumes 1 & 2 - Technical leadership and communication
2 Books
"Software Architecture for Developers" is a practical and pragmatic guide to modern, lightweight software architecture, specifically aimed at developers. You'll learn:The essence of software architecture.Why the software architecture role should include coding, coaching and collaboration.The things that you really need to think about before... - #4
CCIE Service Provider Ultimate Study Bundle
2 Books
Piotr Jablonski, Lukasz Bromirski, and Nick Russo have joined forces to deliver the only CCIE Service Provider training resource you'll ever need. This bundle contains a detailed and challenging collection of workbook labs, plus an extensively detailed technical reference guide. All of us have earned the CCIE Service Provider certification... - #6
Pattern-Oriented Memory Forensics and Malware Detection
2 Books
This training bundle for security engineers and researchers, malware and memory forensics analysts includes two accelerated training courses for Windows memory dump analysis using WinDbg. It is also useful for technical support and escalation engineers who analyze memory dumps from complex software environments and need to check for possible... - #8
Modern C++ Collection
3 Books
Get All about Modern C++C++ Standard Library, including C++20Concurrency with Modern C++, including C++20C++20Each book has about 200 complete code examples. Updates are included. When I update one of the books, you immediately get the updated bundle. You can expect significant updates to each new C++ standard (C++23, C++26, .. ) and also...