Python Dictionary Comprehension: A Powerful One-Liner Tutorial

Dictionary Comprehension is a concise and memory-efficient way to create and initialize dictionaries in one line of Python code. It consists of two parts: expression and context. The expression defines how to map keys to values. The context loops over an iterable using a single-line for loop and defines which (key,value) pairs to include in … Read more

[Dash + Flask] How to Deploy a Python Dash App on

Here’s the step-by-step approach of how to deploy your Dash app on using Flask and pip: Create an account on Create a Flask application. Create a Dash application. Copy the Dash app into the Flask app. Connect the Flask server with the Dash app. Modify the WSGI configuration file. Install Dash with pip … Read more

Python One-Line Password Generator

Can you believe it? People use unknown and potentially insecure websites to generate their random passwords! This works as follows: A website generates a “random” password for them and they copy&paste it and assume this is a safe password because of the randomness of the characters. What a security flaw! Why? Because the website could … Read more

Python Webscraper Regex [Free Book Chapter Tutorial]

This tutorial is a chapter excerpt drafted for my new book “Python One-Liners” (to appear in 2020, No Starch Press, San Francisco). Are you an office worker, student, software developer, manager, blogger, researcher, author, copywriter, teacher, or self-employed freelancer? Most likely, you are spending many hours in front of your computer, day after day. In … Read more

Python Comments — 2-Minute Guide with Exercise

Wouldn’t reading code be much easier if the author constantly shared their thoughts with you? Commenting is good practice in Python because it helps others (and your future self) understanding your code much better. Writing commented code makes you more productive in the long term! There are two types of comments: one-line comments and multi-line … Read more

Python One Line Class

Do you hate those lengthy class definitions with __init__ and too many whitespaces and newlines? Python One-Liners to the rescue! Luckily, you can create classes in a single line—and it can even be Pythonic to do so! Sounds too good to be true? Let’s dive right into it! Problem: How to create a Python class … Read more

Python Define Multiple Variables in One Line

In this article, you’ll learn about two variants of this problem. Assign multiple values to multiple variables Assign the same value to multiple variables Let’s have a quick overview of both in our interactive code shell: Exercise: Increase the number of variables to 3 and create a new one-liner! Let’s dive into the two subtopics … Read more

Python Slice Assignment

Slice Assignment

Slice assignment is a little-used, beautiful Python feature to replace a slice with another sequence. Simply select the slice you want to replace on the left and the values to replace it on the right side of the equation. For example, the slice assignment list[2:4] = [42, 42] replaces the list elements with index 2 … Read more