9 Reasons Why Most Fail Learning Python and How to Avoid Them

What will we cover?

In this tutorial we will look at some of the common reasons why most fail a learning Python, but more interestingly, how to avoid to fall in that category.

The good news is, that everyone can learn Python. I have been working as a developer for 15 years and seen people getting from no knowledge or programming to mastering it at a decent level. This includes people of all ages. Simply said, there is no reason why you shouldn’t be able to learn it too.

That said, it will be a challenging ride and nobody is just born naturally as a programmer. But it depends on many factors how big a challenge it will be. With proper forces and understanding of what you will face, you can increase your chances to 100% of mastering Python and get paid a great salary as well as getting many opportunities you do not get in other industries.

Let’s look at the 9 reasons. Actually, reason 9 is a big one.

#1 A poor learning approach

This is kind of funny. We all learn in different ways, but I have noticed that many have an idea that learning to program (Python in our case) is like you remember school. You go to class, learn the skill, and then you master it.

Actually, nobody learns like that. If you don’t use it. There is no way you will learn it.

Another problem with this naive idea, is, that it takes time to process the input. And actually, your brain does an amazing job while you are not consciously thinking about it.

Let me ask you a question. Did you ever sit with problem and try really hard to figure it out. It might be something like remember a name of one of your acquaintances. You think really hard about it, but you fail. You start talking about something else, but then suddenly, while you are focused on something else, the name pops up.

“Jack” you yell out of surprise, while the people around you do not understand what this is about. Well, the name was Jack, the acquaintance you forgot the name of.

Learning Python is the same. You need to let your brain process the new stuff, while you don’t think of it.

Take this free Python course in 17 lessons.

The course is in 17 lessons. Each lesson comes with some theory, and a project, along with a solution to the project.

Said differently:

  • It introduces new concepts for you to understand.
  • Then you are introduced to a small project using the new concepts.
  • Finally, a solution is provided for you to see how it can be solved.

Well, how would I suggest we apply our knowledge to that.

  • Day 1: See lesson with new concepts – take notes.
  • Day 2: Recap lesson (either from notes or video) – then see introduction to project. Try to solve the project and stop when you get stuck.
  • Day 3: Possibly recap lesson again, then continue with project. If you are really stuck – see solution.

This gives your subconscious time to learn and is not overloaded with new concepts.

The full 17 lessons will be 51 days of programming, but done in a way that will leave time for you to really learn it.

#2 Start something different when it gets tough

We used to learn that knowledge is power. When I was a kid the only place you could get knowledge was in school, documentaries in the telly, library books, some magazines, and so forth.

Most of them (all except libraries) were chosen by someone else. If I wanted to learn programming, there was only the library to go to. Because, programming was not in big demand. So there was no programming in school, no documentaries where you could learn programming, no magazines (at least available in my local stores), so there was the library.

Unfortunately, there we not many books on programming and most used programming languages I didn’t have access to.

Today, we live in the Information Age. But that actually has a price.

The information is competing to get our attention.

What do I mean?

Did you ever wonder why you keep scrolling the news feed on your favorite news media? You want to find the most interesting news and you do not want to miss the best ones and be the only one not knowing. You want to be the first one to share it your friends and close ones.

Learning is the same.

If you want to learn Python one week because you want that 6-figure contract they talk about, then when it becomes a big tough, well, you focus on something different, which looks more appealing than suffering the feeling of not being smart enough to learn Python. Why waste energy on that when you can learn something better giving you the live you have been wanting.

How can we use that to our understanding?

Know that the mind will try to convince you to start something else, something that looks easier, when it becomes challenging with learning Python.

There will be times when it doesn’t seem easy to learn Python. I have been sitting with bugs for days, felt hopeless, stupid, not smart enough. But when you solve it, I also know the feeling I will get.

Prepare yourself not to quit and start something else, when it gets tough.

#3 Lack of Focus

This is kind of related to the previous one. With all the possibilities comes difficult choices.

Most learners want to learn everything about everything.

As a senior, you know that is not possible. Most senior developers aer specialists and really good at what they do. That is why they get paid high salaries, not because they are average at everything.

When it comes to learning, the more clear you are on what you want to learn, the easier it is.

Python is a great programming language, which is great at many aspects of programming. But this also opens options to choose from, and as a beginner, you think you need to master them all.

And I understand, that you might not know if you want to make web apps with Python or want to use it for Machine Learning.

