-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #495 from realpython/pep8-update
Add supplementary materials for the pep8 resurfacing
- Loading branch information
Showing
7 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"""This is a docstring.""" | ||
|
||
# ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 )) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |