To check the version of Python you have installed on your computer, you can use the python --version
or python -V
command. This will print the version number to the terminal or command prompt, like this:
1 2 3 |
$ python --version Python 3.8.5 |
Alternatively, you can use the sys.version
or sys.version_info
attributes of the sys
module in the Python standard library to print the version information. Here is an example:
1 2 3 4 5 6 |
import sys print(sys.version) # or print(sys.version_info) |
This will print something like the following to the output:
1 2 3 |
3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0] |
The sys.version_info
attribute is a named tuple with several attributes that contain detailed information about the version of Python that is installed. You can access these attributes to get more specific information, like the major and minor version numbers, the release level, and the serial number. Here is an example:
1 2 3 4 5 6 7 8 |
import sys print(f"Major: {sys.version_info.major}") print(f"Minor: {sys.version_info.minor}") print(f"Micro: {sys.version_info.micro}") print(f"Release Level: {sys.version_info.releaselevel}") print(f"Serial: {sys.version_info.serial}") |
This will print something like the following to the output:
1 2 3 4 5 6 |
Major: 3 Minor: 8 Micro: 5 Release Level: final Serial: 0 |
I hope this helps! Let me know if you have any other questions.