Skip to content

Python PEP8 Style Guide

PEP 8 is a style guide for writing Python code. It provides guidelines and recommendations on how to format code to improve readability and maintainability. Here are some key points from PEP 8:

However, there are some general syntax guidelines that align with PEP 8 principles.

1. Indentation: Use four spaces for each level of indentation. Avoid using tabs or mixing tabs and spaces.

2. Line length: Limit lines to a maximum of 79 characters. If a line needs to be longer, you can break it into multiple lines using parentheses or backslashes.

3. Parentheses and Brackets: Use parentheses and brackets to group expressions and clarify the order of operations. For example:

result = (a + b) * c
my_list = [1, 2, 3]

4. String quotes: Use single quotes or double quotes to define string literals. Choose one and stick with it consistently throughout your codebase.

name = 'John'
message = "Hello, world!"

5. Comments: Use the # symbol for inline comments. Place comments on a separate line above the code they refer to. Write comments in complete sentences and start them with a capital letter.

# Calculate the sum of two numbers
total = a + b

6. Function and method definitions: Use def keyword followed by a space and then the function or method name. Include a space after the function name, and use parentheses to enclose the parameters. Use a colon at the end of the line, and indent the function body with four spaces.

def calculate_sum(a, b):
    return a + b

7. Control flow statements: Place a space before and after binary operators, assignment operators, and comparison operators. Use a space after commas in lists, tuples, and function arguments.

These are some general syntax guidelines that align with the principles of PEP 8. It’s always a good practice to refer to the official Python documentation and PEP 8 style guide for more detailed information and examples.

Python PEP8 example

Here’s an example of Python code written in accordance with the PEP 8 style guide:

# This is a simple Python script
import os
import sys
import math

result = None

def calculate_sum(a, b):
    Calculate the sum of two numbers.

        a (int): The first number.
        b (int): The second number.

        int: The sum of the two numbers.
    return a + b

class Circle:
    """A class representing a circle."""

    def __init__(self, radius):
        self.radius = radius

    def area(self):
        Calculate the area of the circle.

            float: The area of the circle.
        return math.pi * self.radius ** 2

def main():
    The main function of the script.
    x = 5
    y = 3
    result = calculate_sum(x, y)
    print("The sum of", x, "and", y, "is:", result)

    circle = Circle(5)
    print("The area of the circle with radius", circle.radius, "is:", circle.area())

if __name__ == "__main__":


Python PEP8 Style Guide

Do comment if you have any doubts or suggestions on this Python basic topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *