These are some of the major features of the Python programming language.
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, we need to indent all code blocks 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 curly braces to group code blocks like Java, C, and C#. This fact alone removes a lot of clutter and symbols from your code. Although it’s a subjective matter, people generally agree that it makes Python easier on the eyes.
Many languages require a manual compilation step before you can run your program, while other languages are so-called interpreted languages: they can be run directly by the interpreter. Python is actually somewhere in between these two worlds.
When you create a Python program, you can run it directly without a manual compilation step. This is the case with all interpreted languages, and that’s why most people will tell you it is an interpreted language. However, internally Python compiles your code into lower-level code, called bytecode. This code is not specific to a system architecture (like Intel vs ARM), but it is faster to read and parse than plain text files.
In some situations, this bytecode is even cached on disk in files ending with the *.pyc extension. But when used as a scripting language, Python won’t cache the bytecode. To us, it doesn’t matter. We can just run our code directly and don’t have to worry about bytecode, since Python handles it all automatically. This has several advantages:
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.
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 String
, int
, and float
. It gets even uglier when objects are involved.
Now let’s look at Python variables. 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 get started quickly. Some will tell you that it’s more error-prone. A strongly typed language like Java won’t compile when there’s a type error. Python will probably continue running, but the output will be unexpected. It is my experience that it doesn’t happen that often. In addition, you’ll find out soon enough during testing and fix the error before the software ever goes to production.
I’m not arguing that typing is a bad thing, though. In fact, Python supports type hints since Python 3.5. It’s an optional feature, but many programmers embrace it since it has quite a few advantages, like better auto-completion in your Python IDE. I love typing because it takes away the guessing. Explicit typing is a form of documentation and I use it where appropriate in my own day-to-day work.
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 automatically cleans up unused variables!
I’m not going into the nitty-gritty details here, but you can rest assured Python will do a perfect job, and it will never accidentally clean up a variable that you still need.