Jump to ratings and reviews
Rate this book

Python One-Liners: Write Concise, Eloquent Python Like a Professional

Rate this book
Python programmers will improve their computer science skills with these useful one-liners.

Python One-Liners will teach you how to read and write "one-liners": concise statements of useful functionality packed into a single line of code. You'll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.

The book's five chapters cover tips and tricks, regular expressions, machine learning, core data science topics, and useful algorithms. Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You'll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments. You'll also learn how


By the end of the book, you'll know how to write Python at its most refined, and create concise, beautiful pieces of "Python art" in merely a single line.

216 pages, Paperback

Published June 2, 2020

78 people are currently reading
139 people want to read

About the author

Christian Mayer

73 books5 followers

Ratings & Reviews

What do you think?
Rate this book

Friends & Following

Create a free account to discover what your friends think of this book!

Community Reviews

5 stars
23 (29%)
4 stars
25 (32%)
3 stars
23 (29%)
2 stars
7 (8%)
1 star
0 (0%)
Displaying 1 - 16 of 16 reviews
Profile Image for Anna.
898 reviews22 followers
Read
February 27, 2024
A mixed bag. I liked the practice with list comprehensions a lot. Sadly, I was lost for the last ~quarter of the book. Part of the problem is that I never fully mastered lambda functions, but also I think one-liners suffer from poor readability in general. I’m of two minds about the chapter on machine learning. On the one hand, I needed to start learning the basics of ML/AI and was glad to at least make a start. On the other, one chapter isn’t enough to start learning the basics of ML!
Profile Image for Ian Mizer.
8 reviews1 follower
July 27, 2020
First off I did get this book from the publisher for free, but the thoughts in this review are my own.

This book is great for flexing in an interview or thinking of python in completely different ways. The gripes I have with it are minor and will mostly boil down to nitpicking and the books subject getting in the way of its readability. If you're ok with that then buy this book for every would be or current junior developer you know.

Ok, Are they gone? I think I got rid of all the people who will only look at the highlight sentences. So now I'm going to sit down and go through all the actual pieces of this book and explain my system. I rated this book on 3 main subjects.

Readability : How easy would it be for someone to sit down with this book and finish it quickly.

Accuracy :
How accurate is the information being given? Could you write every single line of code in this book and have the books answers match the code you wrote. Normally this is a -1 out of 10 per major mistake and per 4 minor mistakes the book makes

Subject : Does this book do what it wants to do and is what it wants to do good?

Bonus points: for books that provide outside resources. If the outside resources expand on the subject matter of the book then even more bonus points.

Readability : 8 out of 10

This book personally took me 4 days to read. The first 80 pages are a dream to get through. Everything is quick and easy to understand and if you have some understanding of everything already being talked about within the book then you'll find the first 3 chapters easy. In fact when I first sat down with the book I thought I would be able to get through this book in 3 days reading 60 pages a day. Then I got to chapter 4.
This is where the machine learning topics came in. Doing machine learning in a single line of code is a bit of a slog. It isn't so much that the material is poorly written as it is that the topic is difficult to make easily readable. The author does what he/she can, but I believe that only an incredibly experienced writer who focuses completely on the readability of each sentence could conquer this problem. Kudos for the attempt.

There are some pages of this book that refer to and use certain operators long after they've explained them. Like the star/asterisk, which the book explains on the first and second chapter and then brings up 40 pages later only to not send you back to the explanation. This usage will confuse some readers and cause them to stop and google what's going on in the code they're looking at. This will causes some slowdown in your reading and I have mentioned this to the publisher. So if it's fixed in the reprint then I guess you're welcome.

The author also falls into the most common pitfall of the entire Code writing industry : He/She uses variables instead of using words that describe those variables. This causes the obvious problems of making the material a little harder to read overall. It also causes a few problems where a variable name like “a” or “i” will be used and it will cause you to stop to check if you've lost your place on the page.
Example : “In plain English,
you create a new array containing only those elements of the data array a
for which indexing array indices contains True values at the respective array positions.“
Using variables this way for text can have a jarring feeling as though you lost your place on the page. The important thing is : “Using variables makes text less readable than if you just use a descriptor for the variable you're referring to.” Something like “ListObject1” or “Absolute1” instead of “a” or “i”

Accuracy : 9/10

