Python Basic: Exercise-2 with Solution
Write a Python program to get the Python version you are using.
A string containing the version number of the Python interpreter plus additional information on the build number and compiler used. This string is displayed when the interactive interpreter is started.
Version info:
A tuple containing the five components of the version number: major, minor, micro, releaselevel, and serial. All values except releaselevel are integers; the release level is 'alpha', 'beta', 'candidate', or 'final'. The version_info value corresponding to the Python version 2.0 is [2, 0, 0, 'final', 0]. The components can also be accessed by name, so sys.version_info[0] is equivalent to sys.version_info.major and so on.
Note : 'sys' module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter.
Sample Solution:
Python Code:
import sys
print["Python version"]
print [sys.version]
print["Version info."]
print [sys.version_info]
Sample Output:
Python version 3.5.2 [default, Sep 10 2016, 08:21:44] [GCC 5.4.0 20160609] Version info. sys.version_info[major=3, minor=5, micro=2, releaselevel='final', serial=0]
Check the Python version on command line:
Python Code:
[email protected]:~$ python --version
Python 2.7.17
[email protected]:~$ python -V
Python 2.7.17
[email protected]:~$ python3 --version
Python 3.6.9
[email protected]:~$ python3 -V
Python 3.6.9
Using platform module:
Python Code:
import platform
print[platform.python_version[]]
Python version as tuple [major, minor, patchlevel] of strings:
Python Code:
import platform
print[platform.python_version_tuple[]]
print[type[platform.python_version_tuple[]]]
Python Code Editor:
Have another way to solve this solution? Contribute your code [and comments] through Disqus.
Previous: Write a Python program to print the following string in a specific format [see the output].
Next: Write a Python program to display the current date and time.
Python: Tips of the Day
Union Of Collections:
To get a distinct combined set of two sets.
a = {1,2,3} b = {3,4,5} c = a.union[b]
In this article, we will learn the command to check the version of Python using Python script. Let's first have a quick look over what is a Python script.
Python is a well known high-level programming language. The Python script is basically a file containing code written in Python. The file
containing the python script has the extension ‘.py’
or can also have the extension ‘.pyw’
if it is being run on a windows machine. To run a python script, we need a python interpreter that needs to be downloaded and installed.
Check Python Version
Like much other software, Python too has several different versions released to date. After Python or any installation, one should check the version of the software that is running the script. Python uses version
keywords to check
the Python version. The python script is a simple code that can be run in different modes such as using a Python interpreter, on a command line, or using any Python editor. Let us look over the commands to check the Python version.
There are different versions of Python, but the two most popular ones are Python 2 and Python 3. When looking at the version number, there are usually three digits to read:
- the major version
- the minor version
- the micro version
Let us look over the commands to check the Python version.
Check Python Version in Linux
Most modern Linux distributions come with Python pre-installed. To check the version installed, open the Linux terminal window and enter the given command:
python ––version
Check Python Version in Windows
Most out-of-the-box Windows installations do not come with Python pre-installed. However, it is always a good practice to check the version. To check the version installed, open Windows Powershell or any Python editor, and enter the given command:
python ––version
Check Python Version in MacOS
If you are using a macOS, check the Python version by entering the given command in the terminal:
python –version
In all the three version checks, you will an output somewhat like Python 3.5.2. As discussed above 3 is the major version, 5 is the minor version and 2 is the micro version.
Use sys module to check Python version
import sys
print["Python version"]
print [sys.version]
print["Version info."]
print [sys.version_info]
Python version
3.5.2 [default, Sep 10 2016, 08:21:44]
[GCC 5.4.0 20160609]
Version info.
sys.version_info[major=3, minor=5, micro=2, releaselevel='final', serial=0]
Use platform module to check Python version
from platform import python_version
print[python_version[]]
3.5.2
Conclusion
In this article, we learned to use a simple command python --version
to check the version of Python on any Python script whether it is an editor, interpreter, or a command line. We saw the other two ways i.e.
sys.version
and python_version
to check the Python version.