Python is one of the hottest programming languages right now, with an ever-growing community of developers worldwide. Python is especially popular in prototyping, machine learning, data science, and it’s even picking up pace in the web development and embedded systems communities. All thanks to its excellent accessibility and a vast amount of libraries that are almost always plug-n-play.
In this article, I want to tell the most important reasons you should learn Python as a hobbyist, student, or seasoned programmer.
What is Python?
Python is an interpreter-based programming language initially created by Guido van Rossum in the late 1980s when he worked at the Centrum Wiskunde & Informatica (CWI) research institute in the Netherlands. The initial idea of van Rossum was to create a programming language for the distributed operating system Amoeba. After a year of development, van Rossum and his colleagues decided that Python might not only be useful for Amoeba. They released it under the MIT open source license, which allowed other developers to use and extend the programming language. And especially in the last couple of years, Python became very popular.
The core idea of Python written down in The Zen of Python (PEP20) which includes the following statements:
- Beautiful is better than ugly.
- Explicit is better than implicit.
- Simple is better than complex.
- Complex is better than complicated.
- Readability counts.
You can see the complete list of statements when you enter the following into a Python script.
1
import this
Another big goal of the Python community is to keep the language fun, which is also reflected in the name derived from the British comedy group Monty Python.
Python is easy to learn
One crucial part of why you should learn Python is that it is easy to learn. When you already got a programming background, e.g. in Java, C++, C#, or comparable languages learning Python won’t take you longer than a week. And even if you haven’t used a programming language before, Python is a perfect start. This is because Python’s syntax, the code you write, is very readable and does not include a lot of confusing pieces like ->
, #
or ::
. Together with an easy syntax comes a clear semantic, which means what the code does that you have written. And if you write your code in a certain way, it can almost be understood as English when read aloud.
Another critical factor why Python is so easy to learn is that it does not have to be compiled into an executable like C++ or Go. You sometimes have to enter several complex command-line command to see your program in action when using those languages. Python is an interpreter-based language which means that another program, the Python interpreter, is responsible for executing your code. Together with being an interpreter-based language comes Python’s high portability. You can send your Python code to a friend or co-worker, and it will run on their system when they get a similar version of the Python interpreter installed. Sending code of a compiled language to someone else involves a lot more work because compiling a program under Windows can be very different from compiling something under macOS.
What is Python used for?
The list of fields that use Python is long and still growing and makes another vital point why you should learn Python. The most popular use cases are prototyping, testing, automation, web development, data science, machine learning, astronomy, and even embedded systems.
Data Science and Visualization
First and foremost is data science and visualization. For example, the package pandas
allows you to analyze and manipulate data quickly. It can read files from a wide variety of structured data formats like CSV, XML, Excel, HDF5, and many more, which are commonly used when storing data from scientific measurements and observations. And if you want to get an overview of a data set, pandas
gives you several methods for that.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import pandas as
# download the famous IRIS dataset as pandas dataframe
df = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')
# show the first five elements
df.head()
# sepal_length sepal_width petal_length petal_width species
# 0 5.1 3.5 1.4 0.2 setosa
# 1 4.9 3.0 1.4 0.2 setosa
# 2 4.7 3.2 1.3 0.2 setosa
# 3 4.6 3.1 1.5 0.2 setosa
# 4 5.0 3.6 1.4 0.2 setosa
# show important data set properties
df.describe()
# sepal_length sepal_width petal_length petal_width
# count 150.000000 150.000000 150.000000 150.000000
# mean 5.843333 3.057333 3.758000 1.199333
# std 0.828066 0.435866 1.765298 0.762238
# min 4.300000 2.000000 1.000000 0.100000
# 25% 5.100000 2.800000 1.600000 0.300000
# 50% 5.800000 3.000000 4.350000 1.300000
# 75% 6.400000 3.300000 5.100000 1.800000
# max 7.900000 4.400000 6.900000 2.500000
The package numpy
allows you to efficiently work with multi-dimensional arrays and operations on those arrays to do matrix manipulations. It also provides you with many mathematical and logical functions to carry out scientific calculations efficiently.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import numpy as np
# define 2x3 matirx A
A = np.array([[2, 0, 2],
[0, 1, 0]])
# define 3x2 matrix B
B = np.array([[3, 2],
[1, 0],
[2, 3]]
# multiply A and B
np.matmul(A, B)
# [[10 10]
# [ 1 0]]
And if you want to visualize your data the packages matplotlib
and seaborn
give you the tools to create professional plots and diagrams for presentation and publications. seaborn
itself is based on matplotlib
which shows that the interoperability of different Python packages is one key aspect why it is so popular.
1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0.0, 5.0)
y = np.cos(2 * np.pi * x) * np.exp(-x)
fig, ax = plt.subplots()
ax.plot(x, y, 'o-')
ax.set_ylabel('Damped oscillation')
plt.show()
In short, you can use pandas
for handling your data, use numpy
to implement complex operations on this data, and present your results with matplotlib
/seaborn
. The customizability and flexibility is endless when using Python for data science compared to other tools like R, SPSS or Excel.
Machine Learning
Closely related and influenced by data science is the field of machine learning. Turning the vast amount of data available to us today into knowledge to guide decision-making requires algorithms that adapt themselves to an ever-changing world. Machine learning provides statistics and linear algebra methods to make sense of the endless stream of data produced. Over the last decade, machine learning has become an essential tool for academia and businesses. Python packages and APIs are at the forefront of this development, making it incredibly easy to build, validate, and deploy custom machine learning models.
For classic tasks like regression and classification, the package scikit-learn
(which is built on numpy
, scipy
and matplotlib
) offers a great tool set. Making a simple linear regression model only requires a handful of lines of code:
1
2
3
4
5
6
7
8
9
from sklearn import linear_model
model = linear_model.LinearRegression()
model.fit([[0, -0.5], [1, 1.2], [2, 1.9], [3, 3.1], [4, 3.8]], [0, 1, 2, 3, 4])
model.intercept_
# 8.881784197001252e-16
model.coef_
# [ 1.00000000e+00 -6.97597723e-16]
If you want to classify high-dimensional data like images, sounds, or long and complex time series, deep neural networks are the state-of-the-art tool today. When it comes to describing and training neural networks, frameworks like Tensorflow and PyTorch are the most popular in the Python world. Both of those frameworks allow you to easily describe neural network architectures with different preprocessing steps and layers. With backends to high-performance languages CUDA and OpenCL for training deep neural networks, they are straightforward to use without the need to write your own GPU code. Take, for example, this PyTorch code (Source: github.com/ChawDoe/LeNet5-MNIST-PyTorch) that describes the famous LeNet-5 to recognize handwritten digits.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from torch.nn import Module
from torch import nn
class Model(Module):
def __init__(self):
super(Model, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5)
self.relu1 = nn.ReLU()
self.pool1 = nn.MaxPool2d(2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.relu2 = nn.ReLU()
self.pool2 = nn.MaxPool2d(2)
self.fc1 = nn.Linear(256, 120)
self.relu3 = nn.ReLU()
self.fc2 = nn.Linear(120, 84)
self.relu4 = nn.ReLU()
self.fc3 = nn.Linear(84, 10)
self.relu5 = nn.ReLU()
def forward(self, x):
y = self.conv1(x)
y = self.relu1(y)
y = self.pool1(y)
y = self.conv2(y)
y = self.relu2(y)
y = self.pool2(y)
y = y.view(y.shape[0], -1)
y = self.fc1(y)
y = self.relu3(y)
y = self.fc2(y)
y = self.relu4(y)
y = self.fc3(y)
y = self.relu5(y)
return y
LeNet-5 Architecture take from the original publication: Y. LeCunn, L. Button, Y. Bengio, P. Haffner, “Gradient-Based Learning Applied to Document Recognition” in Proceedings of the IEEE, Volume: 86, Issue: 11, November 1998.
After a neural network is trained, you usually want to deploy it. For deploying it on servers with powerful GPUs and CPUs, one can stick to Tensorflow or PyTorch. However, devices like smartphones, intelligent sensors, and cars can not be equipped with power-hungry GPUs that demand up to 600W. They have to operate on a minimal power budget due to running on batteries most of the time. Due to this, those devices often employ custom hardware architectures for executing deep neural networks. However, it is not an easy task to map a deep neural network onto custom hardware. The Python project tvm
(Tensor Virtual Machine) provides a framework for the compilation and optimization of neural networks and helps you to deploy your model onto custom accelerators.
To sum it up, with Python you can build complex machine learning models, train them efficiently, and even deploy them onto different devices. You don’t have to learn other languages or set up multiple development environments.
Web Development
Having a website or web service these days is a must to bring your product to the customers. For a long time, web development backends were dominated by PHP and ASP.NET. But Python is becoming more and more popular in the web development world. Setting up a bare-bones HTTP server with Python only requires a couple of lines of code using the built-in module SimpleHTTPServer
:
1
2
3
4
5
6
7
8
9
10
import http.server
import socketserver
PORT = 8000
handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", PORT), handler) as httpd:
print("Server started on localhost:" + str(PORT))
httpd.serve_forever()
This starts an HTTP server on port 8080 that lists the directory it was launched in. You can use this snipped to build your API with GET and POST calls or take it one step further and use django
to build a fully-featured web application with many useful presets that allow you to focus on the high-level implementation. Or you can use flask
to make your web application very small and fit into a single Python file:
1
2
3
4
5
6
7
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
And even building Web3 blockchain-based applications are possible with Python using web3
. So if you are already familiar with Python and want to develop a web application, you don’t have to switch gears and can incorporate your favorite packages into it.
Embedded Systems
Coming from an embedded systems background myself, I was skeptical of Python for a long time because I was used to working with C/C++, which gives you complete control and all the problems of memory management. But Python even made it to embedded platforms. The Raspberry Pi gave Python a huge boost in popularity, not only on embedded systems. Due to the affordability and accessibility of the Raspberry Pi and an extensive library of example programs, many people started to learn Python as their first programming language. However, Python on the Raspberry Pi still runs under Linux. But projects like MicroPython and CircuitPython allow you to run Python on bare-metal devices without an operating system. Python enables you to quickly build an embedded application for your needs if you want to read some button presses and display data on display through I2C or SPI busses without strict timing or power requirements. For example, letting an LED flash every second on a Raspberry Pi 2040 can be done with only a few lines of code:
1
2
3
4
5
6
7
8
9
10
import machine
import utime
led = machine.Pin(25, machine.Pin.OUT)
while True:
led.value(1)
utime.sleep(1.0)
led.value(0)
utime.sleep(1.0)
Testing and Prototyping
With this rich environment of different frameworks and packages that can be integrated into one application Python is the perfect language for testing and rapid prototyping. If you want to test out an idea for a web application that does advanced data analysis, you can combine flask
with scipy
and build a prototype within several days. And together with Python’s portability, such a prototype can be given to others to play around with it. Especially in agile software development, where short turnaround times and prototypes that customers can interact with are crucial Python is the perfect language for building such applications.
Who uses Python?
According to the 2020 HackerRank Developer Skills Report Python is the second most popular language among hiring managers looking for candidates and is only slightly behind JavaScript. And in Stackoverflow’s 2020 Developer Survey Python is in first place for the most wanted language by developers.
Python has been adopted by many big companies such as Instagram, which uses django
as their web framework. Instagram’s developer team has a big focus on simplicity and practicality, which Python can deliver. And they even praise their deployed Python web service for its efficiency and scalability.
Another big player is the music streaming service Spotify, which uses Python on their backend mainly for data analysis and recommendation systems which are composed out of many different services which communicate through their own messaging protocol.
Another well-known streaming service is Netflix, which heavily relies on Python on its backend. One of the most prominent examples of Netflix’s use of Python is Chaos Gorilla. This service constantly simulates losses of critical infrastructure to test and improve the reliability of their services.
Last but not least is Dropbox which even went that as far as hiring the creator of Python, Guido van Rossum, from 2013 till 2019. Because Dropbox uses Python for almost everything. From their web frontend to the backend, everything is based on Python.
The list of Python success stories is very long and proves that it is not just scripting or prototyping language for beginners. Some of the biggest tech companies run their business with the help of Python. And a big reason for this is Python’s simplicity and the rich open source community that provide modules, packages, and frameworks for almost everything you need. The 2020 GitHub Octoverse community report shows Python in second place among the top languages used in GitHub projects. To sum it up, you probably interact with services build with Python daily.
Python developers are in high demand
Python ranks among the most sought-after programming languages in 2021 due to its broad adoption among leading tech companies and its large community. An entry-level Python developer earns an average salary of $65,000 per year in the US, according to Daxx whereas Python experts can easily make up to six figures annually.
And those are the reasons why you should learn Python. It is easy to understand, comes with an insurmountable amount of third-party packages for almost every use case; the code is platform-independent and can be executed on virtually any system. Python is relevant and growing in nearly every sector, and even as an entry-level developer, you can earn a decent salary.
So now is the best time to start learning Python to kick start a new career or take a new direction in your current one. I’m launching a Computer Science and Python introduction course here on my website and on my Youtube channel soon, so make sure to subscribe and join our lovely Discord community of programming and computer science enthusiasts.