PEP 8: Hanging Indentation and Closing Brackets in Python

PEP 8 purists are ready to attack you and your code if they catch you not complying with the PEP 8 standard. For instance, Python coders put their braces, brackets, or parentheses into a separate line to make it easier to grasp nested lists or dictionaries.

This article shows how to line up the closing braces, brackets, and parentheses correctly in Python. This is called “hanging indentation” and it’s at the heart of PEP 8 standardized, clean code that’s easy to read and understand!

A quick example shows how you can create a multi-line construct that complies with the PEP 8

Continue reading the original article

Python One Line Return if

Problem: How to return from a Python function or method in single line?

Example: Consider the following “goal” statement:

def f(x):
return None if x == 0

However, this leads to a Syntax error:

In this tutorial, you’ll learn how to write the return statement with an if expression in a single line of Python code. You can get an overview of the three methods in the interactive code shell:

Exercise: The code has no output. Print the results of all three function executions for a given x. Is it always the same?

Let’s dive into the three methods.

Method 1: As a Multi-Liner

The following

Continue reading the original article

The Real Python Podcast – Episode #20: Building PDFs in Python with ReportLab

Have you wanted to generate advanced reports as PDFs using Python? Maybe you want to build documents with tables, images, or fillable forms. This week on the show we have Mike Driscoll to talk about his book “ReportLab – PDF Processing with Python.”

[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]

Continue reading the original article

Fixing ALTER TABLE errors with Flask-Migrate and SQLite

If you’ve done any work with SQLite databases you surely know that this database is very limited in terms of making changes to the database schema. When working with a migration framework such as Flask-Migrate, it is common to end up with migration scripts that fail to upgrade or downgrade just because they need to remove or modify a column in a table, something that SQLite does not support.
In this article I’m going to discuss this limitation of the SQLite database, and show you a workaround that is specific to Flask-Migrate and Alembic.

Continue reading the original article

Python One Line Reverse Shell

This article will be fun! You’ll learn about an important concept in security: reverse shells. You’ll also learn how to create reverse shells in Python in a single line of code. So, let’s start with the big question:

What is a Reverse Shell?

Here’s the definition of a Reverse Shell:

A reverse shell is used by hackers to gain access to a target machine. The target machine opens a shell to communicate to the attacking machine. The attacking machine receives the connection (listening on a given port) and is now able to access the target computer. To accomplish a reverse shell, a hacker

Continue reading the original article

Top 5 Python Freelancer Jobs to Earn $51 per Hour on Upwork or Fiverr

Python freelancers earn $51 per hour on average. But how do they do it? In the following video I show you the top five trending gigs for Python freelancers:

In summary, these are the most trending jobs how Python freelancers earn money in 2020:

Create educational content:. You can write blog articles for blog owners, write ebooks for publishers, or create full video courses for education companies. These gigs usually require that you’re dedicated to teaching and you’re willing to learn. But the payoff can be huge—because you learn new things, increase your value to the marketplace and get paid in the

Continue reading the original article

Namespaces and Scope in Python

This tutorial covers Python namespaces, the structures used to organize the symbolic names assigned to objects in a Python program.
The previous tutorials in this series have emphasized the importance of objects in Python. Objects are everywhere! Virtually everything that your Python program creates or acts on is an object.
An assignment statement creates a symbolic name that you can use to reference an object. The statement x = ‘foo’ creates a symbolic name x that refers to the string object ‘foo’.
In a program of any complexity, you’ll create hundreds or thousands of such names, each pointing to a specific object. How

Continue reading the original article

How to Execute Multiple Lines in a Single Line Python From Command-Line?

Summary: To make a Python one-liner out of any multi-line Python script, replace the new lines with a new line character ‘n’ and pass the result into the exec(…) function. You can run this script from the outside (command line, shell, terminal) by using the command python -c “exec(…)”.

Problem: Given a multi-line code script in Python. How to execute this multi-line script in a single line of Python code? How to do it from the command line?

Example: Say, you have the following for loop with a nested if statement in the for loop body. You want to run this

Continue reading the original article

Python’s None: Null in Python

If you have experience with other programming languages, like C or Java, then you’ve probably heard of the concept of null. Many languages use this to represent a pointer that doesn’t point to anything, to denote when a variable is empty, or to mark default parameters that you haven’t yet supplied. null is often defined to be in those languages, but null in Python is different.
Python uses the keyword None to define null objects and variables. While None does serve some of the same purposes as null in other languages, it’s another beast entirely. As the null in Python, None

Continue reading the original article