/ Python

Learn Python the right way in 5 steps

Python is an amazingly versatile programming language. You can use it to build websites, machine learning algorithms, and even autonomous drones. A huge percentage of programmers in the world use Python, and for good reason. It gives you the power to create almost anything. But -- and this is a big but -- you have to learn it first. Learning any programming language can be intimidating. I personally think that Python is better to learn than most, but learning it was still a rocky journey for me.

One of the things that I found most frustrating when I was learning Python was how generic all the learning resources were. I wanted to learn how to make websites using Python, but it seemed like every learning resource wanted me to spend 2 long, boring, months on Python syntax before I could even think about doing what interested me.

This mismatch made learning Python quite intimidating for me. I put it off for months. I got a couple of lessons into the Codecademy tutorials, then stopped. I looked at Python code, but it was foreign and confusing:

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

The above code is from the tutorial for Django, a popular Python website development framework. Experienced programmers will often throw snippets like the above at you. "It's easy!", they'll promise. But even a few seemingly simple lines of code can be incredibly confusing. For instance, why are some lines indented? What's django.http? Why are some things in parentheses? Understanding how everything fits together when you don't know much Python can be very hard.

The problem is that you need to understand the building blocks of the Python language to build anything interesting. The above code snippet creates a view, which is one of the key building blocks of a website using the popular MVC architecture. If you don't know how to write the code to create a view, it isn't really possible to make a dynamic website.

Most tutorials assume that you need to learn all of Python syntax before you can start doing anything interesting. This is what leads to months spent just on syntax, when what you really want to be doing is analyzing data, or building a website, or creating an autonomous drone. This is what leads to your motivation ebbing away, and to you just calling the whole thing off. I like to think of this as the "cliff of boring". You need to be able to climb the "cliff of boring" to make it to the "land of interesting stuff you work on" (better name pending).

A boring cliff. Boring cliffs are hard to climb.

After facing the "cliff of boring" a few times and walking away, I found a process that worked better for me. What worked was blending learning the basics with building interesting things. I spent as little time as possible learning the basics, then immediately dove into creating things that interested me. In this blog post, I'll walk you through step by step how to replicate this process, regardless of why you want to learn Python.

1. Figure out what motivates you

Before you start diving into learning Python, it's worth asking yourself why you want to learn it. This is because it's going to be a long and sometimes painful journey. Without enough motivation, you probably won't make it through. For example, I slept through high school and college programming classes when I had to memorize syntax and I wasn't motivated. On the other hand, when I needed to use Python to build a website to automatically score essays, I stayed up nights to finish it.

Figuring out what motivates you will help you figure out an end goal, and a path that gets you there without boredom. You don't have to figure out an exact project, just a general area you're interested in.

Pick an area you're interested in, such as:

  • Data science / Machine learning
  • Mobile apps
  • Websites
  • Games
  • Hardware / Sensors / Robots
  • Scripts to automate your work

Yes, you can make robots using Python! From the Raspberry Pi Cookbook.

Figure out one or two areas that interest you, and you're willing to stick with. You'll be gearing your learning towards them, and eventually will be building projects in them.

2. Learn the basic syntax

Unfortunately, this step can't be skipped. You have to learn the very basics of Python syntax before you dive deeper into your chosen area. You want to spend the minimum amount of time on this, as it isn't very motivating. I personally made it about 30% into the Codecademy Python tutorials, which was enough.

Here are some good resources to help you learn the basics:

  • Codecademy — does a good job of teaching basic syntax, and builds on itself well.
  • Learn Python the Hard Way — a book that teaches Python concepts from the basics to more in-depth programs.
  • Dataquest — I started Dataquest to make learning Python and data science easier. Dataquest teaches Python syntax in the context of learning data science. For example, you'll learn about for loops while analyzing weather data.
  • The Python Tutorial — the tutorial on the main Python site.

I can't emphasize enough that you should only spend the minimum amount of time possible on basic syntax. The quicker you can get to working on projects, the faster you will learn. You can always refer back to the syntax when you get stuck later. You should ideally only spend a couple of weeks on this phase, and definitely no more than a month.

3. Make structured projects

Once you've learned the basic syntax, it's possible to start making projects on your own. Projects are a great way to learn, because they let you apply your knowledge. Unless you apply your knowledge, it will be hard to retain it. Projects will push your capabilities, help you learn new things, and help you build a portfolio to show to potential employers.

However, very freeform projects at this point will be painful — you'll get stuck a lot, and need to refer to documentation. Because of this, it's usually better to make more structured projects until you feel comfortable enough to make projects completely on your own. Many learning resources offer structured projects, and these projects let you build interesting things in the areas you care about while still preventing you from getting stuck.

Let's look at some good resources for structured projects in each area:

