DEV Community

Asma Dua
Asma Dua

Posted on

"5 Python mistakes I keep making as a beginner (and how I fixed them)"

## 1. Using return instead of print inside a loop

This one bit me early on. I wanted to print every item in a list, so I wrote something like this:

python
def show_items(items):
for item in items:
return item

show_items(["apple", "banana", "cherry"])

Run it, and... nothing prints. Why? return doesn't just output a value it immediately exits the function. So the loop runs exactly once, hands back "apple", and the function is done. banana and cherry never gets a chance.

The solution is simple once you see it:

python
def show_items(items):
for item in items:
print(item)

show_items(["apple", "banana", "cherry"])

Lesson: print() displays something and lets execution continue. return hands a value back to whoever called the function and ends it. They can't be taken for same.

  1. Writing a ternary expression as literal text inside an f-string

I wanted to print whether a number was even or odd, so I wrote:

python
num = 7
print(f"The number is {'even' if num % 2 == 0 else 'odd'})")

Small typo — the closing } was in the wrong place, so part of my ternary logic ended up printed as literal text instead of being evaluated. It's an easy mistake to make because f-string expressions look like normal code, but every {} boundary has to be exactly right.

Lesson: Anything you want evaluated inside an f-string has to be fully and correctly wrapped in {} — including the entire ternary expression, not just part of it.

  1. Nesting a plain string inside an f-string ternary

A frustrating version of the same problem:

python
status = "active"
print(f"Status: {'User is online' if status == 'active' else 'User is offline: {status}'}")

Here, the inner strings 'User is online' and 'User is offline: {status}' are just regular strings — not f-strings. So {status} inside that second string prints literally as {status} instead of showing the actual value.

The fix: make sure any inner string that needs interpolation is also an f-string:

python
print(f"Status: {'User is online' if status == 'active' else f'User is offline: {status}'}")

Lesson: The f prefix only applies to the string it's directly attached to. Nested strings need their own f if they need their own interpolation.

  1. Mixing up loop variables and the original list name python scores = [85, 92, 78, 90]

for score in scores:
print(scores) # oops — meant to print score, not scores

This one doesn't throw an error, which makes it worse — it just quietly prints the wrong thing four times instead of the four individual scores. It usually happens when I'm typing fast and my fingers default to the more "familiar" variable name.

Lesson: After writing a loop, it's worth a quick second glance — is the singular loop variable being used inside the loop body, or did the plural list name sneak back in?

  1. Forgetting self when accessing an attribute inside a class method

This one showed up once I started learning classes:

python
class Dog:
def init(self, name):
self.name = name

def bark(self):
    print(f"{name} says Woof!")   # here it is a NameError because name is not defined
Enter fullscreen mode Exit fullscreen mode

I dropped the self. in front of name inside bark(), so Python went looking for a plain variable called name — which doesn't exist inside that method. The fix is just remembering that any attribute stored on the object has to be accessed through self:

python
def bark(self):
print(f"{self.name} says Woof!")

Lesson: Inside a class, self.attribute and a plain attribute are two completely different things. Only self.attribute refers to the data actually stored on the object.

None of these mistakes are advanced, they're the kind of small, easy to miss errors that come from moving fast and not yet having the muscle memory for the syntax. Writing them down has honestly helped more than just fixing them silently and moving on. If you're a beginner too, hopefully seeing these saves you a few minutes of confused debugging.
although I'm having hard time keeping them in this small mind of mine, but practice makes it better which i'm not doing also because of university

Top comments (0)