Member-only story

9 Python Errors That Signal You’re Still a Beginner.

Elshad Karimov
2 min readDec 26, 2023

Python is popular, but even experts make mistakes. Here are 9 common Python errors, explained with examples, to help new coders

  1. Pythonic Idioms Ignored:
  • Wrong: Looping with for to create a list.
squares = []
for i in range(5): squares.append(i**2)
  • Right: Embrace Pythonic list comprehensions.
squares = [i**2 for i in range(5)]

2. Mutable Defaults Misused:

  • Wrong: Default list in function arguments.
def add_item(item, list=[]): list.append(item)
  • Right: Use None and initialize inside.
def add_item(item, list=None):
if list is None: list = []
list.append(item)

3. ‘is’ Operator Confusion:

  • Wrong: Using ‘is’ for value equality.
if a_list is another_list: ...
  • Right: Use ‘==’ for equality checks.
if a_list == another_list: ...

4. Indentation Errors:

  • Wrong: Misaligned indentation.
def example():
print("Oops!")
  • Right: Correctly indented code.
def example():
print("Correct!")

5. Variable Scope Oversight:

  • Wrong: Accessing local variables outside.
def func(): x = 10
print(x)
  • Right: Define and return variables appropriately.
def func():
x = 10
return x
print(func())

6. Global Variable Overuse:

  • Wrong: Modifying globals inside functions.
x = 5
def change(): x = 10
  • Right: Pass and return values.
x = 5
def change(x): return x + 5
x = change(x)

7. Ignoring Exception Handling:

  • Wrong: Dividing by zero without handling.
print(10/0)
  • Right: Use try-except for error handling.
try: 
print(10/0)
except ZeroDivisionError:
print("Can't divide by zero.")

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Elshad Karimov
Elshad Karimov

Written by Elshad Karimov

Software Engineer, Udemy Instructor and Book Author, Founder at AppMillers

No responses yet

Write a response