Data science / Machine learning

  • Dataquest — Teaches you Python and data science interactively. You analyze a series of interesting datasets ranging from CIA documents to NBA player stats. You eventually build complex algorithms, including neural networks and decision trees.
  • Python for Data Analysis — written by the author of a major Python data analysis library, it's a good introduction to analyzing data in Python.
  • Scikit-learn documentation — Scikit-learn is the main Python machine learning library. It has some great documentation and tutorials.
  • CS109 — this is a Harvard class that teaches Python for data science. They have some of their projects and other materials online.

Mobile apps

  • Kivy guide — Kivy is a tool that lets you make mobile apps with Python. They have a guide on how to get started.

Websites

  • Flask tutorial — Flask is a popular web framework for Python. This is the introductory tutorial.
  • Bottle tutorial — Bottle is another web framework for Python. This is how to get started with it.
  • How To Tango With Django -- A guide to using Django, a complex Python web framework.

Games

An example of a game you can make with Pygame. This is Barbie Seahorse Adventures 1.0, by Phil Hassey.

Hardware / Sensors / Robots

Scripts to automate your work

Once you've done a few structured projects in your own area, you should be able to move into working on your own projects. But, before you do, it's important to spend some time learning how to solve problems.

4. Work on projects on your own

Once you've completed some structured projects, it's time to work on projects on your own. You'll still be consulting resources and learning concepts, but you'll be working on what you want to work on. Before you dive into working on your own projects, you should feel comfortable debugging errors and problems with your programs. Here are some resources you should be familiar with:

  • StackOverflow — a community question and answer site where people discuss programming issues. You can find Python-specific questions here.
  • Google — the most commonly used tool of every experienced programmer. Very useful when trying to resolve errors. Here's an example.
  • Python documentation — a good place to find reference material on Python.

Once you have a solid handle on debugging issues, you can start working on your own projects. You should work on things that interest you. For example, I worked on tools to trade stocks automatically very soon after I learned programming.

Here are some tips for finding interesting projects:

  • Extend the projects you were working on previously, and add more functionality.
  • Go to Python meetups in your area, and find people who are working on interesting projects.
  • Find open source packages to contribute to.
  • See if any local nonprofits are looking for volunteer developers.
  • Find projects other people have made, and see if you can extend or adapt them. Github is a good place to find these.
  • Browse through other people's blog posts to find interesting project ideas.
  • Think of tools that would make your every day life easier, and build them.

Remember to start very small. It's often useful to start with things that are very simple so you can gain confidence. It's better to start a small project that you finish that a huge project that never gets done. At Dataquest, we have guided projects that give you small data science related tasks that you can build on.

It's also useful to find other people to work with for more motivation.

If you really can't think of any good project ideas, here are some in each area we've discussed:

Data science / Machine learning

  • A map that visualizes election polling by state.
  • An algorithm that predicts the weather where you live.
  • A tool that predicts the stock market.
  • An algorithm that automatically summarizes news articles.

You could make a more interactive version of this map. From RealClearPolitics.

Mobile apps

  • An app to track how far you walk every day.
  • An app that sends you weather notifications.
  • A realtime location-based chat.

Websites

  • A site that helps you plan your weekly meals.
  • A site that allows users to review video games.
  • A notetaking platform.

Games

  • A location-based mobile game, where you capture territory.
  • A game where you program to solve puzzles.

Hardware / Sensors / Robots

  • Sensors that monitor your home temperature and let you monitor your house remotely.
  • A smarter alarm clock.
  • A self-driving robot that detects obstacles.

Scripts to automate your work

  • A script to automate data entry.
  • A tool to scrape data from the web.

My first project on my own was adapting my automated essay scoring algorithm from R to Python. It didn't end up looking pretty, but it gave me a sense of accomplishment, and started me on the road to building my skills.

The key is to pick something and do it. If you get too hung up on picking the perfect project, there's a risk that you'll never make one.

5. Keep working on harder projects

Keep increasing the difficulty and scope of your projects. If you're completely comfortable with what you're building, it means it's time to try something harder.

Here are some ideas for when that time comes:

  • Try teaching a novice how to build a project you made.
  • Can you scale up your tool? Can it work with more data, or can it handle more traffic?
  • Can you make your program run faster?
  • Can you make your tool useful for more people?
  • How would you commercialize what you've made?

Going forward

At the end of the day, Python is evolving all the time. There are only a few people who can legitimately claim to completely understand the language, and they created it.

You'll need to be constantly learning and working on projects. If you do this right, you'll find yourself looking back on your code from 6 months ago and thinking about how terrible it is. If you get to this point, you're on the right track. Working only on things that interest you means that you'll never get burned out or bored.

Python is a really fun and rewarding language to learn, and I think anyone can get to a high level of proficiency in it if they find the right motivation.

I hope this guide has been useful on your journey. If you have any other resources to suggest, please let us know!