How to Check Python Version?
Before learning how to check the python version, we should first understand Python and its versions briefly.
Python is an object-oriented, high-level interpreted scripting language.
Let us discuss some of the various important points regarding the Python programming language :
- Python programming language is a highly readable language making it easy to understand for new coders.
- ABC programming language can be considered the predecessor of the Python programming language. Modula-3 also has some influence on the Python programming language.
- The Python programming language was developed by Guido Van Rossum in the Netherlands in 1991.
- Python language is widely used in various technical fields such as Artificial Intelligence (AI), Machine Learning (ML), Scientific Calculation, Desktop Application, Web Development (using Django, and Flask framework, etc.) Mobile Application Development, etc.
Let us learn how to check the python version using the command line and scripts.
Check Python Version: Command-Line
Let us first learn how to check the python version using the various command-line commands used in different operating systems.
i) Windows:
In Windows operating systems, we can use the command python --version to check the python version.
The steps to check the python version in Windows are very simple:
- Open the Windows command prompt or Windows Powershell, and enter the following command on the shell :
If we have a Python interpreter installed on the Windows operating system then the command will show the version like :
Output :
Note :
- The version number may differ, but if the Python interpreter is installed in a system, it will similarly show the version.
- We can also search for Python present on the system. We can press the Windows key to launch the search menu and then we can type Python. If the system has installed Python, it will show the Python version.|
ii) MacOS :
In macOS operating systems, we can use the same command, python --version, to check the python version.
The steps to check the python version in macOS are very simple:
- Open the macOS Terminal, and enter the following command on the shell or terminal :
If we have a Python interpreter installed on the macOS operating system, then the command will show the version like :
Output :
iii) Linux :
In Linux operating systems, we can use the same command, python --version to check the python version.
The steps to check the python version in Linux are very simple:
- Open the Linux terminal or shell and type the following command :
If we have a Python interpreter installed on the Linux operating system, then the command will show the version like :
Output :
Note:
We can use the commands such as python --version or python -v or python -VV. All of these commands will provide a similar result. The -VV option is a new command added since Python 3.6. Which can be used for detailed information than -V or --version.
Check Python Version: Script
Let us now learn how to check the python version using scripts. We can use the sys module or the platform module of the standard library to check the python version in various operating systems. The script will be the same for all operating systems such as Windows, Mac, Linux, etc.
The script is very useful for checking the python version even tho we have various versions of Python installed on our system. Let us see the script and its related information.
i) Various information string: sys.version :
We can use the .version method in the sys (system) module to print the version of the Python interpreter installed on the system. The sys. version method returns a string that contains the version number of the Python interpreter installed in the system. It also returns additional information about the build number and the compiler used.
let us now look at the script :
Output :
ii) Tuple of version numbers: sys.version_info :
We can also use the .version_info method in the sys (system) module to print the version of the Python interpreter installed on the system. The sys.version_info method returns a tuple containing five components of the Python interpreter version number: the major version release number, the minor version release number, the micro version release number, the release level, and the serial number.
let us now look at the script :
Output :
Note :
The release level element is a string, and the other output elements are integers.
iii) Version number string: platform.python_version() :
We can also use the .python_version() method present in the platform module to print the version of the Python interpreter installed on the system. The platform.python_version() method returns a string in the form major.minor.patchlevel. The string denotes the major release number then the minor release number, and then the patch level, each separated using a dot(.)
let us now look at the script:
Output :
The script is handy when we want to check the Python interpreter version number as a simple string only.
iv) Tuple of version number strings: platform.python_version_tuple() :
We can also use the .python_version_tuple() method present in the platform module to print the version of the Python interpreter installed on the system. The platform.python_version_tuple() method returns a tuple in the form 'major', 'minor', 'patchlevel'. The tuple denotes the major release number, then the minor release number, and then the patch level, each separated using a comma(,). Each element of the tuple is in the form of a string.
let us now look at the script:
Output :
What are the Different Python Versions?
We have already discussed what Python programming language is and how to check the python version, let us briefly discuss the various popular versions of the Python programming language.
- The first version of Python was released in 1994 named Python 1.0. Python 1.0 had various features like lambda function, map, filter, reduce, etc.
- The second version of Python was released in 2000, with many major new features such as garbage collection systems, list comprehensions, etc.
- The third and latest major Python version release was released in 2008. This version of Python (Python 3.0) is also known as Py3K.
Refer to the below-specified table to check the various Python interpreter versions and their release dates.
Python Versions | Release Date |
---|---|
Python 1.0 | January 1994 |
Python 1.5 | December 31, 1997 |
Python 1.6 | September 5, 2000 |
Python 2.0 | October 16, 2000 |
Python 2.1 | April 17, 2001 |
Python 2.2 | December 21, 2001 |
Python 2.3 | July 29, 2003 |
Python 2.4 | November 30, 2004 |
Python 2.5 | September 19, 2006 |
Python 2.6 | October 1, 2008 |
Python 2.7 | July 3, 2010 |
Python 3.0 | December 3, 2008 |
Python 3.1 | June 27, 2009 |
Python 3.2 | February 20, 2011 |
Python 3.3 | September 29, 2012 |
Python 3.4 | March 16, 2014 |
Python 3.5 | September 13, 2015 |
Python 3.6 | December 23, 2016 |
Python 3.7 | June 27, 2018 |
Python 3.8 | October 14, 2019 |
How to Upgrade to a Newer Version?
We can easily upgrade to the latest or newer version of the Python interpreter by simply going to the (visiting) Python downloads page and downloading the latest version.
What if Your Computer has Multiple Python Versions Installed?
We can have multiple Python interpreter versions installed on our system. We can run a particular python program (or python script) using the specified Python interpreter version.
For example, if we have Python 2.6 and Python 3.6 installed on our system, then we can run a file (let's say program.py) using both interpreters.
Example : (For Python 3.6)
Example : (For Python 2.6)
Ready to take your Python skills to new heights? Enroll in our free Python course with certificate and become a certified Python pro! Click here to start your journey.
Python 2 vs Python 3
Both Python 2 and Python 3 are very popular interpreters, let us discuss some of the major differences between both releases.
Python 2 | Python 3 |
---|---|
Python 2 was released in 2000. | Python 3 was released in 2008. |
Python 2 interpreter considers print as a statement but not a function. | Python 3 interpreter considers print() as a function. |
The strings are by default stored as UNICODE characters in Python 2. | The strings are by default stored as ASCII characters in Python 2. |
The division of two integral values in Python 2 results in another integral value. For example, 7/2 yields 3. | The division of two integral values in Python 3 results in a floating-point value. For example, 7/2 yields 3.5 |
Exception in Python 2 was dealt with in the enclosed notations. | Exceptions in Python 3 are enclosed in parentheses. |
We used the xrange() function for iteration in Python 2. | We use the range() function for iteration in Python 3. |
Python 2 is not longer in use since the year 2020. | Python 3 is more popular than Python 3 but in some places, we still use Python 2. |
With some effort, we can port the Python 2 code to Python 3. | Python 3 has no backward compatibility with the previous version i.e. Python 2.} |
Learn More
To learn more about the Python Programming Language, refer to the articles :
Conclusion
- In Windows operating systems, we can use the command python --version to check the python version. We just need to open the Windows command prompt, or Windows Powershell, and enter the command.
- In macOS operating systems and Linux operating systems, we can use the same command python --version to check the python version.
- We can use the sys. version method present in the sys (system) module to print the version of the Python interpreter installed on the system.
- The sys. version method returns a string that contains the version number of the Python interpreter installed in the system.
- The sys.version_info method returns a tuple that contains a major version number, the minor number, the micro version number, the release level, and the serial number.
- The platform.python_version() method returns a string in the form major.minor.patchlevel.
- We can also use the .python_version_tuple(). The platform.python_version_tuple() method returns a tuple in the form 'major', 'minor', 'patchlevel'.
- The tuple denotes the major release number, the minor release number, and then the patch level separated using a comma(,). Each element of the tuple is in the form of a string.
- We can have multiple Python interpreter versions installed on our system. We can run a particular python program (or python script) using the specified Python interpreter version.