DEV Community

Cover image for Python tips for beginners, the inexperienced, the beaten and the damned...
Jaako
Jaako

Posted on

Python tips for beginners, the inexperienced, the beaten and the damned...

What I think about Python?

I learned how to code in Python when I was just a sophomore college student, and for a student who only knows how to code in C and have only seen low-level programming languages like Fortran, Basic and Cobol. Discovering Python was a Godsend.

I can never forget that feeling of witnessing a 15 liner in C reduced to a 1 liner in Python. It felt magical, the same feeling as a mathematician discovering a new formula for the first time. Just pure amazement of the things it could do.

And if I'm going to rate Python from 1 to 10 it would be the cover photo above.

Let's start!

These are for beginners, the inexperienced and those who have just started learning Python.

1. First tip! Follow and Conform to the PEP 8 style guide

Conform

Most beginners do not even follow the PEP 8 style guide nor even have heard about it.

When I started coding in Python, I did not follow any style guides whatsoever. I only did the bare minimum and as much as possible I try to make the code not confusing.

In fact, I have only heard about PEP 8 and other style guides just recently upon entering the coding industry, what a shame.

I'm not going to explain PEP 8 or its pros and cons in this post, so here are some links that could help you learn more about it.

My favorite link to learn PEP 8: https://realpython.com/python-pep8/

Another link where I got this awesome quote from: https://www.python.org/dev/peps/pep-0008/

However, know when to be inconsistent -- sometimes style guide recommendations just aren't applicable. When in doubt, use your best judgment. Look at other examples and decide what looks best. And don't hesitate to ask!

And there is also a PEP 8 checker: http://pep8online.com/

2. Second tip! Don't use list comprehension other than making a list.

No way

Another thing that I see most beginners do is their blatant use of list comprehensions. I'm also not a stranger to that mistake, and I am very thankful that an experienced programmer told me how to properly use it.

Comprehensions are supposed to return a list, dictionary a set or a generator and using it other than that beats its purpose.

Using list comprehensions to print some values is forgivable but appending values to other list, that is heresy of the highest order.

I mean you can get a free pass to use comprehensions when you're debugging something just make sure you remove it afterwards. But anything other than that is a big NO.

This is forgivable,

[print(f'I love number {x}!') for x in range(0,100)]
Enter fullscreen mode Exit fullscreen mode

because you only shortened this code.

for x in range(0,100):
    print(f'I love number {x}!')
Enter fullscreen mode Exit fullscreen mode

But this on the other hand is heresy,

num = []

[num.append(x) for x in range(0, 100)]
Enter fullscreen mode Exit fullscreen mode

deus vult

and can be easily refactored to this:

num = [x for x in range(0,100)]
Enter fullscreen mode Exit fullscreen mode

One might argue, "But it is a lot shorter and cleaner sire...". Well, you are not wrong but comprehensions are created for another purpose my child.

It's just the same as saying that "Killing all of the world's population could save the planet". It might be the right thing to do but why are we saving the planet in the first place? or saying that "A knife can be used as a spoon also". Yes, you can use it as a spoon but a knife is not made for that kind of function and you'll just hurt yourself in doing so.

Bonus tip:

3. Third tip! Utilize f-strings!

reason
For some reason other programmers are not utilizing this radical way of formatting a string. It's a lot faster, better and less prone to error so why aren't everyone using it?

Check the code below for comparison:

Ugly and not elegant.

num = 3
# Stop doing this
print("The tree is " + num + " years old.")
Enter fullscreen mode Exit fullscreen mode

This is not sexy.

# Prone to error
print("The tree is %s years old." % (num))
Enter fullscreen mode Exit fullscreen mode

Even more so.

# This has a tendency to become extremely long.
print("The tree is {} years old.".format(num))
Enter fullscreen mode Exit fullscreen mode

This is the real deal and sexy as hell.

# A lot faster and less prone to error
print(f"The tree is {num} years old.")
Enter fullscreen mode Exit fullscreen mode

I'm not going to dive deep about why f-string is the future, so here are some links for you to read and learn more.

Explains why you need to use f-string:
https://realpython.com/python-f-strings/

How they work:
https://hackernoon.com/a-closer-look-at-how-python-f-strings-work-f197736b3bdb

String formatting best practices:
https://realpython.com/python-string-formatting/

4. Fourth tip, Use the Python modules and utilize every one of them.

Whether it is a builtin module or a package which you need to install, you need to learn how to utilize them properly.

This is another thing that most inexperienced Python programmers overlook. They tend to not use any of the modules that could help make their jobs a lot easier.

When you are not utilizing those modules.

notutilized

When you are utilizing those modules.

utilized

All you need to do is read and understand the documentation.

I learned that just because everything is working fine does not mean it cannot be improved. Using a module could make your code more efficient and faster. It can also reduce the lines dramatically by removing unnecessary codes that the module replaced.

For example, we want to count how many of each color appeared in the list.

data = ['green', 'red', 'red', 'green', 'green', 'red', 'orange', 'green', 'green', 'green', 'red']
Enter fullscreen mode Exit fullscreen mode

We could do this, which is U-G-L-Y ugly:

new = {}
for x in data:
    new[x] = data.count(x)

print(new)

Enter fullscreen mode Exit fullscreen mode

But can be improved to this, by utilizing dictionary comprehension and it looks meh:

new = {x:data.count(x) for x in data}
Enter fullscreen mode Exit fullscreen mode

But by using Counter from collections module, tada! I now present a lot cleaner and shorter code:

from collections import Counter

new = Counter(data)
Enter fullscreen mode Exit fullscreen mode

Here are some useful modules:

That's all folks!

To be honest this post is a lot more about my mistakes as a beginner and what I learned from it. I hope that you learned from this post and always get feedback from anyone that you think could help you improve your skills as a programmer.

Top comments (6)

Collapse
 
bretthancox profile image
bretthancox

Great article.

I remember reading the updates when f strings were added and getting genuinely excited. Complicated string formatting is so much more readable now.

Your example of Counter from collections is also perfect. That first time I found out about it was fantastic and depressing. I couldn't imagine how much time I'd wasted writing loops to do the same task.

Generally, Python has one thing to remember: if you're doing it regularly in code, others probably do as well, so there's probably a library for it. If you're lucky, it might have been written in C and be more efficient than anything you can write in Python.

Collapse
 
jaakofalltrade profile image
Jaako • Edited

Thank you and by the way, this is really a good thing to remember.

if you're doing it regularly in code, others probably do as well, so there's probably a library for it.

Collapse
 
ejbarba profile image
Elijah Jeremiah L. Barba

This is good!

Collapse
 
jaakofalltrade profile image
Jaako

Thank you, you don't know how much this comment means to me.

Collapse
 
kojiadrianojr profile image
JARVUC

Great post!

Collapse
 
jaakofalltrade profile image
Jaako

Thank you!