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.’ You’ll find that Python books, code examples, and documentation sometimes contain references to this television show.
Table of contents
What is Python used for?
Python can be used in many situations. Its 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 written in Python! Dropbox, as far as I know, is largely written in Python as well.
You can use Python to automate tasks, perform calculations, create user interfaces, create website backends, access databases, download information from the Internet, 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.
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.
It is often said that Python comes with batteries included. It’s a fun way of stating that it 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.
What are Python’s major features?
It’s is 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") else: # 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.
Interpreted vs compiled
Python is Interpreted
Python code is interpreted on the fly by the Python interpreter when you run a program. This is true for CPython, which is the reference implementation of the Python programming language. Being an interpreted language means that the Python interpreter opens the file and starts reading it line by line, performing the appropriate 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 compiles 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 no interpreter is acting as an intermediary, compiled code is much faster.
Advantages of interpreted languages
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 ties itself to a specific platform, like Windows and Linux, and specific processor architecture, like Intel or ARM.
Some of these advantages can also be a disadvantage. As already mentioned, interpreted languages 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.
Between interpreted and compiled
An in-between option is a just-in-time compiler (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 is usable as a drop-in replacement in many cases. See the chapter on Python concurrency for more information on PyPy.
Another advantage of interpreted languages is that it opens the door to dynamic typing. 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
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 often and that you’ll find out soon enough during testing and just fix the error.
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.
What is Python’s popularity?