To access and display the Python version, you can use the following code:
import sys
print(f"Python version: {sys.version}")
In the code above, we first import the sys
module. We then use the sys.version
attribute to retrieve the Python version information. Finally, we use the print
function to display the version information.
When you run this code, you will see the Python version displayed in the output. The version string will include the detailed version number and additional information about the compiler and build details.
It is important to note that sys.version
only retrieves the Python version information at runtime. If you want to perform version checking or use specific version-related features, you can also use the sys.version_info
attribute. This attribute provides access to the version information as a tuple of integers, allowing you to compare specific version numbers or perform conditional checks.
import sys
if sys.version_info >= (3, 7):
print("Python version is 3.7 or higher")
else:
print("Python version is lower than 3.7")
In the code snippet above, we check if the Python version is 3.7 or higher using the sys.version_info
attribute. If the condition is true, we print a corresponding message. Otherwise, we print a different message.
Using the sys.version
and sys.version_info
attributes, you can easily obtain and utilize the Python version information in your code. This can help in ensuring compatibility and taking advantage of specific features available in different Python versions.