为什么在任务中的sqrt要交math.呢,而讲解中的abs不需要,两者有什么区别?
为什么在任务中的sqrt要交math.呢,而讲解中的abs不需要,两者有什么区别?
为什么在任务中的sqrt要交math.呢,而讲解中的abs不需要,两者有什么区别?
2018-03-27
由于python中有很多模块,并不是所有函数都在python默认函数列表中 例如: abs 就在python默认函数列表里 而sqrt在模块math中,首先你得导入模块,你才能调用sqrt函数方法 #!/usr/bin/env python # -*- coding: utf-8 -*- ''' sqrt() 方法返回数字x的平方根。 math.sqrt ''' import math def add(x, y, f): return f(x) + f(y) print add(4, 9, math.sqrt) print math.sqrt(4) + math.sqrt(9) # 根号4 + 根号9 = 2.0 + 3.0
举报