python_one-liners_-_write_concise_eloquent_python_like_a_professional

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

Return to Python Bibliography

Fair Use Commentary Text of Python One-Liners - Write Concise, Eloquent Python Like a Professional

PYTHON ONE-LINERS

Write Concise, Eloquent Python Like a Professional

by Christian Mayer

San Francisco

PYTHON ONE-LINERS. Copyright © 2020 by Christian Mayer.

All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.

ISBN-10: 1-7185-0050-5

ISBN-13: 978-1-7185-0050-1

Publisher: William Pollock

Production Editors: Janelle Ludowise and Kassie Andreadis

Cover Illustration: Rob Gale

Interior Design: Octopod Studios

Developmental Editors: Liz Chadwick and Alex Freed

Technical Reviewer: Daniel Zingaro

Copyeditor: Sharon Wilkey

Compositor: Danielle Foster

Proofreader: James Fraleigh

Indexer: JoAnne Burek

For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly:

No Starch Press, Inc.

245 8th Street, San Francisco, CA 94103

phone: 1.415.863.9900; [email protected]

www.nostarch.com

The Library of Congress issued the following Cataloging-in-Publication Data for the first edition:

Names: Mayer, Christian (Computer Scientist), author.

Title: Python one-liners: write concise, eloquent Python like a professional / Christian Mayer.

Description: San Francisco : No Starch Press, Inc., 2020. | Includes index.

Identifiers: LCCN 2020001449 (print) | LCCN 2020001450 (ebook) | ISBN

9781718500501 | ISBN 9781718500518 (ebook)

Subjects: LCSH: Python (Computer program language)

Classification: LCC QA76.73.P98 M39 2020 (print) | LCC QA76.73.P98

(ebook) | DDC 005.13/3–dc23

LC record available at https://lccn.loc.gov/2020001449

LC ebook record available at https://lccn.loc.gov/2020001450

No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.

The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.

To my wife Anna

About the Author

Christian Mayer is a doctor of computer science and the founder and maintainer of the popular Python site https://blog.finxter.com/ and its associated newsletter, which has 20,000 active subscribers and is still growing. His rapidly growing websites help tens of thousands of students improve their coding skills and online businesses. Christian is also the author of the Coffee Break Python series of self-published books.

About the Technical Reviewer

Dr. Daniel Zingaro is an assistant teaching professor of computer science and award-winning teacher at the University of Toronto. His main area of research is computer science education, where he studies how students learn (and sometimes don’t learn) computer science material. He is the author of Algorithmic Thinking (forthcoming from No Starch Press).

BRIEF CONTENTS

Acknowledgments

Introduction

Chapter 1: Python Refresher

Chapter 2: Python Tricks

Chapter 3: Data Science

Chapter 4: Machine Learning

Chapter 5: Regular Expressions

Chapter 6: Algorithms

Afterword

Index

CONTENTS IN DETAIL

ACKNOWLEDGMENTS

INTRODUCTION

Python One-Liner Example

A Note on Readability

Who Is This Book For?

What Will You Learn?

Online Resources

1

PYTHON REFRESHER

Basic Data Structures

Numerical Data Types and Structures

Booleans

Strings

The Keyword None

Container Data Structures

Lists

Stacks

Sets

Dictionaries

Membership

List and Set Comprehension

Control Flow

if, else, and elif

Loops

Functions

Lambdas

Summary

2

PYTHON TRICKS

Using List Comprehension to Find Top Earners

The Basics

The Code

How It Works

Using List Comprehension to Find Words with High Information Value

The Basics

The Code

How It Works

Reading a File

The Basics

The Code

How It Works

Using Lambda and Map Functions

The Basics

The Code

How It Works

Using Slicing to Extract Matching Substring Environments

The Basics

The Code

How It Works

Combining List Comprehension and Slicing

The Basics

The Code

How It Works

Using Slice Assignment to Correct Corrupted Lists

The Basics

The Code

How It Works

Analyzing Cardiac Health Data with List Concatenation

The Basics

The Code

How It Works

Using Generator Expressions to Find Companies That Pay Below Minimum Wage

The Basics

The Code

How It Works

Formatting Databases with the zip() Function

The Basics

The Code

How It Works

Summary

3

DATA SCIENCE

Basic Two-Dimensional Array Arithmetic

The Basics

The Code

How It Works

Working with NumPy Arrays: Slicing, Broadcasting, and Array Types

The Basics

The Code

How It Works

Conditional Array Search, Filtering, and Broadcasting to Detect Outliers

The Basics

The Code

How It Works

