Many programming languages come and go. A few like C, C++, C#, and Java, stick around for quite a while. One programming language that hit the scene in the late 1980s/early 1990s has been near the top of the charts for programming languages used but has tended to remain below the radar for most. In the last few months, however, that language has been getting more interest. In fact, in November 2020, Python landed in the number 2 spot on Tiobe’s Index for programming languages, ahead of languages like Java, C#, C++ and Visual Basic. To be fair, it dropped back to third place in December, as Java barely edged ahead of Python again.
So Why Python?
There are many reasons for Python’s popularity. These include allowing for multiple programming paradigms. It can be used in both object-oriented and structured-programming models. It also has the ability to support functional, aspect-oriented, logic, and other programming models. Part of the reason for this support of a wide range of approaches is that Python was built to be highly extensible while also striving to be compact.
Python was also created to be fast. This includes allowing elements to be removed if not needed in order to speed up the final programming results.
Python works on multiple platforms, including Mac OS X, Windows, Linux, and Unix. Builds have been created for mobile platforms as well, including Android and iOS. It is free to use and can be extended under open-source licensing.
As a programming language, it supports a variety of core features. The language includes the standard basic data types ranging from numbers to strings but also includes items such as lists and dictionaries. Data types can be strongly and dynamically type, plus the language will flag with an exception when you try to mix types. As an object-oriented language, it supports classes, multiple inheritance, and more. The language includes exception handling and supports automatic memory management.
Like most programming languages, before using Python you need to have Python on your system. Also like Java, you might already have the necessary tools on your computer. To check if you have a Python interpreter on your system, simply type python into a command window and see what results you get. If you have a copy on your system, you will be shown the version number similar to what is in the following figure:
The current version should start with 3. As you can see in the figure, my system has a 2.7 version installed. There is a chance that a 3.x version could also be installed. You can check by typing python3 on the command line. If it is not installed, then if you are in Windows, you should be taken to the Windows Store to install it. You can also go to the download page at http://www.python.org/downloads/ to get the latest version (3.9.1 as of the time of this article). Once downloaded and installed, you can type python3 again to confirm you have indeed installed it as shown in the following figure.
Python Hello World
Once you have Python installed, you can see it in action. Python is also an interpretive language. This means you can start the interpreter – which you did if you typed python or python3 earlier – and begin entering Python commands that will execute immediately. Once you’ve started the interpreter, you can simply type in code. For example, you can type in the following line of code:
Once you press enter (or return) after this, the line of code will execute, and you’ll see the following message displayed:
Note that printhad a lowercase p. If you typed an uppercase P, you would get an error similar to the following:
>>> Print("Hello world!") Traceback (most recent call last): File "", line 1, in NameError: name 'Print' is not defined
Of course, you can do a bit more on the command line as well. For example, you could enter the following code:
this_is_true = True if this_is_true: print(“It was true!”)
This code creates a variable and uses an if condition to determine if something should be printed as shown in the following figure:
Note that a tab was entered before the line containing the call to print. The tab helps to indicate that the code block continues. You can see that the variable called this_is_true was created and set equal to True. It is then used in the if statement. The if statement evaluates to true and results in the message being displayed. If you enter this code again but change it to set this_is_true to False, then when the if statement executes, you’ll see that nothing is displayed.
This was a pretty basic example of using the command line interpreter to execute Python code. To get out of the interpreter on Windows, you can press Ctrl+Z then the enter key.
For More Information on Python
Of course, you can do a lot more with Python. This article was simply to mention why Python was worth taking a few minutes to review.
The bottom line is that Python has been around a long time. In recent weeks and months, it has been getting a lot more attention. As a developer, if you’ve not taken a look at Python, now might be a really good time to give it some attention. A good place to start is with articles here on Developer.com. You can also find information at https://www.python.org/.