Python中sys.version_info.major()函数的使用方法
发布时间:2023-12-27 08:00:01
sys.version_info.major()函数用于获取Python解释器的主版本号。
使用方法如下:
import sys major_version = sys.version_info.major print(major_version)
例子:
import sys
if sys.version_info.major < 3:
print("Python 2 is not supported.")
else:
print("Running on Python 3 or later.")
上述例子中,我们通过判断Python解释器的主版本号,来确定是否支持Python 2。如果主版本号小于3,则打印"Python 2 is not supported.";否则打印"Running on Python 3 or later."。
另外,需要注意的是,在代码中调用major()时,不需要添加括号。因为major()是一个属性而不是函数。
