Skip to content

Commit

Permalink
Merge pull request #495 from realpython/pep8-update
Browse files Browse the repository at this point in the history
Add supplementary materials for the pep8 resurfacing
  • Loading branch information
KateFinegan authored Feb 5, 2024
2 parents d7659cb + ef995e9 commit b566d79
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pep8-beautiful-code/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# How to Write Beautiful Python Code With PEP 8

This repository contains code snippets shown in the tutorial on [How to Write Beautiful Python Code With PEP 8](https://realpython.com/python-pep8/).

You can uncomment the code that isn't PEP 8 compliant in the following files if you want to test the errors, linters, and autoformatters described in the tutorial:

- [`mixed_indentation.py`](./mixed_indentation.py)
- [`trailing_whitespace.py`](./trailing_whitespace.py)
- [`unfashionable.py`](./unfashionable.py)

After uncommenting the code, you can then run, lint, and format the files locally.
3 changes: 3 additions & 0 deletions pep8-beautiful-code/documented_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""This is a docstring."""

# ...
9 changes: 9 additions & 0 deletions pep8-beautiful-code/mixed_indentation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Uncomment the code below
# for an example of code with mixed indentation:

# def mixed_indentation(greet=True):
# if greet:
# print("Hello") # Indented with 4 spaces.
# print("World") # Indented with a tab.

# mixed_indentation()
12 changes: 12 additions & 0 deletions pep8-beautiful-code/quadratic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def quadratic(a, b, c):
"""Solve quadratic equation via the quadratic formula.
A quadratic equation has the following form:
ax**2 + bx + c = 0
There always two solutions to a quadratic equation: x_1 & x_2.
"""
x_1 = (-b + (b**2 - 4 * a * c) ** (1 / 2)) / (2 * a)
x_2 = (-b - (b**2 - 4 * a * c) ** (1 / 2)) / (2 * a)

return x_1, x_2
7 changes: 7 additions & 0 deletions pep8-beautiful-code/trailing_whitespace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Uncomment the code below
# for an example of code with a trailing whitespace:

# x = 1 + 2 + \ # noqa w291
# 3 + 4

# print(x)
24 changes: 24 additions & 0 deletions pep8-beautiful-code/unfashionable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
numbers = [1, 2, 3, 4]


def add_all_numbers_from_collection(
number_one, number_two, number_three, number_four
):
return number_one + number_two + number_three + number_four


print(add_all_numbers_from_collection(*numbers))

# The commented-out code shows the code before linting and formatting:

# import math

# numbers = [1,2,\
# 3,4]

# def add_all_numbers_from_collection(
# number_one, number_two, number_three,
# number_four):
# return number_one+number_two + number_three +number_four

# print (add_all_numbers_from_collection( *numbers ))
12 changes: 12 additions & 0 deletions pep8-beautiful-code/variance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def calculate_variance(number_list):
sum_list = 0
for number in number_list:
sum_list = sum_list + number
mean = sum_list / len(number_list)

sum_squares = 0
for number in number_list:
sum_squares = sum_squares + number**2
mean_squares = sum_squares / len(number_list)

return mean_squares - mean**2

0 comments on commit b566d79

Please sign in to comment.