diff --git a/pep8-beautiful-code/README.md b/pep8-beautiful-code/README.md new file mode 100644 index 0000000000..59a1324a69 --- /dev/null +++ b/pep8-beautiful-code/README.md @@ -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. diff --git a/pep8-beautiful-code/documented_module.py b/pep8-beautiful-code/documented_module.py new file mode 100644 index 0000000000..993bde9210 --- /dev/null +++ b/pep8-beautiful-code/documented_module.py @@ -0,0 +1,3 @@ +"""This is a docstring.""" + +# ... diff --git a/pep8-beautiful-code/mixed_indentation.py b/pep8-beautiful-code/mixed_indentation.py new file mode 100644 index 0000000000..6c12284f59 --- /dev/null +++ b/pep8-beautiful-code/mixed_indentation.py @@ -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() diff --git a/pep8-beautiful-code/quadratic.py b/pep8-beautiful-code/quadratic.py new file mode 100644 index 0000000000..ef357c2b69 --- /dev/null +++ b/pep8-beautiful-code/quadratic.py @@ -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 diff --git a/pep8-beautiful-code/trailing_whitespace.py b/pep8-beautiful-code/trailing_whitespace.py new file mode 100644 index 0000000000..23a3bbfef2 --- /dev/null +++ b/pep8-beautiful-code/trailing_whitespace.py @@ -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) diff --git a/pep8-beautiful-code/unfashionable.py b/pep8-beautiful-code/unfashionable.py new file mode 100644 index 0000000000..62fd2ebc52 --- /dev/null +++ b/pep8-beautiful-code/unfashionable.py @@ -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 )) diff --git a/pep8-beautiful-code/variance.py b/pep8-beautiful-code/variance.py new file mode 100644 index 0000000000..97e3bbdb90 --- /dev/null +++ b/pep8-beautiful-code/variance.py @@ -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