×
Post thumbnail

Begineers common mistakes as a python developer - Hands on Guide..

1. Improper Variable Naming
  • Mistake: Using vague or confusing names
  • Best Practice: Use clear, descriptive names
## Bad Practice
x = 5 
## Good Practice
item_count = 5

2. Not Using Snake Case for Variables and Functions
  • Mistake: Using other naming conventions
  • Best Practice: Use snake_case for readability
# Bad
itemCount = 5
def calculateTotal(): pass

# Good
item_count = 5
def calculate_total(): pass

3. Incorrect Indentation
  • Mistake: Mixing spaces and tabs, inconsistent indentation
  • Best Practice: Use 4 spaces per indentation level
# Bad
if True:
    print("Hello")
 print("World")

# Good
if True:
    print("Hello")
    print("World")

4. Using Mutable Default Arguments
  • Mistake: Using mutable types (like lists or dicts) as default arguments
  • Best Practice: Use None and initialize inside the function
# Bad
def add_item(item, items=[]):
    items.append(item)
    return items

# Good
def add_item(item, items=None):
    if items is None:
        items = []
    items.append(item)
    return items

5. Neglecting to Handle Exceptions
  • Mistake: Not using try-except blocks to handle errors
  • Best Practice: Always handle potential exceptions
# Bad
result = 1 / 0

# Good
try:
    result = 1 / 0
except ZeroDivisionError:
    result = None
    print("Cannot divide by zero")

6. Not Using List Comprehensions
  • Mistake: Using loops for simple list operations
  • Best Practice: Use list comprehensions for concise code
# Bad
squares = []
for x in range(10):
    squares.append(x**2)

# Good
squares = [x**2 for x in range(10)]

7. Hardcoding Values
  • Mistake: Hardcoding values instead of using variables/constants
  • Best Practice: Use constants or configuration files
# Bad
print("Welcome to the program version 1.0")

# Good
VERSION = "1.0"
print(f"Welcome to the program version {VERSION}")

8. Ignoring PEP 8 Style Guide
  • Mistake: Writing code that does not adhere to PEP 8 standards
  • Best Practice: Follow PEP 8 guidelines for consistent style
# Bad
def func(x):return x**2

# Good
def func(x):
    return x**2

9. Not Using Virtual Environments
  • Mistake: Installing packages globally
  • Best Practice: Use virtual environments to manage dependencies
# Bad
pip install numpy

# Good
python -m venv myenv
source myenv/bin/activate
pip install numpy

10. Ignoring Code Comments and Documentation
  • Mistake: Not commenting code or providing documentation
  • Best Practice: Write meaningful comments and docstrings
# Bad
def add(a, b):
    return a + b

# Good
def add(a, b):
    """Return the sum of a and b."""
    return a + b

Quick Reference Commands
  • Run a Python Script: python script.py
  • Create a Virtual Environment: python -m venv myenv
  • Activate Virtual Environment:
    • Windows: myenv\Scripts\activate
    • Unix/Mac: source myenv/bin/activate
  • Install Packages: pip install package_name
  • Check PEP 8 Compliance: pip install flake8 then flake8 script.py

By following 🪜 these guidelines 📚 and practices, you'll be well on your way to writing professional 🧠, clean, and efficient Python 🐍 code.

• • •

Latest Opinions

No Opinion so far...

End.

No internet connection

Trying to reconnect...

Loading...