w3resource

Python: Get the Python version

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.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Unpack variables from iterable:

# One can unpack all iterables (tuples, list etc)
>>> a, b, c = 1, 2, 3
>>> a, b, c
(1, 2, 3)

>>> a, b, c = [1, 2, 3]
>>> a, b, c
(1, 2, 3)