Boolean Indexing to Filter Two-Dimensional Arrays

The Basics

The Code

How It Works

Broadcasting, Slice Assignment, and Reshaping to Clean Every i-th Array Element

The Basics

The Code

How It Works

When to Use the sort() Function and When to Use the argsort() Function in NumPy

The Basics

The Code

How It Works

How to Use Lambda Functions and Boolean Indexing to Filter Arrays

The Basics

The Code

How It Works

How to Create Advanced Array Filters with Statistics, Math, and Logic

The Basics

The Code

How It Works

Simple Association Analysis: People Who Bought X Also Bought Y

The Basics

The Code

How It Works

Intermediate Association Analysis to Find Bestseller Bundles

The Basics

The Code

How It Works

Summary

4

MACHINE LEARNING

The Basics of Supervised Machine Learning

Training Phase

Inference Phase

Linear Regression

The Basics

The Code

How It Works

Logistic Regression in One Line

The Basics

The Code

How It Works

K-Means Clustering in One Line

The Basics

The Code

How It Works

K-Nearest Neighbors in One Line

The Basics

The Code

How It Works

Neural Network Analysis in One Line

The Basics

The Code

How It Works

Decision-Tree Learning in One Line

The Basics

The Code

How It Works

Get Row with Minimal Variance in One Line

The Basics

The Code

How It Works

Basic Statistics in One Line

The Basics

The Code

How It Works

Classification with Support-Vector Machines in One Line

The Basics

The Code

How It Works

Classification with Random Forests in One Line

The Basics

The Code

How It Works

Summary

5

REGULAR EXPRESSIONS

Finding Basic Textual Patterns in Strings

The Basics

The Code

How It Works

Writing Your First Web Scraper with Regular Expressions

The Basics

The Code

How It Works

Analyzing Hyperlinks of HTML Documents

The Basics

The Code

How It Works

Extracting Dollars from a String

The Basics

The Code

How It Works

Finding Nonsecure HTTP URLs

The Basics

The Code

How It Works

Validating the Time Format of User Input, Part 1

The Basics

The Code

How It Works

Validating Time Format of User Input, Part 2

The Basics

The Code

How It Works

Duplicate Detection in Strings

The Basics

The Code

How It Works

Detecting Word Repetitions

The Basics

The Code

How It Works

Modifying Regex Patterns in a Multiline String

The Basics

The Code

How It Works

Summary

6

ALGORITHMS

Finding Anagrams with Lambda Functions and Sorting

The Basics

The Code

How It Works

Finding Palindromes with Lambda Functions and Negative Slicing

The Basics

The Code

How It Works

Counting Permutations with Recursive Factorial Functions

The Basics

The Code

How It Works

Finding the Levenshtein Distance

The Basics

The Code

How It Works

Calculating the Powerset by Using Functional Programming

The Basics

The Code

How It Works

Caesar’s Cipher Encryption Using Advanced Indexing and List Comprehension

The Basics

The Code

How It Works

Finding Prime Numbers with the Sieve of Eratosthenes

The Basics

The Code

How It Works

Calculating the Fibonacci Series with the reduce() Function

The Basics

The Code

How It Works

A Recursive Binary Search Algorithm

The Basics

The Code

How It Works

A Recursive Quicksort Algorithm

The Basics

The Code

How It Works

Summary

AFTERWORD

INDEX

ACKNOWLEDGMENTS

The world doesn’t need more books; it needs better books. I’m incredibly grateful to the people at No Starch Press for putting everything at work toward this philosophy. This book is the result of their invaluable advice, constructive feedback, and hundreds of hours of diligent work. My deep gratitude goes to the No Starch team for making the book-writing process such a fun experience.

In particular, I’d like to thank Bill Pollock for inviting me to write this book and for providing me inspiration and deep insights into the publishing world.

I’m very grateful for my brilliant content editor, Liz Chadwick, who skillfully, patiently, and eloquently transformed my rough drafts into a much more human-readable form. It’s because of her excellent support that the book reached a level of clarity I would have never imagined when starting this project.

I want to express my appreciation to Alex Freed for her relentless focus on improving the text quality. It has been an honor to work together with such a talented editor.

I’d like to thank my production editor, Janelle Ludowise, for polishing the book with a great love for every detail. Janelle put her skills to work—in a positive and enthusiastic manner—to craft the final version of the book. Thanks, Janelle. Many thanks as well to Kassie Andreadis, who energetically pushed the book through to completion.

My distinctive appreciation goes to Professor Daniel Zingaro. He didn’t shy away from investing much of his time, effort, and excellent computer science skills into eradicating inaccuracies from the book. He also contributed many wonderful suggestions that brought clarity to the book. Without his effort, the book would not only contain more bugs but also be harder to read. That said, any inaccuracies that remain are my own.

My doctorate supervisor, Professor Rothermel, contributed indirectly to this book by investing considerable time, skill, and effort into my computer science education. I owe him my deepest gratitude and appreciation.

I’m forever grateful to my beautiful wife, Anna Altimira, who keeps listening to, encouraging, and supporting even my wildest ideas. I’m also thankful to my kids, Amalie and Gabriel, for their inspiring curiosity and the happiness they bring to my life through thousands of smiles.

Lastly, the greatest source of motivation came from the active members of the Finxter community. First and foremost, I’ve written this book for ambitious coders—like you—who want to advance their coding skills and solve practical problems in the real world. After long working days, it was grateful emails from Finxter members that encouraged me to write more sections of the book.

INTRODUCTION

With this book, I want to help you become a Python expert. To do this, we’re going to focus on Python one-liners: concise, useful programs packed into a single line of Python. Focusing on one-liners will help you read and write code faster and more concisely, and will improve your understanding of the language.

There are five more reasons I think learning Python one-liners will help you improve and are worth studying.

First, by improving your core Python skills, you’ll be able to overcome many of the small programming weaknesses that hold you back. It’s hard to make progress without a profound understanding of the basics. Single lines of code are the basic building block of any program. Understanding these basic building blocks will help you master high-level complexity without feeling overwhelmed.

Second, you’ll learn how to leverage wildly popular Python libraries, such as those for data science and machine learning. The book consists of five one-liner chapters, each addressing a different area of Python, from regular expressions to machine learning. This approach will give you an overview of possible Python applications you can build, as well as teach you how to use these powerful libraries.

Third, you’ll learn to write more Pythonic code. Python beginners, especially those coming from other programming languages, often write code in un-Pythonic ways. We’ll cover Python-specific concepts like list comprehension, multiple assignment, and slicing, all of which will help you write code that’s easily readable and sharable with other programmers in the field.

Fourth, studying Python one-liners forces you to think clearly and concisely. When you’re making every single code symbol count, there’s no room for sparse and unfocused coding.

Fifth, your new one-liner skill set will allow you to see through overly complicated Python codebases, and impress friends and interviewers alike. You may also find it fun and satisfying to solve challenging programming problems with a single line of code. And you wouldn’t be alone: a vibrant online community of Python geeks compete for the most compressed, most Pythonic solutions to various practical (and not-so-practical) problems.

Python One-Liner Example

The central thesis of this book is that learning Python one-liners is both fundamental to understanding more-advanced codebases and an excellent tool for improving your skills. Before understanding what’s going on in a codebase with thousands of lines, you must understand the meaning of a single line of code.

Let’s have a quick look at a Python one-liner. Don’t worry if you don’t fully understand it. You will master this one-liner in Chapter 6.

q = lambda l: q( ➊[x for x in l[1:] if x ⇐ l[0]]) + [l[0]] + q([x for x in l if x > l[0]]) if l else []

This one-liner is a beautiful and concise way of compressing the famous Quicksort algorithm, though the meaning may be difficult to grasp for many Python beginners and intermediates.

Python one-liners often build on each other, so one-liners will increase in complexity throughout the book. In this book, we’ll start with simple one-liners that will become the basis for more-complex one-liners later. For example, the preceding Quicksort one-liner is difficult and long, based on the easier concept of list comprehension ➊. Here’s a simpler list comprehension that creates a list of squared numbers:

lst = [x**2 for x in range(10)]

We can break this one-liner into even simpler one-liners that teach important Python basics, such as variable assignments, math operators, data structures, for loops, membership operators, and the range() function—all of which happens in a single line of Python!

Know that basic doesn’t mean trivial. All the one-liners we’ll look at are useful, and each chapter addresses a separate area or discipline in computer science, giving you a broad perspective on the power of Python.

A Note on Readability

The Zen of Python comprises 19 guiding principles for the Python programming languages. You can read it in your Python shell by entering import this:

import this

The Zen of Python, by Tim Peters

Beautiful is better than ugly.

Explicit is better than implicit.

Simple is better than complex.

Complex is better than complicated.

Flat is better than nested.

Sparse is better than dense.

Readability counts.

–snip–

According to The Zen of Python, “Readability counts.” One-liners are minimalistic programs to solve problems. In many cases, rewriting a piece of code as a Python one-liner will improve readability and make the code more Pythonic. An example is using list comprehension to reduce the creation of lists into a single line of code. Have a look at the following example:

  1. BEFORE

squares = []

for i in range(10):

squares.append(i**2)

print(squares)

  1. [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

In this code snippet, we need five lines of code to create a list of the first 10 square numbers and print it to the shell. However, it’s much better to use a one-liner solution that accomplishes the same thing in a more readable and concise way:

  1. AFTER

print([i**2 for i in range(10)])

  1. [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

The output is the same, but the one-liner builds on the more Pythonic concept of list comprehension. It’s easier to read and more concise.

However, Python one-liners can also be hard to understand. In some cases, writing a solution as a Python one-liner isn’t more readable. But just as the chess master must know all possible moves before deciding which one is best, you must know all ways of expressing your thoughts in code so that you can decide on the best one. Going for the most beautiful solution is not a low-priority matter; it’s at the core of the Python ecosystem. As The Zen of Python teaches, “Beautiful is better than ugly.”

Who Is This Book For?

Are you a beginner- to intermediate-level Python coder? Like many of your peers, you may be stuck in your coding progress. This book can help you out. You’ve read a lot of programming tutorials online. You’ve written your own source code and successfully shipped small projects. You’ve finished a basic programming course and read a programming textbook or two. Maybe you’ve even finished a technical program in college, where you’ve learned about the basics of computer science and programming.

Perhaps you’re limited by certain beliefs, like that most coders understand source code much faster than you, or that you’re nowhere near the top 10 percent of programmers. If you want to reach an advanced coding level and join the top coding experts, you need to learn new applicable skills.

I can relate because when I started out studying computer science 10 years ago, I struggled with the belief that I knew nothing about coding. At the same time, it seemed that all my peers were already very experienced and proficient.

In this book, I want to help you overcome these limiting beliefs and push you one step further toward Python mastery.

What Will You Learn?

Here is an overview of what you will learn.

Chapter 1: Python Refresher Introduces the very basics of Python to refresh your knowledge.

Chapter 2: Python Tricks Contains 10 one-liner tricks to help you master the basics, such as list comprehension, file input, the functions lambda, map(), and zip(), the all() quantifier, slicing, and basic list arithmetic. You’ll also learn how to use, manipulate, and leverage data structures to solve various day-to-day problems.

Chapter 3: Data Science Contains 10 one-liners for data science, building on the NumPy library. NumPy is at the heart of Python’s powerful machine learning and data science capabilities. You’ll learn elementary NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics.

Chapter 4: Machine Learning Covers 10 one-liners for machine learning with Python’s scikit-learn library. You’ll learn about regression algorithms that predict values. Examples of these include linear regression, K-Nearest Neighbors, and neural networks. You’ll also learn classification algorithms such as logistic regression, decision-tree learning, support-vector machines, and random forests. Furthermore, you’ll learn about how to calculate basic statistics of multidimensional data arrays, and the K-Means algorithm for unsupervised learning. These algorithms and methods are among the most important algorithms in the field of machine learning.

Chapter 5: Regular Expressions Contains 10 one-liners to help you achieve more with regular expressions. You’ll learn about various basic regular expressions that you can combine (and recombine) in order to create more-advanced regular expressions, using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators.

Chapter 6: Algorithms Contains 10 one-liner algorithms addressing a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting. Many of these form the basis of more-advanced algorithms and contain the seeds of a thorough algorithmic education.

Afterword Concludes this book and releases you into the real world, packed with your new and improved Python coding skills.

Online Resources

To enhance the training material in this book, I’ve added supplementary resources that you can find online at https://pythononeliners.com/ or http://www.nostarch.com/pythononeliners/. The interactive resources include the following:

Python cheat sheets You can download those Python cheat sheets as printable PDFs and pin them to your wall. The cheat sheets contain essential Python language features, and if you study them thoroughly, you can refresh your Python skills and ensure that you’ve closed any knowledge gap you may have.

One-liner video lessons As part of my Python email course, I’ve recorded many Python one-liner lessons from this book, which you can access for free. Those lessons can assist you in your learning and provide a multimedia learning experience.

Python puzzles You can visit the online resources to solve Python puzzles and use the Finxter.com app for free to test and train your Python skills and measure your learning progress as you go through the book.

Code files and Jupyter notebooks You must roll up your sleeves and start working with code to make progress toward Python mastery. Take your time to play around with various parameter values and input data. For your convenience, I’ve added all Python one-liners as executable code files.

python_one-liners_-_write_concise_eloquent_python_like_a_professional.txt · Last modified: 2024/04/28 03:23 by 127.0.0.1