But becoming really good at one thing, will make it easier to transition to another programming aspect.

Stay focused on learning one purpose of Python programming. Learn that good. Then switch focus if you figure out something else has your interest.

A focused learning path will teach you programming faster.

#4 Unclear goal

The fastest from first line of code to a paid job that I know of was done of friend of mine. He did it 9 months. Yes, he has a steady salary every month. And he loves what he does.

He used to serve coffee in a coffee-shop (not the types in Amsterdam), and the pay was hopeless, people were rude, working hours awful, his boss was (I think we should sensor what he said here).

How did he manage to get this job that fast?

Well, learning Python programming in 9 month to a level where you get paid, that is remarkable. I am not talking about someone making script to automate trivial things. No, he builds web applications and writes Python backend code. That is great achievement.

What did he do?

He had a clear goal. He wanted to write Python backend code for web applications.

He first of all, stayed focused on only learning that, and also, his goal was clear.

It was not just something he tried to see if he liked it or had talent for. Do you know what I mean? Well, most of the time when we begin something, we only “try it” to “feel if it is something for me”.

That is vague and unclear what that means?

He had a clear goal. Python backend web applications.

Now he does it.

Keep a clear goal.

#5 Poor learning material

Do you remember the hopeless hours you spend in school being bored, counting the seconds until the break?

Luckily, our brain help us remember most positive things. But I do remember all that. What I remember better are the hours at lectures at University, listening to professors that know a great deal about the subject, but where really poor at teaching.

The truth is, most professors at University are more passionate about new science, than teaching students that are not engaging.

If I was to redo my University time, I would not waste time on the lectures. They were not good.

Funny enough, the books were either written by the professors or 600-1000 pages books authored by the sole purpose of getting as many pages as possible.

To take the professors books, well, they had the advantage, that they were more notes and only a few pages for each lecture. The bad part was, they were not good and often not structured very good.

The 600-1000 pages books, were no better, it was pages up and down written with the sole purpose of filing as many pages as possible. Why? Because they get paid by pages, and the ULTIMATE BOOK OF (insert the subject here), just needs to be 700+ pages to convince you.

Again, we want to learn it all. We want the ULTIMATE BOOK, instead of a specialized book.

What is the point?

Poor learning material kills the joy, and learning material often has another agenda than teaching. To sell books and courses.

The same is true of most material online. The main purpose is not making great learning material, it is to sell.

What to do about it?

Find an inspiring source to learn from. Someone that is burning to teach others, not just focused on earning money.

#6 Not realistic time frame

We all hear the story about that guy or girl, which got the first paying job in 9 months and less. And yes, I know it is possible.

But let’s face the truth.

No matter how talented and naturally gifted you are, you will not be on a senior developer level after 9 months.

It takes on average 5 years of full time professional working to become a senior developer. If you take a college degree in computer science, that is, you spent 5 years learning a bunch of programming stuff, then it still takes 5 years of professional working experience to become a senior developer.

Because learning is one thing. Using it in real world is another thing.

But when we hear it can be done in a short time frame, we immediately think we can do the same.

The only person I know who did it in 9 months, well, he committed all his free time to do it. All he did was, work his miserable job, train a bit to stay physically healthy and relax that way, eat and then code, code, code, and some more code.

Most people cannot commit to this kind of life for 9 months. And some people, me included, are not able to do it in 9 month, even if we had the time.


Because we all have different learning journeys. I always felt like a slow learning, and I most likely am. But you know what? In the end it doesn’t matter how long you used to get there. You will be rewarded in the same way.

How can we use this knowledge?

Well, don’t set goals like “I will get my first paying job in 12 months”. Sorry, only a few can do it, and I will reveal a last thing about my friend.

He did an extraordinary thing to get the job. Not just a normal application and land it. No he tried that with no success. He did more than that. First of all, he called the hiring manager talking to him. Had phone interviews first. Accepted to start with 14 days with no pay and no guarantee to get hired.

How long does it take on average?

Well, I cannot say. It depends on how much work you put in. Just saying, that these wonder stories are not everyday stories.

Other options, like making freelance jobs, is quite easy to get.

#7 Feel alone

For a senior like me, I didn’t think about at first. I started programming at age 12, and got some friends hooked on it as well. Then in college I was surrounded by like minded.

Then I talk to all of you and I realize. Many sit alone with problems and don’t know where to find answers to your problems.

