Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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



Python: Tips of the Day

Find current directory and file's directory:

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)

To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

Ref: https://bit.ly/3fy0R6m