Python One-Liners

Book description

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 learnhow 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 to:

•Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution
•Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics
•Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning
•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
•Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting

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.

Table of contents

  1. Cover Page
  2. Title Page
  3. Copyright Page
  4. Dedication
  5. About the Author
  6. About the Technical Reviewer
  7. Brief Contents
  8. Contents in Detail
  9. Acknowledgments
  10. Introduction
    1. Python One-Liner Example
    2. A Note on Readability
    3. Who Is This Book For?
    4. What Will You Learn?
    5. Online Resources
  11. 1 Python Refresher
    1. Basic Data Structures
    2. Container Data Structures
    3. Control Flow
    4. Functions
    5. Lambdas
    6. Summary
  12. 2 Python Tricks
    1. Using List Comprehension to Find Top Earners
    2. Using List Comprehension to Find Words with High Information Value
    3. Reading a File
    4. Using Lambda and Map Functions
    5. Using Slicing to Extract Matching Substring Environments
    6. Combining List Comprehension and Slicing
    7. Using Slice Assignment to Correct Corrupted Lists
    8. Analyzing Cardiac Health Data with List Concatenation
    9. Using Generator Expressions to Find Companies That Pay Below Minimum Wage
    10. Formatting Databases with the zip() Function
    11. Summary
  13. 3 Data Science
    1. Basic Two-Dimensional Array Arithmetic
    2. Working with NumPy Arrays: Slicing, Broadcasting, and Array Types
    3. Conditional Array Search, Filtering, and Broadcasting to Detect Outliers
    4. Boolean Indexing to Filter Two-Dimensional Arrays
    5. Broadcasting, Slice Assignment, and Reshaping to Clean Every i-th Array Element
    6. When to Use the sort() Function and When to Use the argsort() Function in NumPy
    7. How to Use Lambda Functions and Boolean Indexing to Filter Arrays
    8. How to Create Advanced Array Filters with Statistics, Math, and Logic
    9. Simple Association Analysis: People Who Bought X Also Bought Y
    10. Intermediate Association Analysis to Find Bestseller Bundles
    11. Summary
  14. 4 Machine Learning
    1. The Basics of Supervised Machine Learning
    2. Linear Regression
    3. Logistic Regression in One Line
    4. K-Means Clustering in One Line
    5. K-Nearest Neighbors in One Line
    6. Neural Network Analysis in One Line
    7. Decision-Tree Learning in One Line
    8. Get Row with Minimal Variance in One Line
    9. Basic Statistics in One Line
    10. Classification with Support-Vector Machines in One Line
    11. Classification with Random Forests in One Line
    12. Summary
  15. 5 Regular Expressions
    1. Finding Basic Textual Patterns in Strings
    2. Writing Your First Web Scraper with Regular Expressions
    3. Analyzing Hyperlinks of HTML Documents
    4. Extracting Dollars from a String
    5. Finding Nonsecure HTTP URLs
    6. Validating the Time Format of User Input, Part 1
    7. Validating Time Format of User Input, Part 2
    8. Duplicate Detection in Strings
    9. Detecting Word Repetitions
    10. Modifying Regex Patterns in a Multiline String
    11. Summary
  16. 6 Algorithms
    1. Finding Anagrams with Lambda Functions and Sorting
    2. Finding Palindromes with Lambda Functions and Negative Slicing
    3. Counting Permutations with Recursive Factorial Functions
    4. Finding the Levenshtein Distance
    5. Calculating the Powerset by Using Functional Programming
    6. Caesar’s Cipher Encryption Using Advanced Indexing and List Comprehension
    7. Finding Prime Numbers with the Sieve of Eratosthenes
    8. Calculating the Fibonacci Series with the reduce() Function
    9. A Recursive Binary Search Algorithm
    10. A Recursive Quicksort Algorithm
    11. Summary
  17. Afterword
  18. Index

Product information

  • Title: Python One-Liners
  • Author(s): Christian Mayer
  • Release date: April 2020
  • Publisher(s): No Starch Press
  • ISBN: 9781718500501