While Python is designed to be beginner-friendly, there are some common pitfalls that hold newcomers back. Experienced Python developers know how to avoid traps like these, but for beginners they can mean hours of frustration. If you’ve tried to learn Python in the past and felt like you hit a brick wall, do not give up! Once you get past these initial hurdles, you’ll be on a path of steady improvement.
Don’t feel bad if you’ve gotten stuck at this stage! An operating system and a programming language are two complicated pieces of technology, and getting them to play nice together can be tricky. Following a guide is a good idea, but it’s difficult to know which one. Some of the guides online were written years ago or are intended for experienced programmers, and they all have different instructions. Which one do you follow?
The good news is that I’ve narrowed down the easiest method for you. I recommend that all beginners, across both Windows and Mac, download the Anaconda Distribution to get started with Python. Anaconda is a major company in the Python data science space, and they call it a “distribution” because it comes pre-packaged with a lot of helpful tools.
First and foremost, the Anaconda Distribution includes a reliable installation wizard. You can mindlessly click through and it will install like any normal app. Second, it provides a graphical user interface, Anaconda Navigator, that lets you use Python in several different formats. If you are writing Python code for the first time, I’d recommend launching a Jupyter Notebook from their list of options. Jupyter Notebooks are normally used for doing data analysis, but also serve as a beginner-friendly sandbox for code. Finally, the Anaconda Distribution includes a wealth of data science tools which are normally even more difficult to install than Python. This distribution is a great way to launch into Python, especially if you are interested in data.
The main downside to using the Anaconda Distribution is its size. It will take up 300MB, while a bare-bones installation of Python would only take around 50MB. However, to put this all in perspective, a handful of high-resolution photos could easily fill 300MB as well. On a modern computer, this amount of space shouldn’t matter.
Your First Error Message
At some point, inevitably, you will type something that isn’t valid Python code. This is a frequent occurrence, even for professionals. Perfectly normal, perfectly healthy. Python will stop when it reaches your funky code and throw up its hands. It will show you what it was reading before it crashed, essentially announcing “I can’t figure out what to do with this!” When Python spits out this information, it is called a “traceback.” It may look technical and scary, but its entire purpose is to help provide clues about what to fix.
If you were running something simple, like a single line of code, the traceback will be short, like this example below:
Or, if the code is more complex, it might look like this:
To read a traceback, you need to understand two things. First, how to understand the layout of the information. Second, how to translate the error it is reporting into human terms.
The layout of the traceback tells you where the problem arose. This is pretty trivial information in the first example since there is only one line of code. However, we can still break down the information it’s providing, starting on the line that begins with “File.” The line contains three sections, separated by commas. The first section gives you the name of the file Python is running code from, which in this case is “<stdin>” (which means the user typed it in). The second section tells you which line of that file Python was on when it crashed. The third section tells you what procedure it was carrying out when things went wrong. In this case, it wasn’t doing any procedure in particular, just running the whole file, so it says “in <module>” here.
When things get more complex, and you import someone else’s code, you’ll see longer tracebacks like in the second example. Why is this? Because when Python crashes, it will spit out everything it was in the middle of doing. When you are using external code, Python will spit out what was going on inside the tools you imported as well as in your own code. It will print out where the crash started, and then working its way upward, it will print out everything that led up to the error itself. So, when you are looking at a traceback, you will usually find the most important information at the bottom.
At the absolute bottom of a traceback, you’ll find an error message that will tell you exactly what caused the crash. Errors come in different flavors. If you know what causes each flavor of error, you are will know what to look for when bug hunting. The problem is in translating the very technical names for these errors back to English.
Some errors are very common, or they're decipherable from the accompanying error message. In the first traceback example, you see a TypeError which tells you Python was expecting a real number but got a string. Whoops! This is a fairly straightforward error, so I can either rely on the error message by itself or use the Python error documentation to look up to do with this error. Then I’ll have to go back to my code and find the string that was intended to be a number. Simple mistakes like this come up a lot, so it will soon become second nature to find and fix them.
However, other times you will see an error you don’t recognize or understand. I might not know what the heck a “schema” is, for example, in the second traceback ending with a MissingSchema exception. Here you can use Stack Overflow to get help translating this error into English. If you haven’t heard of it before, Stack Overflow is a Q&A forum for programmers. It is used by nearly every professional programmer, so any error you encounter has likely already been discussed and solved. The fastest route to this communal knowledge is to just copy-paste your error into google and add “stack overflow” to the end. Users vote on the quality of posted answers, so the help you need will usually be within the first two answers provided.
Hunting Down a Bug
When your code is doing something unexpected and confusing, your first impulse will be to Just Fix It. You’ll make a guess about what needs to be changed and then re-run your code. Then you’ll make another guess. And yet another guess. As the guesses pile up, so does your frustration. By the time you’ve made a dozen incorrect guesses, you will feel like throwing your keyboard through a window.
Taking shortcuts like this is human nature. We want our problems fixed quickly, and with minimal effort. However, when you tire of taking shots in the dark, you need to try a more systematic approach.
Being systematic about debugging means sticking to two important principles:
First, get visibility before you make changes. This is pretty common sense: you don’t even know where the problem is yet! You might just be making things worse. You have a story in your head about what your code is doing, but that story is likely wrong. The fact you have a bug is a strong indication of that. So the first order of business is to bring that story in your head down to earth and find where your expectations are mistaken. Here your best tool, as a beginner, is the print function. If you peek inside the intermediate steps in your program, you’ll likely find where things are going wrong.
Second, you need to divide and conquer. Printing out the result of every single line is ridiculously time-consuming, especially as your program gets longer and longer. What is an easier way to find it? Well, if you can verify that things still look good at the midpoint of your program, then you can assume the bug is in the second half of your code. And similarly, if you print out your variables halfway through the second half, you can figure out which quarter of your program the bug lives in. You won’t fix your program immediately, but you will make steady process in narrowing down where your bug might be, even for very large, complicated programs.
Sticking with It
The last and hardest part of picking up Python is sticking with it. This might sound like a problem of willpower, but it’s not! The problem is in the approach that most people take. See if this sounds familiar: You give yourself the goal of learning Python, and then find a nice thick textbook. You spend hours reading and doing exercises, but after a couple of days, you stop. You feel exhausted even just thinking about Python. What went wrong?
The main problem is that you’ve given yourself no incentive to keep going. This is why it feels exhausting—you do a ton of work and get nothing in return. Imagine playing Tetris for hours, but you can never get the score above zero. Sounds like a terrible game, but this is essentially the game you have set yourself up to play. You are pouring hours of work in, but when do you feel that you’ve accomplished something? After you have completely “learned Python?” You’re never going to totally finish that!
You need to look for small, concrete victories instead. Initially, you can reward yourself just for effort. For every hour you put in, treat yourself to an hour of Netflix or a scoop of ice cream. Once you have the basics down, the results of your code can be reward enough if you choose your projects wisely. Focus on tutorials that build up to something fun, like this rock-paper-scissors project. Alternatively, look for ways that Python can empower you at work, whatever field you happen to be in. A good, free resource for this is Automate the Boring Stuff with Python, which covers how to write Python programs to do things like sending emails and creating PDFs. When you create something fun or empowering, you’ll be re-energized and excited to learn more.
Python Classes at Noble Desktop
Python is one of the most exciting and versatile coding languages used today! You can get started learning Python at Noble through our Python in a Day and Intro to Python Programming courses. If you're looking for a more in-depth learning experience, we offer 30-hour Bootcamps to learn and master Python for Data Science and Python for Web Development. For those looking to use Python to gather large quantities of information from the web, Python for Webscraping is the class for you!