What is Python?

Python is a computer programming language, a vocabulary and set of grammatical rules for instructing a computer to perform tasks. It is named after the BBC television show 'Monty Python's Flying Circus'. 1

You can use Python to automate tasks, perform calculations, create user interfaces, create website backends, access databases, download information from the Internet, etc. etc. It's a versatile language that is easy to learn and write. It's perfect for beginning programmers, but is just as useful and powerful for seasoned professionals.

It is often said that Python comes with batteries included. It's a fun way of saying that Python includes a comprehensive base library. This is augmented with hundreds of thousands of external packages. You'll find supporting base libraries and packages for pretty much anything you want to accomplish with Python.

Let's take a look at some of Python's major features!

Easy to read and write

One of Python's most notable features is the way it enforces the use of indentation for readability. Without proper indentation, your code won't even run. In Python, all code blocks need to be indented to the same level. Here's an example of this at work. If you don't understand the code yet, don't worry:

def bigger_than_five(x):
  # The contents of a function are indented
  if x > 5:
    # This is another, even more indented block of code
    print("X is bigger than five")
    # And one more!
    print("x is 5 or smaller")

Because indentation is required, the Python language does not need accolades to group blocks of code like Java, C, and C#. Although it's a subjective matter, people generally agree that it makes Python easier on the eyes.

General purpose

Python can be used in many situations. It's rich base library makes it excellent for all kinds of little helper scripts. But it scales just as well to large systems. YouTube was originally writting in Python! Dropbox, as far as I know, is largely writting in Python as well.

Another quickly growing field of expertise is called data science. Many data scientists use Python for their day to day work. And these are just a few examples. If you start looking closely, Python is very ubiquitous.

Interpreted versus compiled

Python code is interpreted on the fly by the Python interpreter when you run a program.2 This means the Python interpreter opens the file and starts reading it line by line, performing the appropiate actions for each statement. For example, when the Python interpreter encounters print("Hello") in your code, it will take the provided string and feed it to some internal function that will print the text to your screen.

This is a totally different approach than compiled languages like C. C code gets compiled into low level machine code, which can be run directly by the computer's processor. This is also called ahead-of-time compilation (AOT). Because there's no interpreter acting as an intermediary, compiled code is much faster.

An in-between option is just-in-time compilation (JIT). With JIT, your code is compiled while running the program. JIT tries to combine the speed advantage of ahead-of-time compilation with the flexibility of interpretation. A big advantage of JIT is that the JIT compiler can keep optimizing your code, even while it is running. The longer your code runs, the more optimized it will become.

Java and the .NET framework are notable examples of JIT compilation, but there's also a JIT based Python implementation called PyPy. This project has come a long way, and can in many cases be used as a drop-in replacement. See the chapter on Python concurrency for more information on PyPy.

An interpreted language has several advantages compared to ahead-of-time compilation:

  • You can write your code in a text editor and execute it directly. No additional steps like compilation and linking are necessary.

  • Because it's plain text, you can simply open a program and inspect its contents. In contrast, compiled code is not human readable.

  • It is platform independent. As long as the platform has a Python interpreter, your code will work. Compiled code is tied to a specific platform, like Windows and Linux, and a specific processor architecture, like Intel or ARM.

Some of these advantages can also be a disadvantage. As already mentioned, interpreted lanuages are not high performance languages. Also, the fact that the source code is easy to read and modify is not an advantage to vendors that want to protect their copyright.

In short, every language has its pros and cons and specific niches --- Python has proven to be very usable in many situations.

Dynamically typed

Another advantage to being an interpreted language, is that it opens the door to be dynamically typed. So what does that mean? I'll demonstrate it with some simple code.

Here are a few variable declarations in Java:

String myName = "Erik";
int myAge = 37;
float mySalary = 1250.70;

In a strongly typed language, you need to specify the exact type of each variable, like String, int, and float. It gets even uglier when objects are involved.

In Python, we can do exactly the same without types:

my_name = "Erik"
my_age = 37
my_salary = 1250.70

As you can see, the Python variant is a lot cleaner and easier on the eyes!

When running this code, Python dynamically finds out the type of our variables. Say, for example, I'd like to know my yearly income by multiplying my salary by 12. I'd need to do the following:

my_income = my_salary * 12

Python will look at my_salary, see that it is a floating point value, and perform the math. If my_salary would have been a string, Python wouldn't complain though. It would detect it's a string and just create a new one, consisting of 12 repetitions of that string! Java, however, would fail with an error in such cases.

Dynamic typing has many advantages. In general, it makes it easier to quickly get started. Some will tell you that it's more error prone. A strongly typed language like Java won't compile when a type error is made. Python will probably continue running, but the output will be unexpected. It is my experience that it doesn't happen that much, and that you'll find out soon enough during testing and just fix the error.

Garbage collection

Python has the concept of variables. A variable allows you to store any value like a number, a string of text or even bigger objects.

Each variable you declare takes up space in your computer's memory. This can add up quickly, especially when you create programs that run for a long time. So you need a way to clean up variables that you don't use anymore.

In some languages, you need to perform this cleanup explicitly. This is prone to a type of error called a memory leak. If you make a little mistake and forget to clean up, your software will slowly eat up available memory. Lucky for us, Python's garbage collector takes care of cleaning up unused variables automatically!

I'm not going into the nitty-gritty details here, but you can rest assured Python will do a very good job and it will never accidentally clean up a variable that you still need.

Python is wildly popular

On a well known list 3 of popular programming languages, Python has a shared second place with Java since januari 2020. Javascript, due to it's stronghold as a web development language, is still number one though. This popularity is a great advantage. There are vast amounts of resources, sample code, and help available. Python is here to stay --- learning it is a safe bet!

  1. You'll find that Python books, code examples, and documentation sometimes contain references to this television show. 

  2. This is true for CPython, which is the reference implementation of the Python programming language. 

  3. https://redmonk.com/sogrady/2020/02/28/language-rankings-1-20/ 

If you liked this page, please share it with a fellow learner: