Common Questions

How Long Does It Take to Learn Python?

How long does it take to learn Python?

Well, how long is a piece of string? How much peanut butter is too much? Is this glass half empty or half full?

None of these questions has one correct answer. The answer to each depends on your opinion and your goals. And how long it takes you to learn Python depends on what you want to create with the language.

So, while I can’t tell you how long it will take to learn Python, I can give you deeper insight into the language and how it will suit your needs.

What Do You Need to Learn Python?

Python is a general-purpose language. You can use it for automating tasks such as backing up your computer, starting and stopping programs, and sending emails. It’s also good for web applications and trading stocks, currencies, or bonds.

Python supports both structured and object-oriented programming. That means that you can write your programs using two very different approaches—in the same language.

So, before you can determine how long it will take to learn Python, you need to define what you want to learn. Will learning the language’s syntax and basic constructs be enough? Or do you need to learn one of its specialized libraries like statistics, robotics, or science?

The real question isn’t “How long does it take to learn Python?” It’s “How long does it take to learn how to code for” whatever you’re trying to do.

Let’s take a quick look at the language and how it compares to others.

Python Is Simple

The Python language was designed with simplicity and readability in mind. This may seem like an idle boast, but simplicity and readability don’t come built-in for many languages.

Python Syntax

Let’s understand Python syntax by looking at the same example in three different languages.

Here’s a loop that prints the values 0 through 9 in C:

for( a = 0; a < 10; a++) {
  printf("%d\n", a);

To decipher this, you need to understand what’s going on inside the parentheses on the first line.

  • a = 0 initializes a to 0 before the loop executes.
  • a < 10 indicates the loop should stop when it reaches 10.
  • a++ means increment a by one each time a loop is completed.

The last step only happens after the loop completes once! It’s why it prints 0 through 9 and not 1 through 9.

A beginner doesn’t know by reading the code that each clause inside runs at different times (once at the start, before each iteration, and after each iteration). You know it because you read about it and messed up a few loops until you learned.

The body of the loop isn’t self-explanatory, either. C’s formatting syntax for printing an integer is another thing you just have to know. The function printf replaces the %d with the integer value of a. Then it adds a new line because that’s what \n means.

Now, here’s the same loop in Java:

for (int a = 0; a < 10; a++) {

Java uses the same syntax for loops. But it’s easier to print an integer since println takes an integer without the cryptic formatting string and prints a new line each time you call it. You still need to know about the System object, the out object that corresponds to the terminal, and a println method.

Finally, here’s how you do it in Python:

for a in range(10):

The first line almost reads like a sentence in English. Take a range of 10 numbers and assign each value to a. You need to know that the range starts at 0, which is why we get 0 through 9. (You can change that behavior by telling range where to start, but that’s a different topic.)

Printing a value is more intuitive, too. Python has a print instruction. It prints whatever you pass it.

Memory Management

Python is a garbage-collected language. This doesn’t mean that it’ll come to your office and empty your wastebasket once a week for you. It means that you can create new objects when you want and let Python worry about freeing up the memory. Java has this, too, as do many other modern languages. But it’s one less thing you need to learn when comparing Python to C or C++.

Data Types

One of the reasons that it took so much work to print an integer in C when compared to Python is how the two languages handle types.

In C, a was declared to be an int. We wanted a number that we could increment by one and compare to 10. C has specific rules about data types and what you can do with them. Int was the obvious choice.

But to print that int to the terminal, we needed to convert it to a string. (Or, more accurately, an array of characters terminated with a null.) That’s where printf and the formatting string came in.

In Python, we don’t need to provide a type for a, and we don’t need to worry about printing it. Python infers that for us and does the work.

Code Formatting

Most languages ignore white space.

That means this statement:

for (int a = 0; a < 10: a++) {

This statement:

for (int a = 0; a < 10; a++)

And this statement:

for (int a = 0; a < 10; a++) { System.out.println(a); }

Will all compile and run. They all mean the same thing.

So, while most Java developers would never use the last example, the first two differences are a matter of great debate. Wherever there’s a choice, there’s a debate.

In Python, white space counts and is part of how code is formatted. For most statements, there is only one way to format them. Braces like in Java and C don’t delineate the for loop above. The indentation before the print statement tells Python it’s part of the loop.

At first, this seems too limiting, especially if you’re coming to Python from another language. But after a while, you realize it’s not. It’s one less thing to worry about.

It also makes reading someone else’s code a lot easier! You don’t have to translate their style to yours since everyone’s code is formatted the same.

The Zen of Python

This approach to formatting is only one example of the Zen of Python.

While many languages boast more than one way to do things, Python tries to take the opposite approach. Although there’s often more than one way to do things in Python, there’s usually one obvious and best way to get them done. It’s usually the easiest and simplest way, too.

This programming approach reflects a strongly held belief in the Python community. In fact, that belief is so strong that it’s found its way into the official language standard. It’s the Zen of Python.

It’s built into the language.

$ python3
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
GCC 9.3.0 on linux
Type "help", "copyright", "credits" or "license" for more information.
>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Python Is Complicated, But Only When It Needs to Be

Python is used for everything from basic systems administration to controlling robotsstatistical analysis, biology, and much, much more.

These tasks are done with Python’s vast set of libraries. Python’s standard library is already powerful, and there’s little you can’t do with it. But it’s also easy to write new libraries for specialized tasks. PyPi, one of Pythons’ more popular library sites, has more than 270,000 libraries.

As such, when it’s time to write code for a specialized task, you can spend your time learning the library and how it maps into your job.

So, the real question isn’t “How long does it take to learn Python?” It’s “How long does it take to learn how to code for” whatever you’re trying to do.

And isn’t that how it should be?

PS: There’s no such thing as too much peanut butter.

This post was written by Eric Goebelbecker. Eric has worked in the financial markets in New York City for 25 years, developing infrastructure for market data and financial information exchange (FIX) protocol networks. He loves to talk about what makes teams effective (or not so effective!).