There are 4 pages in the whole book that I was able to find that had errors and 2 of those errors are easy to correct in your head without thinking about it. All of these errors are able to be seen by the reader and quickly worked out once you read through them. I would say this is almost as accurate as a technical book can actually be.

Subject : 10/10

The subject of writing all of your code in a single line is exactly what this book gets done. One-Liners as a subject itself is contentious. If you're going into a machine learning interview then writing all of your answers out as a single line will impress interviewers. If you can show testing for that line and write a well commented version of it within the same interview then you will impress the interviewer even more.
If you're going into a company that doesn't do machine learning or has a large code base that will need to be managed by the applicant then this will do well, but not impress anyone. One-Liners can never really hurt you in a code interview they can only hurt you in large code bases. The worst case scenario I've gathered is that this just might not be what the person you're interviewing with is looking for. It does improve your understanding of code as a whole, but use it based on who you're looking to work for.



Bonus points:

+1 The website “pythononeliners.com” referenced by the author is great for expanding the book, improving on, and better explaining the pieces of the book you might not understand. The video series is currently up to 8 videos and the youtube channel itself has a wealth of python information. I would highly recommend checking it out yourself.

+1 The accompanying youtube channel is something that I will be adding to my regular watch list as it seems filled to the brim with an expansion on python knowledge.
There is a major expansion on python 1 liners on the Finxter website and youtube channel. The expansions are a must watch for anyone who enjoyed the book and it's concepts
Profile Image for Matt Chan.
159 reviews5 followers
June 20, 2020
So the problem with is book is that the premise seemed sound enough; there are a good amount of robust and READABLE Python one-liners that can be valuable to learn and remember, such as list/dictionary/generator comprehensions, one-line function definition and lambdas and so on. However, much of the examples and even whole chapters in this book really run against a lot of Python principles, such as readable codes, PEP8, etc. The author admitted as much that many of the one-liners aren't really that practical in the real world, but were used as pedagogical devices in the book. However, I was unsure what he was trying to teach. I guess the saving grace of the book is that it forced me to read up more (elsewhere) about list/dictionary/generator comprehension and lambdas, and now I do feel much more comfortable in using them.
224 reviews14 followers
December 3, 2020
I know this book receives some criticism, often by judging the book by it's title alone. The concept of a 'one-lines' has a very negative connotation in computer programming as oneliners are very often extremely unreadable (even by the original author as time passes).
However if you go beyond the cover of the book, there is actually some value in the approach brought by the author. By focusing on extremely small code snippets around one specific problem you are forced to focus and understanding of special constructs like slicing, list comprehension and lambda's really improve.
The bad is that one of the parts is about machine learning, and that subject is simply too large and too complex to be suitable for this matter (even though it is a trend). I appreciated some of the extra exercises but there could've been more of these.
Profile Image for Ghulam.
58 reviews1 follower
November 28, 2020
Must read for every python programmer to learn pythonic one-liners.
32 reviews
July 31, 2020
Decent book.
Useful when you're looking for a specific python trick.
Not going to improve your Python skill level substantially.
600 reviews11 followers
September 8, 2020
I liked the first chapters and learned many useful tricks. However, the deeper I got into the book the less interesting the topics where for me. I felt more and more that the book is not really helping me and that I should turn to other resources to learn more about those topics. YMMV, but I expected a lot more.
6 reviews1 follower
December 17, 2022
Read the book pretty fast. The book stopped at the regex chapter . Anything else was just forcing to cram code into one line. The lambda part should have been extended as a preparation for the rest of the chapters. It is mostly a mix of introduction to different topics and verbose decision to cram everything in one line . Some of the decisions make sense while others are clearly forced.
20 reviews1 follower
August 12, 2021
must read. マシンラーニングとアルゴリズムの章がシンプルで初心者に分かり良かった。
Profile Image for Eric Xia.
178 reviews4 followers
June 4, 2022
This book is so interesting and fun. Hopefully I can return to it soon
Profile Image for Mr. H.
41 reviews1 follower
June 24, 2023
Not bad, more like a cookbook for diverse subjects with no specific focus
Profile Image for Hoà.
50 reviews12 followers
August 14, 2022
Some tricks for brief Python codes. New things I learn: negative lookahead in regex, reduce function.
Displaying 1 - 16 of 16 reviews

Can't find what you're looking for?

Get help and learn more about the design.