Whether you’ve just decided to learn programming or you're a seasoned developer starting a new project, you need to pick the programming language you'll use.
This article makes a case for Python.
Python is a great programming language to learn and use, and here is why.
SyntaxPermalink
Today, most mainstream languages have C-style syntax (C, Java, JavaScript, C#, Go, etc.).
While this syntax is fine and familiar, it is pretty verbose and allows programmers to go wild with the code formatting.
Python took a different approach and is not using curly braces to delimit code blocks. Instead, it uses significant white spaces and code blocks are indented. We don't use a semicolon (;
) at the end of the line in Python.
Take this function written in C-style language, for example:
function my_function(p1, p2) {
if (p1 > p2) {
function_a();
}
else {
function_b();
}
}
We can also write it like this:
function my_function(p1, p2) {
if (p1 > p2) {function_a();}
else
{
function_b();
function_c();
}
}
And it will do the same thing.
We can write the same function in Python like this:
def my_function(p1, p2):
if p1 > p2:
function_a()
else:
function_b()
function_c()
But if we change it like the following:
def my_function(p1, p2):
if p1 > p2:
function_a()
else:
function_b()
function_c()
It is now a different function because it will always call function_c
, while before, it was called only as a part of the else
branch.
These style requirements impact the readability and maintainability of the code as programmers must style code well to make it run.
Python was designed to be a highly readable language which is important because code is written once but read many times.
Python is also using English words instead of punctuations for logical
operators. For AND operator, it uses the word and
instead of &&
, for OR, it uses the word or
instead of ||
, for NOT, it uses the word not
instead of !
.
In C style language:
if (x > 5 && y > 8 || z == 10) {
// Do the thing
}
In Python:
if x > 5 and y > 8 or z == 10:
# Do the thing
ConcisenessPermalink
Conciseness is partly related to syntax, which we've discussed above, but Python has excellent ways to do some things.
Python is a very concise language.
It allows us to do a lot with very little code. That is important because it makes code easy and fast to read and write.
We don't have to write a lot of code that is not directly related to what we're trying to achieve. It saves us time and mental energy.
Say we want to check that the variable number
is bigger than 5
and smaller than 10
. In C style languages, we would do this:
if (number > 5 && number < 10) {
// Do the thing
}
In Python, we have chained comparison, and we can do this instead:
if 5 < number < 10:
# Do the thing
It's a similar story when we want to get part of a list (array). In Python, we can use slicing:
numbers = [1, 2, 3 , 4, 5]
first_two = numbers[:2]
While in C style languages, we would do this:
let numbers = [1, 2, 3, 4, 5];
let first_two = numbers.slice(0, 2)
With the in
operator, we can check if an item is in the list or other data structure.
To check if a number is in the list, we would write:
numbers = [1, 2, 3, 4, 5]
if 5 in numbers:
# Do the thing
To check if a key is in the dictionary:
if 'my_key' in my_dict:
# Do the thing
We can also use it to check if a string contains another string:
if 'python' in 'Robust python':
# Do the thing
Comprehensions are another great feature of Python making our code super concise.
We can create a list, a set or a dictionary in one line. We can also map and filter with them. For example, to create a list of squares of numbers from one to ten in C style language, we would do the following:
let squares = [];
for (i = 1; i <= 10; i++) {
squares.push(i*i)
}
In Python, we can achieve the same with list comprehension:
squares = [i * i for i in range(1, 11)]
To get only squares bigger than 50, we could do this in Python:
big = [s for s in squares if s > 50]
This conciseness and clarity of Python make us very productive as developers while our code is still easily readable and clear to others.
Multi paradigmPermalink
In Python, we can choose which programming paradigm we want to use for our program.
You can write programs in the traditional imperative (procedural) programming style, where the computer executes a series of statements in order.
Python also supports an Object-oriented programming paradigm where we create hierarchies of classes with methods. It supports polymorphism, inheritance (including multiple inheritance) and other OOP concepts.
Last but not least, Python has excellent support for functional programming.
All functions in Python are first-class functions. They can be passed around and manipulated like any other object. This feature allows higher-order functions like map, filter and reduce. It has comprehensions and generator expressions. Many other functions to support functional programming are in itertools and functools modules.
VersatilePermalink
We can do pretty much anything in Python.
- simple scripts to automate some trivial tasks
- websites with millions of users
- heavy data processing backends
- APIs
- desktop apps
- software for IoT (Internet of Things, think RaspberryPi, Arduino etc.)
- data visualisation
- ML
- and so much more
That is why many major companies (Amazon, Google, Netflix, Instagram, Dropbox, Spotify, NASA and so on) and millions of small companies and independent developers worldwide use Python.
Runs almost everywherePermalink
Python runs on most of the relevant platforms today. It runs on x86, x86_64 and ARM. It also runs on all major operating systems of today - Linux, Windows, macOS and even FreeBSD.
We can write our code anywhere, and we can run it anywhere.
Community and librariesPermalink
Python is an open-source, independent and community-driven language. It has a vast community of developers and enthusiasts. They're very supportive and friendly, which benefits both new and expert developers.
Community is dedicated to maintaining and spreading the language, so there are hundreds of thousands of libraries to help us with whatever we need to do.
Frameworks like Django and Flask (for web development), Numpy, Panda, NLTK, PyTorch (for ML and data science) and thousands of libraries for almost anything we can think of are free to use and open-source.
Thousands of Python developers are willing to help us if we ask.
Machine Learning and Data sciencePermalink
Machine learning and data science have been rising over the last few years and will continue to grow in the coming years.
We can use any language for ML and data-science related tasks, but Python has become a preferred language for this domain. Most companies and most of the tutorials and courses use Python as the language of choice for the ML domain.
It's thanks to Python's strengths which we mentioned already above:
- syntax and conciseness - which makes developers very productive and allows even beginners to write good code
- exhaustive libraries which give us solutions to all existing problems
- community build around the whole ML domain
So, for anyone interested in Machine learning, Python is the language that will help them succeed.
Gradual typingPermalink
Python is a dynamic language.
It has its advantages and disadvantages, which I don't want to discuss here in detail, but it means we can write code without worrying about types and the compiler checking them.
It's great if we need to write a little script to automate something, or we want to build a prototype of the idea, or we're working on a smaller project with few people.
However, for bigger projects with more developers, types are beneficial as they help us write more robust code, and we can catch more errors before we run the code.
It would look like this:
from typing import List
def function_a(a: int, b: List[str]):
pass
In the above function, parameter a
has to be of type int
, and parameter b
must be a list of strings - List[str]
.
The beauty of Python is that we can have both. We can write our code without types, but we can also add type annotations if we want to.
Then we can use a static type checker like mypy to do the type checking to help us catch any possible errors. We can choose to use types from the beginning of the project (or after we've built MVP), or we can start our project without types and introduce them much later when our codebase and number of developers grow.
CareerPermalink
Python has been around for over 30 years, and over time it's only growing in popularity. Startups, small and medium companies, and big enterprises alike are using it to create their software.
Its popularity creates a constant demand for Python programmers all over the world.
And Python jobs come with high salaries.
Python will serve you well, whether you want to build a career as an employee, or you want to be an independent developer or build your own business.
You might also like
Join the newsletter
Subscribe to learn how to become a Remote $100k+ developer. Programming, career & Python tips.