Why Python Became the Language of ML
Everyone uses Python. Why?
Open any machine learning job post, tutorial, research paper's code or Kaggle notebook, and you will find Python. Not Java, not C++, not JavaScript. Python, almost every time.
That is a little strange if you know one fact about Python: it is a slow language. For raw speed, C++ and Java beat it easily. And machine learning involves a huge amount of number crunching. So why would the whole field pick a slow language?
The answer explains a lot about how ML tools work, and it will make the rest of this chapter click. There are four reasons, and the third one is the most surprising.
Reason 1: It reads almost like English
Let us do one small job in two languages: find the average of four exam marks. Here it is in Java:
1public class Average {
2 public static void main(String[] args) {
3 int[] marks = {78, 92, 65, 88};
4 int total = 0;
5 for (int m : marks) {
6 total += m;
7 }
8 double average = (double) total / marks.length;
9 System.out.println("Average: " + average);
10 }
11}Average: 80.75
And here is the same thing in Python:
1marks = [78, 92, 65, 88]
2print("Average:", sum(marks) / len(marks))Average: 80.75
Eleven lines against two. And you can probably read the Python version out loud without knowing Python: "marks is this list, print the sum of marks divided by the length of marks".
This matters a lot in ML. Most people doing machine learning are not full-time programmers. They are data analysts, scientists, researchers, doctors, economists, students. They want to spend their energy on the problem, not on the language. Python gets out of the way.
Reason 2: Someone already wrote the hard parts
A library is a bundle of ready-made code that someone else wrote, tested and shared, so you do not have to. You bring it into your program with one import line and use it.
Python has the best set of ML libraries of any language, and they are all free. You will meet each of these in this chapter:
Key Points
- NumPy: fast maths on large lists of numbers. Almost every other ML library is built on top of it.
- pandas: working with tables of data, like a spreadsheet you control with code.
- Matplotlib: drawing charts and graphs.
- scikit-learn: ready-made classic ML models, the ones this course teaches.
- PyTorch and TensorFlow: for deep learning and neural networks.
Remember lesson 1.3, where you trained a real model in three lines: import it, create it, call fit? That was scikit-learn. The actual maths behind linear regression is several pages long. Someone wrote it once, very carefully, and now everyone can use it in one line.
Reason 3: Python is slow, but ML in Python is fast
Here is the surprising part. When you use NumPy or scikit-learn, the heavy maths does not actually run in Python.
Those libraries are written mostly in fast languages like C, C++ and Fortran. Python just gives the instructions. Think of it like driving a car. You hold the steering wheel and decide where to go. The engine does the hard work, and you never have to touch it. Python is the steering wheel. The fast code underneath is the engine.
Your code
Python. Short, readable, easy to change.
↓
Libraries
NumPy, pandas, scikit-learn, PyTorch. You call them from Python.
↓
The engine
Written in C, C++ and Fortran. This is where the heavy maths actually runs.
↓
Hardware
Your CPU, or a GPU for big models.
You can see the difference yourself. This code does the same job twice: double 10 million numbers and add them all up. First with a normal Python loop, then with NumPy.
1import time
2import numpy as np
3
4# Double 10 million numbers and add them all up. Twice.
5
6numbers = list(range(10_000_000))
7start = time.time()
8total = 0
9for n in numbers:
10 total += n * 2
11print(f"Plain Python: {total:,} in {time.time() - start:.2f} seconds")
12
13array = np.arange(10_000_000)
14start = time.time()
15total = (array * 2).sum()
16print(f"NumPy: {total:,} in {time.time() - start:.2f} seconds")Plain Python: 99,999,990,000,000 in 0.46 seconds NumPy: 99,999,990,000,000 in 0.02 seconds
Same answer, but NumPy was over 20 times faster. Your exact times will be different, since they depend on the computer, but the gap will be similar. When your data has millions of rows, that is the difference between waiting two seconds and waiting a minute.
Tip:This gives you a golden rule for ML code: if you are writing a for loop over a huge list of numbers, there is almost always a NumPy or pandas way to do it that is much faster. Lesson 2.3 shows how.
So Python gives you the best of both: code that is easy to write and read, running on an engine that is fast. That combination is the real reason it won.
Reason 4: Everyone else is already there
Once researchers started using Python for ML in the 2000s and 2010s, a snowball effect kicked in. More users meant more libraries. More libraries brought even more users.
Today that works in your favour in very practical ways. When you get an error, someone has usually asked about it online already, with an answer. When a new model comes out, its code is almost always released in Python first. Tutorials, courses and ML job interviews all assume Python.
There is one more reason worth mentioning, which leads into the next lesson. Python can be run a little bit at a time: run one line, look at the result, change something, run it again. ML work is full of that kind of trying and checking, especially in the explore step of the lifecycle from lesson 1.5. Tools like Jupyter and Google Colab are built around exactly this.
What about other languages?
Python is the main language, but not the only one you will come across:
Key Points
- SQL: the language for pulling data out of databases. Companies keep their data in databases, so most ML jobs expect you to know some SQL. Worth learning alongside Python.
- R: a language built for statistics. Still popular in research, universities and some analytics teams.
- C++: used to make the libraries themselves fast, and to run models in places where every millisecond counts.
- JavaScript: sometimes used to run small models directly inside a web browser.
But for learning ML and doing most day-to-day ML work, Python is the language. That is why this whole course uses it.
How much Python do you actually need?
Less than you might fear. You do not need to be an expert programmer to start machine learning. To follow this course comfortably, you should be okay with:
Key Points
- Variables, numbers and strings
- Lists and dictionaries
- if / else and for loops
- Writing and calling simple functions
- Importing a library and calling its functions
- Reading an error message without panicking
Things like classes, decorators or async code can wait. You will pick up what you need as you go. If some of the list above is new to you, spend a week or two practising Python basics first. Everything in this course will feel much easier.
Where people get confused
Key Points
- "Python is slow, so ML in Python must be slow." The heavy work runs inside libraries written in fast languages. Python only steers.
- "I have to master Python before I touch ML." The basics above are enough to start. You learn the rest by doing.
- "I need a powerful laptop for ML." For this course, you do not. Google Colab runs your code on Google's computers for free. That is the next lesson.
- "Python 2 or Python 3?" Always Python 3. Python 2 stopped being supported in 2020.
Quick check
If Python is a slow language, how can ML libraries written for Python be fast?
Because the heavy maths inside libraries like NumPy runs in fast compiled languages like C and Fortran. Python only gives the instructions, like a steering wheel controlling an engine.
What is a library, in one sentence?
Ready-made code that someone else wrote and shared, which you bring into your program with an import so you do not have to write it yourself.
You are about to write a for loop that goes through 5 million numbers one by one. What should you think first?
Whether NumPy or pandas can do the same thing without a loop. It almost always can, and it will usually be many times faster.
Recap
Key Points
- Python is easy to read and write, so people can focus on the problem instead of the language.
- It has the best free ML libraries: NumPy, pandas, Matplotlib, scikit-learn, PyTorch, TensorFlow.
- Python itself is slow, but the libraries do their heavy work in fast languages underneath.
- Everyone uses it, so help, tutorials and new tools show up in Python first.
- You only need solid Python basics to start ML.
Next up: Jupyter notebooks and Google Colab. Where ML code is actually written, and why it looks so different from normal programs.
Theia
Can you explain this concept?
Practise Why Python Became the Language of ML with realistic interview questions. Choose spoken or typed answers, get feedback on your reasoning, then return to the material that needs work.
Sign in to practise this conceptThe session stays focused on this lesson. Your selected interview level sets the bar.