Friday 19 July 2024

Why you should use PEP 8 guidelines ?

 

7. Error Prevention

Reason: PEP 8 includes guidelines that help prevent common errors, such as mixing tabs and spaces for indentation.


Without PEP 8:


def calculate_sum(a, b):

  return a + b

    print("Sum calculated")

  Cell In[16], line 3

    print("Sum calculated")

    ^

IndentationError: unexpected indent

With PEP 8:


def calculate_sum(a, b):

    return a + b


print("Sum calculated")

Sum calculated

6. Community Standard

Reason: PEP 8 is the de facto standard for Python code. Following it ensures your code aligns with what other Python developers expect, making it easier for others to read and contribute to your projects.


Without PEP 8:


class Person: 

  def __init__(self,name,age):

    self.name=name

    self.age=age

  def getDetails(self):

    return self.name + " is " + str(self.age)

With PEP 8:


class Person:

    def __init__(self, name, age):

        self.name = name

        self.age = age


    def get_details(self):

        return f"{self.name} is {self.age}"

5. Professionalism

Reason: Following PEP 8 shows that you care about writing high-quality code. It demonstrates professionalism and attention to detail, which are valuable traits in any developer.


Without PEP 8:


def square(x):return x*x

With PEP 8:


def square(x):

    return x * x

4. Maintainability

Reason: Code that adheres to a standard style is easier to maintain and update. PEP 8’s guidelines help you write code that is more maintainable in the long run.


Without PEP 8:


def processData(data): 

  result = data["name"].upper() + " is " + str(data["age"]) + " years old"

  return result


    

With PEP 8:


def process_data(data):

    result = f"{data['name'].upper()} is {data['age']} years old"

    return result


#clcoding.com

3. Collaboration

Reason: When everyone on a team follows the same style guide, it’s easier for team members to read and understand each other’s code, making collaboration smoother.


Without PEP 8:


def fetchData(): 

  # fetch data from API

  data = {"name":"John","age":30}

  return data

With PEP 8:


def fetch_data():

    # Fetch data from API

    data = {"name": "John", "age": 30}

    return data

2. Readability

Reason: Readable code is easier to understand and debug. PEP 8 encourages practices that make your code more readable.


Without PEP 8:


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

With PEP 8:


def add(a, b):

    return a + b

1. Consistency

Reason: Consistent code is easier to read and understand. PEP 8 provides a standard style guide that promotes consistency across different projects and among different developers.


Without PEP 8:


def my_function():print("Hello"); print("World")

my_function()

Hello

World

With PEP 8:


def my_function():

    print("Hello")

    print("World")


my_function()

Hello

World

0 Comments:

Post a Comment

Popular Posts

Categories

AI (29) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (121) C (77) C# (12) C++ (82) Course (67) Coursera (195) Cybersecurity (24) data management (11) Data Science (100) Data Strucures (7) Deep Learning (11) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Pandas (3) PHP (20) Projects (29) Python (831) Python Coding Challenge (277) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (41) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses