w3resource

Check Python version

Python Basic: Exercise-2 with Solution

Write a Python program to find out what version of Python 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  # Import the sys module to access system-specific parameters and functions

# Print the Python version to the console
print("Python version")

# Use the sys.version attribute to get the Python version and print it
print(sys.version)

# Print information about the Python version
print("Version info.")

# Use the sys.version_info attribute to get detailed version information and print it
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)   

Explanation:

The said code imports the "sys" module and then uses it to print the version of Python currently being used, as well as additional version information.

  1. The first line imports the "sys" module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It is always available.
  2. The second line prints the string "Python version".
  3. The third line prints the version of Python currently being used to the console.
  4. The fourth line prints the string "Version info.".
  5. The fifth line prints version information of the current Python interpreter. It returns a named tuple (version_info) containing the five components of the version number: major, minor, micro, releaselevel, and serial.

Check the Python version on command line:

Python Code:

user@machine1:~$ python --version
Python 2.7.17
user@machine1:~$ python -V
Python 2.7.17
user@machine1:~$ python3 --version
Python 3.6.9
user@machine1:~$ python3 -V
Python 3.6.9 

Using platform module:

Python Code:

import platform
print(platform.python_version())

Explanation:

The said code imports the platform module and then uses it to print the version of Python currently being used.

  1. The first line import platform imports the platform module, which provides various services that interact with the host platform.
  2. The second line print(platform.python_version()) uses the python_version() function from the platform module to get the version of Python currently being used and prints it to the console.

This is a simpler and more efficient way to get the version of python that you are running, as it does not need to import any other modules like sys.

Output:

3.10.6

Python version as tuple (major, minor, patchlevel) of strings:

Python Code:

import platform
print(platform.python_version_tuple())

Output:

('3', '10', '6')

Explanation:

The said code imports the platform module and then uses it to print the version of Python currently being used as a tuple.

  1. The first line import platform imports the platform module, which provides various services that interact with the host platform.
  2. The second line print(platform.python_version_tuple()) uses the python_version_tuple() function from the platform module to get the version of Python currently being used as a tuple of integers (major, minor, micro) and prints it to the console.

This is a way to get the version of python that you are running in a more structured way, as it provides the version details in the form of a tuple. For example, if the python version is 3.8.7, the function will return (3,8,7) as a tuple.

Versions:

Version Release date End of full support
0.9 1991-02-20 1993-07-29
1.0 1994-01-26 1994-02-15
1.1 1994-10-11 1994-11-10
1.2 1995-04-13 Unsupported
1.3 1995-10-13 Unsupported
1.4 1996-10-25 Unsupported
1.5 1998-01-03 1999-04-13
1.6 2000-09-05 2000-09
2.0 2000-10-16 2001-06-22
2.1 2001-04-15 2002-04-09
2.2 2001-12-21 2003-05-30
2.3 2003-06-29 2008-03-11
2.4 2004-11-30 2008-12-19
2.5 2006-09-19 2011-05-26
2.6 2008-10-01 2010-08-24
2.7 2010-07-03 2020-01-01
3.0 2008-12-03 2009-06-27
3.1 2009-06-27 2011-06-12
3.2 2011-02-20 2013-05-13
3.3 2012-09-29 2014-03-08
3.4 2014-03-16 2017-08-09
3.5 2015-09-13 2017-08-08
3.6 2016-12-23 2018-12-24
3.7 2018-06-27 2020-06-27
3.8 2019-10-14 2021-05-03
3.9 2020-10-05 2022-05-17
3.10 2021-10-04 2023-05
3.11 2022-10-24 2024-05
3.12 2023-10 2025-05

Python Code Editor:

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 Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.