Python tips for beginners, the inexperienced, the beaten and the damned...
Tags: python, beginners
Published: 2019-12-18 08:51:03

##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](https://media.giphy.com/media/CXcKi9NsRYa8U/giphy.gif)

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](https://media.giphy.com/media/spfi6nabVuq5y/giphy.gif)

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,
```python
[print(f'I love number {x}!') for x in range(0,100)]
```

because you only shortened this code.
```python
for x in range(0,100):
    print(f'I love number {x}!')
```

But this on the other hand is heresy,
```python
num = []

[num.append(x) for x in range(0, 100)]
```
![deus vult](https://media.giphy.com/media/vtPiQBn0wwXo4/giphy.gif)

and can be easily refactored to this:
```python
num = [x for x in range(0,100)]
```

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:
* There are people who don't know the difference between a __list__ and a __generator__, and when to use them. Make sure to be in the know.

* You can read more about __list__ vs __generators__ here: https://code-maven.com/list-comprehension-vs-generator-expression


###3. Third tip! Utilize f-strings!
![reason](https://media.giphy.com/media/J2OIhU2JZKO3ZAyAUF/giphy.gif)
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.
```python
num = 3
# Stop doing this
print("The tree is " + num + " years old.")
```


This is not sexy.
```python
# Prone to error
print("The tree is %s years old." % (num))
```


Even more so.
```python
# This has a tendency to become extremely long.
print("The tree is {} years old.".format(num))
```

This is the real deal and sexy as hell.
```python
# A lot faster and less prone to error
print(f"The tree is {num} years old.")
```

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](https://media.giphy.com/media/FCerKwnJpUqnm/giphy.gif)

####When you are utilizing those modules.
![utilized](https://media.giphy.com/media/6e61n9Z25XOGk/giphy.gif)


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.
```python
data = ['green', 'red', 'red', 'green', 'green', 'red', 'orange', 'green', 'green', 'green', 'red']
```

We could do this, which is U-G-L-Y ugly:
```python
new = {}
for x in data:
    new[x] = data.count(x)

print(new)

```

But can be improved to this, by utilizing dictionary comprehension and it looks meh:
```python
new = {x:data.count(x) for x in data}
```

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

new = Counter(data)
```

####Here are some useful modules:
* [datetime](https://docs.python.org/3/library/datetime.html)
* [itertools](https://docs.python.org/3/library/itertools.html)
* [functools](https://docs.python.org/3/library/functools.html)
* [collections](https://docs.python.org/3/library/collections.html)
* [requests](https://3.python-requests.org/)
* [sqlite3](https://docs.python.org/3/library/sqlite3.html)
* [arrow](https://arrow.readthedocs.io/en/latest/)


##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.