Most don’t feel comfortable asking beginners questions on Stackoverflow, as it is more focused on how to solve specific questions and not on how to understand some programming concepts.

You burn to learn programming, but do not have a teacher to ask.

That makes you feel alone and many give up.

As a senior developer, you find all the answers to your problems on Stackoverflow and discuss it with your colleagues or other friends with similar interests you have gained by working in the tech industry your entire life.

But as someone starting to learn, where can you ask these questions?

  • The ideal is to have a one-on-one tutor, but that is often expensive, especially if you need qualified help.
  • Take a paid course with guaranteed help from the instructor.
  • Ask someone on Twitter (I get asked by people tagging me).
  • Start in group together – have a Facebook group where you study together with others. A place where you can ask questions and get help from others.

#8 Lack of motivation

This is funny. We talked about focus and clear goals, but often something interesting happens. You loose motivation.

Many think that motivation just sticks if you want something bad enough.

Let me ask you, did you ever experience that you quit on a goal that you were really exited about?

Most will agree they did.

Many will also say, I thought I had a clear goal and focused strategy to get there.

So what happened?

This is what we call lack of motivation. It can sneak up unexpected.

What to do to keep motivation?

Print out your goal. Like my friend. “I will become a Python backend web application programmer.”

Put it in a place you see every day, on the mirror in your bathroom. When you see it, read it out loud. Remind yourself how it will feel when you accomplish your goal.

If you do it every day, you will keep the motivation.

#9 Compare yourself to others

I think this one is big.

You sit there with a problem, you see other just solve it, you get the explanation, you still don’t get it, you feel stupid, because you are the only one in the room not getting it.

Or you see others succeed and get jobs in 9 months, you’ve been working on learning Python in 22 months, and you still don’t fully understand the basics.

Well, I have been there and can still there today. You can always find someone to compare yourself that will make you feel stupid.

The good news is, you don’t need to be a rock-star at every aspect of programming. You just need to be decent. We all make, so called rookie-mistakes, as senior develoers. I have done it, my colleagues have done it, we continue to do it. We ask for help, we laugh when we realize how embarrassing and simple the mistake was. But we have done so many times, and know it will happen again.

How to avoid this?

Stop comparing yourself to others. Your learning journey is yours. In the end, it doesn’t matter how you got there.

As a senior developer, no-one would look at your CV as notice you spend an extra year learning something. Because now you know it and they need your skills.

Python is in high demand.

Want to learn Python programming?

If you are hooked on learning Python I will suggest you follow my beginners course on Python.

It is well structured and has focus on you as a learner.

I suggest you break it down as explained in #1.

  • Day 1: See lesson with new concepts – take notes.
  • Day 2: Recap lesson (either from notes or video) – then see introduction to project. Try to solve the project and stop when you get stuck.
  • Day 3: Possibly recap lesson again, then continue with project. If you are really stuck – see solution.

Then continue that pattern for each lesson.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.

15 Most Useful pandas Shortcut Methods

What will you learn?

Everybody likes the pandas data structure DataFrame, but most miss out on what powerful methods it provides.

pandas is a huge module, which makes it difficult to master. Most just use the data structure (DataFrame), without utilizing the power of the methods. In this tutorial you will learn the 15 most useful shortcut that will help you when working with data in pandas data structures.

#1 groupby

The groupby method involves some combination of splitting the object, applying a function, and combining the result.

Wow. That sounds complex. But it is not. It can be used to group large amounts of data and compute operations on these groups.

Best way to learn is to see some example.

import pandas as pd
data = {'Items': ['Apple','Orange', 'Pear', 'Orange', 'Apple'], 
        'Price': [12, 5, 3, 7, 24]}
df = pd.DataFrame(data)

This results in this DataFrame.

The groupby method can group the items together, and apply a function. Let’s try it here.


This will result in this output.

As you see, it has grouped the Apples, Oranges, and the Pears together and for the price column, it has applied the mean() function on the values.

Hence, the Apple has value 18, as it is the mean of 12 and 24 ((12 + 24)/2). Similar, for Orange and Pear.

#2 memory_usage()

We get more and more data and our project get bigger and bigger. At one point you will need to analyze how much memory your data is using.

What memory_usage does, is, it returns the memory usage of each column in the DataFrame. Sometimes, the data type of a column is object, what that means is, that it is pointing to another object. To get the data usage of these objects included, you need to use the deep=True argument.

