logo CBCE Skill INDIA

Welcome to CBCE Skill INDIA. An ISO 9001:2015 Certified Autonomous Body | Best Quality Computer and Skills Training Provider Organization. Established Under Indian Trust Act 1882, Govt. of India. Identity No. - IV-190200628, and registered under NITI Aayog Govt. of India. Identity No. - WB/2023/0344555. Also registered under Ministry of Micro, Small & Medium Enterprises - MSME (Govt. of India). Registration Number - UDYAM-WB-06-0031863

What are the basic rules of Python?


Basic Rules of Python

Python, like any programming language, has a set of rules and conventions that developers should follow to write clean, readable, and maintainable code.

 

Here are some basic rules and guidelines for Python:

 

1. Indentation:

  • Python uses indentation to indicate blocks of code (instead of braces {} as in some other languages).
  • Use four spaces for each level of indentation. Do not use tabs.
# Good
if x > 0:
    print("Positive number")

# Bad (using tabs)
if x > 0:
\tprint("Positive number")

 

2. Whitespace:

  • Use whitespace to improve code readability.
  • Separate functions and classes with two blank lines.
  • Use a single space around operators and after commas.
# Good
def add_numbers(a, b):
    return a + b

# Bad (missing spaces)
def add_numbers(a,b):
    return a+b

 

3. Comments:

  • Use comments to explain complex parts of the code or to provide context.
  • Keep comments up-to-date; remove unnecessary comments.
# Good
result = x + y  # Adding x and y

# Bad (redundant comment)
result = x + y  # Add x and y to get the result

 

4. Naming Conventions:

  • Follow the PEP 8 naming conventions.
  • Use descriptive variable and function names.
# Good
def calculate_average(numbers_list):
    total = sum(numbers_list)
    count = len(numbers_list)
    return total / count

# Bad (non-descriptive names)
def avg(nums):
    t = sum(nums)
    c = len(nums)
    return t / c

 

5. String Quotes:

  • Use single or double quotes consistently.
  • Triple-quotes are used for docstrings or multi-line strings.
# Good
message = "Hello, World!"

# Bad (mixing quotes)
message = 'Hello, World!'

 

6. Imports:

  • Import modules and packages at the beginning of the file.
  • Use a separate line for each import.
# Good
import math
import os

# Bad (multiple imports on one line)
import math, os

 

7. Function and Method Arguments:

  • Put default arguments at the end of the argument list.
  • Use meaningful default values.
# Good
def calculate_total(price, tax_rate=0.1):
    return price + (price * tax_rate)

# Bad (default argument not at the end)
def calculate_total(tax_rate=0.1, price):
    return price + (price * tax_rate)

 

8. Mutable Default Arguments:

  • Avoid using mutable objects (e.g., lists, dictionaries) as default values for function arguments.
# Good
def process_items(items=None):
    if items is None:
        items = []
    # rest of the function

# Bad (using a mutable default argument)
def process_items(items=[]):
    # rest of the function

 

9. Avoid Global Variables:

  • Minimize the use of global variables.
  • Prefer passing values as arguments and returning results from functions.

 

10. Exception Handling:

  • Handle exceptions appropriately using try, except, and other relevant clauses.
  • Be specific about the exceptions you catch.
# Good
try:
    result = x / y
except ZeroDivisionError:
    result = float('inf')

# Bad (too broad exception handling)
try:
    result = x / y
except Exception:
    result = None

 

These basic rules contribute to writing clean and readable Python code. Adhering to these conventions helps make your code consistent and easy to understand for both yourself and other developers.

 

Thank you,


Give us your feedback!

Your email address will not be published. Required fields are marked *
0 Comments Write Comment