Table Of ContentPYTHON 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; info@nostarch.com
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