Let’s try both, to see the difference.

import pandas as pd
dtypes = ['int64', 'float64', 'complex128', 'object', 'bool']
data = dict([(t, np.ones(shape=1000, dtype=int).astype(t)) for t in
df = pd.DataFrame(data)

Then we can get the memory usage as follows.


Giving the following.

Index           128
int64          8000
float64        8000
complex128    16000
object         8000
bool           1000
dtype: int64

Also, with deep=True.


Giving the following where you see the object column is uses more space.

Index           128
int64          8000
float64        8000
complex128    16000
object        36000
bool           1000
dtype: int64

#3 clip()

clip() can trim values at the input threshold.

I find this is easiest to understand by inspecting an example.

import pandas as pd
data = {'col_0': [9, -3, 0, -1, 5], 'col_1': [-2, -7, 6, 8, -5]}
df = pd.DataFrame(data)

Then we apply the clip, which will ensure the values below -2 are replaced with -2, and values above 5, are replaced with 5. It clips the values.

print(df.clip(-2, 5))

#4 corr()

The correlation between the values in a column can be calculate with corr(). There are different methods to use: Pearson, Kendall, and Spearman. By default it uses the Pearson method, which will do fine giving you an idea if columns are correlated.

Let’s try an example.

import pandas as pd
df = pd.DataFrame([(.2, .3), (.0, .6), (.6, .0), (.2, .1)],
                  columns=['dogs', 'cats'])

The correlation is given by.


The value 1.0 is saying it is perfect correlation, which are shown in the diagonal. This makes sense, as the diagonal is the column with itself.

To learn more about correlation and statistics, be sure to check this tutorial out, which also explains the correlation value and how to interpret it.

#5 argmin()

The name argmin is a bit strange. What it does, it returns the position (the index) of the smallest value in a Series (column of a DataFrame).

import pandas as pd
s = pd.Series({'Corn Flakes': 100.0, 'Almond Delight': 110.0,
               'Cinnamon Toast Crunch': 120.0, 'Cocoa Puff': 110.0})


Corn Flakes              100.0
Almond Delight           110.0
Cinnamon Toast Crunch    120.0
Cocoa Puff               110.0
dtype: float64

And to get the position of the smallest value, just apply the method.


Which will give 0. Remember that it is zero-index, meaning that the first element has index 0.

#6 argmax()

Just like argmin, then argmax() returns the largest element in a Series.

Continue with the example from above.


This will give 2, as it is the largest element in the series.

#7 compare()

Want to know the differences between DataFrames? Then compare does a great job at that.

import pandas as pd
import numpy as np
df = pd.DataFrame(
         "col1": [1.0, 2.0, 3.0, np.nan, 5.0],
         "col2": [1.0, 2.0, 3.0, 4.0, 5.0]
     columns=["col1", "col2"],

We can compare the columns here.


As you see, the only row that differ is the above.

#8 replace()

Did you ever need to replace a value in a DataFrame? Well, it also has a method for that and it is called replace().

df = pd.DataFrame({'A': [0, 1, 2, 3, 4],
                   'B': [5, 6, 7, 8, 9],
                   'C': ['a', 'b', 'c', 'd', 'e']})

Let’s try to replace 5 with -10 and see what happens.

print(df.replace(5, -10))

#9 isna()

Wanted to find missing values? Then isna can do that for you.

Let’s try it.

import pandas as pd
import numpy as np
df = pd.DataFrame(dict(age=[5, 6, np.NaN],
                  born=[pd.NaT, pd.Timestamp('1939-05-27'),
                  name=['Alfred', 'Batman', ''],
                  toy=[None, 'Batmobile', 'Joker']))

Then you get the values as follows.


I often use it also in a combination with sum(), which will then tell how many rows in each column are missing. This is interesting to get an idea about the quality of the dataset.

age     1
born    1
name    0
toy     1
dtype: int64

#10 interpolation()

On the subject of missing values, what to do? Well, there are many options, but one simple can be to interpolate the values.

import pandas as pd
import numpy as np
s = pd.Series([0, 1, np.nan, 3])

This gives the following series.

0    0.0
1    1.0
2    NaN
3    3.0
dtype: float64

Then you can interpolate and get the value between them.

0    0.0
1    1.0
2    2.0
3    3.0
dtype: float64

This is just one way to deal with it. Dealing with missing values is a big subject. To learn more read this tutorial on the subject.

#11 drop()

Ever needed to remove a column in a DataFrame? Well, again they made a method for that.

Let’s try the drop() method to remove a column.

import pandas as pd
data = {'Age': [-44,0,5, 15, 10, -3], 
        'Salary': [0,5,-2, -14, 19, 24]}
df = pd.DataFrame(data)

Then let’s remove the Age column.

df2 = df.drop('Age', axis='columns')

Notice, that it returns a new DataFrame.

#12 drop_duplicates()

Dealing with data that has duplicate rows? Well, it is a common problem and pandas made a method to easily remove them from your DataFrame.

It is called drop_duplicates and does what it says.

Let’s try it.

import pandas as pd
df = pd.DataFrame({
    'brand': ['Yum Yum', 'Yum Yum', 'Indomie', 'Indomie', 'Indomie'],
    'style': ['cup', 'cup', 'cup', 'pack', 'pack'],
    'rating': [4, 4, 3.5, 15, 5]

This DataFrame as duplicate rows. Let’s see how they can be removed.

df2 = df.drop_duplicates()

#13 sum()

Ever needed to sum a column? Even with multi index?

Let’s try.

import pandas as pd
idx = pd.MultiIndex.from_arrays([
    ['warm', 'warm', 'cold', 'cold'],
    ['dog', 'falcon', 'fish', 'spider']],
    names=['blooded', 'animal'])
s = pd.Series([4, 2, 0, 8], name='legs', index=idx)

This will output.

blooded  animal
warm     dog       4
         falcon    2
cold     fish      0
         spider    8
Name: legs, dtype: int64
In [29]:

Then this will sum the column.


And it will output 14, as expected.

#14 cumsum()

Wanted to make a cumulative sum? Then cumsum() does the job for you, even with missing numbers.

import pandas as pd
s = pd.Series([2, np.nan, 5, -1, 0])

This will give.

0    2.0
1    NaN
2    5.0
3   -1.0
4    0.0
dtype: float64

And then.



0    2.0
1    NaN
2    7.0
3    6.0
4    6.0
dtype: float64

Where it makes a cumulative sum down the column.

#15 value_counst()

The value_counts() method returns the number of unique rows in a DataFrame.

This requires an example to really understand.

df = pd.DataFrame({'num_legs': [2, 4, 4, 6],
                   'num_wings': [2, 0, 0, 0]},
                  index=['falcon', 'dog', 'cat', 'ant'])

Here we see we have two rows with 4 and 0 (in that order), while the other rows have unique values.

num_legs  num_wings
4         0            2
2         2            1
6         0            1
dtype: int64

We see there are two rows with 4 and 0, and one of the other rows.

Bonus: unique()

Wanted the unique elements in your Series?

Here you go.

import pandas as pd
s = pd.Series([2, 1, 3, 3], name='A')

This will give the unique elements.

array([2, 1, 3])

Want to learn more?

Want to learn more about Data Science to become a successful Data Scientist?

Then check my free Expert Data Science Blueprint course with the following resources.

  • 15 video lessons – covers the Data Science Workflow and concepts, demonstrates everything on real data, introduce projects and shows a solution (YouTube video).
  • 30 JuPyter Notebooks – with the full code and explanation from the lectures and projects (GitHub).
  • 15 projects – structured with the Data Science Workflow and a solution explained in the end of video lessons (GitHub).

11 Great List Methods That Most Don’t Know

What will you learn?

After this list you will know the most useful methods and techniques with Python lists. It is funny, that many of these are unknown to many that work with Python. Be sure not to be one of them as they can save you a lot of time.

#1 in

This one is probably my favorite. To check if an element is in the list. This is easy with Python and just why we can’t stop loving it.

this_list = ['foo', 'bar', 'foobar', 'barfoo']
if 'foo' in this_list:
if 'foofoo' in this_list:

This will only print yes as ‘foofoo‘ is not an element in the list this_list.

On the other hand ‘foo‘ is in the list, hence it prints yes.

#2 sorted()

This is actually a built-in function and it returns a new sorted list from the items in an iterable.

An iterable is a list or anything you can iterate over in Python.

Let’s try it.

this_list = ['foo', 'bar', 'foobar', 'barfoo']
print(sorted(this_list, reverse=True))

This will output.

['bar', 'barfoo', 'foo', 'foobar']
['foobar', 'foo', 'barfoo', 'bar']

Hence, it can also sort in reverse order.

It is important to understand that it returns a new list with the items from the input argument.

Want to learn more Built-in functions?

#3 sort()

This is kind of funny. And it might look like the same. But there is a big difference.

The method sort() will sort the list in-place and not return a new list.

Let’s try it.

this_list = ['foo', 'bar', 'foobar', 'barfoo']

This will output.

['bar', 'barfoo', 'foo', 'foobar']

What was the difference? Well, sorted() returns a new list, and sort() sorts the list and changes the order of the element of the original list.

Why does that matter?

There can be many reasons – but one major one is if the list is huge and you need to save space and time – then in-place sorting is both more memory and speed efficient.

#4 for

Again, a favorite of mine. Iterating over a list in Python is such a pleasure. Why make complex syntax for something you need all the time?

Well, in Python it is easy to do.

this_list = ['bar', 'barfoo', 'foo', 'foobar']
for item in this_list:

This will output.


#5 append()

Adding an element to the end of a list is something you need all the time. Again, Python does it in a simple manner.

this_list = ['bar', 'barfoo', 'foo', 'foobar']

This will output.

['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo']

And see the added (appended) element at the end of the list.

#6 concatenate lists

This one is great. Again Python goes the extra mile to make things a simple as possible.

If you need to concatenate two lists together. How is that done?

Well, with the addition sign. See this.

list_a = ['foo', 'bar']
list_b = ['foobar', 'barfoo']
list_c = list_a + list_b

This will output.

['foo', 'bar', 'foobar', 'barfoo']

#7 index()

Sometimes you need the index of an element in a list.

Well, here you go.

this_list = ['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo']

This will output 2, as ‘foo‘ has the first occurrence in the list at index 2 (remember, that Python lists are zero indexed – meaning that the first element is 0, second element is 1, etc.).

#8 copy()

Sometimes you need a new copy of a list. Let’s try and understand what that means.

this_list = ['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo']
list_copy = this_list.copy()
list_copy.append('me element')

This will output.

['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo', 'me element']
['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo']

As you see, the append only modifies the list it is appended to. This might not surprise you.

But let’s try something different.

a = this_list

This will output.

['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo', 'element']
['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo', 'element']

Oh no – the element got appended to both lists – or what?

No, actually it is the same list, it is just two variables, which point to the same list.

Now copy starts to make sense.

#9 remove()

Now what if you need to remove an element form this list?

Let’s try how easy it could be.

this_list = ['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo', 'element']

This will output.

['bar', 'barfoo', 'foo', 'foobar', 'foobarfoo']

Oh, it was that simple.

#10 pop()

Actually a Python list can be used as a Stack out of the box.

Append (as we saw above) pushes an element to the back of the list – or you could say, the top of the stack.

And then, what does pop do?

this_list = ['barfoo', 'foo', 'foobar', 'foobarfoo']
element = this_list.pop()

This will output.

['barfoo', 'foo', 'foobar']

As you see, this is just as a stack.

You might complain, it does not have the same performance (big O) as a real stack. Well, because the list is implemented in such an awesome way, it has asymptotic the the same big-O complexity. What does that mean? That it is just as good as a “real” stack.

#11 del

This is actually one I don’t use that much – but it is handy if you like this notation.

Let’s just try it.

this_list = ['barfoo', 'foo', 'foobar']
del this_list[1]

This will delete element at position 1 and give this output.

['barfoo', 'foobar']

Wow. That was great.

Want to learn more?

If this is something you like and you want to get started with Python, then check my 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.’

9 Python Mistakes only Beginners Make

What will we cover?

To get better at Python programming it is good to explore what common mistakes beginners do. This will help you avoid the same pitfalls and understand why you shouldn’t do them.

#1 Use general imports

A common mistake beginners do are importing with the wildcard (*).

from module import *

First of all, this is bad practice and can make your program slow, as you import modules you don’t need.

Also, this can import shadows variables names (the same name from different sources) and make it difficult to know what variable you are referring to.

Instead you should import specific object or whole modules you need.

You should do the following if the module name is short.

import module

This will give you access to all the sub-modules with syntax like: module.submodule.

If the module name is long, then you should do as follows.

import longmodulename as lmn

This gives access to the module with lmn.

If you only need one (or a few, but not too many) element from a module then the following.

from module import xx

The above 3 ways to do it, makes the code easy to read and understand.

#2 try/except Without Specific Exception

It is common for beginners to have a broad except Exception, as it is easy.

Example could be like this.

    # do stuff
    print('Some stuff went wrong!')

First of all, it does not follow the PEP8 standard. You should always try to follow PEP8, as it makes your code easier to read, maintain, and understand for others and yourself later when you need to modify, extend, or debug your code.

Also, it catches all exceptions, like SystemExit and Control-C, which can make it difficult to terminate your program.

What you should do is to catch specific exceptions.

   # do your awesome stuff
except ValueException as exec:
   # Handle your exception
except NameError as exec:
   # Handle this exception here

Notice, you can have as many catchers (except) as you want for any exception type you need to handle.

#3 Not closing file

This is typical for beginners not to handle file opening proper.

f = open('file.txt', 'w')
f.write('some stuff')

Most Python interpreters close the file at the end of the program. But others don’t.

Even thought the Python interpreter closes it for you at the end, it takes up unnecessary resources from the underlying operating system, to keep a file pointer open after you needed. It can keep buffers of data that are needed to be written to storage. This can have undesired consequences if the program terminates unexpectedly.

This is especially a problem if your program is a service running and can also make the operating system run out of file pointers. It is bad practice not to close them after use.

The best practice is to use the with-statement.

with open('file.txt', 'w') as f:
    f.write('some stuff')
# Here the file pointer is closed.

This closes the file pointer after the with-statement.

#4 Not following PEP8 standard

We already talked about it, let’s talk about it again. You should always try to follow the PEP8 standard.

The reasons are clear.

  • It makes your code easy to understand for others – and yourself.

But what to do if you don’t know the PEP8 standard?

Well, I don’t know it by heart either, but there are checkers that can do that for you.

Install pep8 and let it check it for you and tell you what to correct.

pip install pep8

To check a python source file simply run and follow instructions of what to do.

pep8 python_file.py

#5 Dictionary Iteration with Keys

Actually, many beginners don’t know that you can iterate dictionaries in Python. But then they realize you can get all the keys in them.

capitals = {'Denmark': 'Copenhagen', 'Sweden': 'Stockholm', 'Norway': 'Oslo'}
for key in capitals.keys():
    print(key, capitals[key])

But if you want to get the key and values pairs you should do as follows.

for key, value in capitals.items():
    print(key, value)

#6 Not using List Comprehension

If you don’t know list comprehensions, then you often fall prey for making loops that make up lines of code not needed.

A simple example is converting a list of strings to a list of the same strings in lowercase.

my_list = ['UPPER', 'CASE', 'STUFF']
lower_case = []
for item in my_list:

This makes a simple transformation of a list quite difficult to read.

Using List Comprehension makes it easier.

lower_case = [item.lower() for item in my_list]

If you don’t know List Comprehension or want to learn more tricks with them, check out this the following post.

#7 Using range(len(…))

I have done this myself as a beginner. I simply didn’t know the awesome built-in functions in Python.

Say, you want to iterate over two lists simultaneously and process pairs of elements from each list. Then you might end up with the following code.

ist_0 = [1, 2, 3]
list_1 = [4, 5, 6]
for i in range(len(list_0)):
    print(list_0[i], (list_1[i])

This is bad practice and assumes that the length of the lists are the same.

The correct way to do this is to use the built-in function zip.

for i1, i2 in zip(list_0, list_1):
    print(i1, i2)

This also handles if one list is longer than the other.

If you want to learn more about the built-in functions in Python read the following guide.

#8 Use + to format strings

Most Python beginners get exited about how easy it is to concatenate strings in Python using the addition operator, that they end up using it for everything, like formatting strings.

name = "Rune"
my_str = "Hello " + name

This is actually inefficient as it generates a new string for each addition. Also, it just looks bad.

What you should use is formatted strings.

my_str = f'Hello {name}'

If you don’t know formatted strings, then you should read this guide.

#9 Using Non-explicit Variable Names

Here your PEP8 checker will catch you – but this is one of them that happens all the time. You create a variable with a single character.

x = 'my identifier'

While you code, it is the easiest choice. Especially, if the variable is just used in a small context.

But it makes your code difficult to understand and hard to debug.

Get those bad practices out of your system and give the variable meaningful names.

target_id = 'my identifier'

Want to learn more?

If this is something you like and you want to get started with Python, then check my 8 hours FREE video course with full explanations, projects on each levels, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • A FREE 70+ pages eBook with all the learnings from the lessons.