Python Function 函数
2017-12-09 10:31
253 查看
自定义函数
Python 模块 里的函数
这样每次调用要用math.sqrt()很麻烦
所以有以下的方法
然后还有一次性导入 模块 里全部函数的代码
查看 模块 里所有的
不用从模块导入就可以使用的函数?
附一段随意的代码
骚操作?
def name ( parameter1 , parameter2 ) : # 别忘了冒号 参数可以有任意个(貌似可以没有) res = parameter1 * parameter2 #pyhon 用空格缩进 return res # 可以没有返回值
Python 模块 里的函数
print sqrt(25) import math #generic import print math.sqrt(25)
这样每次调用要用math.sqrt()很麻烦
所以有以下的方法
from math import sqrt print sqrt(25)
然后还有一次性导入 模块 里全部函数的代码
from math import * print sqrt(25)
查看 模块 里所有的
import math # Imports the math module everything = dir(math) # Sets everything to a list of things from math print everything # Prints 'em all!
不用从模块导入就可以使用的函数?
a.upper() #将a全转化为大写字母 a.lower() #将a全转化为小写字母 str(a) #返回a的字符串形式 len(a) #返回a的长度 max(a,b,c,d) #返回最大的(参数数量>1),可以包含int和float min(a,b,c,d) #返回最小的(参数数量>1),可以包含int和float abs(a) #返回a的绝对值
# type(a) 返回a的数据类型 print type(42) #返回 int print type(4.2) print type('spam') # Python will output: <type 'int'> <type 'float'> <type 'str'>
附一段随意的代码
def distance_from_zero (a) : if type (a) == int or type(a) == float : return abs(a); else : return "Nope"
骚操作?
def biggest_number(*args): print max(args) return max(args) def smallest_number(*args): print min(args) return min(args) def distance_from_zero(arg): print abs(arg) return abs(arg) biggest_number(-10, -5, 5, 10) smallest_number(-10, -5, 5, 10) distance_from_zero(-10)
相关文章推荐
- Python基础-函数(function)
- 转 Python中Function(函数)和methon(方法)
- python 中 partial function 骗函数的使用介绍
- Python函数(function)与方法(method)区别
- 关于Python的函数(Method)与方法(Function)
- Python function 函数定义
- Python中Function(函数)和methon(方法)
- Python中Function(函数)和methon(方法)
- Python 内建函数 - staticmethod(function)
- 学习笔记☞ python 基础(python3)【四】(函数 function,None,return,列表-2,random模块)
- Python3学习(7)--函数【Function】
- Python 内建函数 - filter(function, iterable)
- python3 第二十三章 - 函数式编程之Partial function(偏函数)
- Python学习笔记3:函数function
- Python学习札记(十) Function1 函数调用
- Python基础-偏函数 Partial function
- python之函数(function)
- 函数名function是一个数据类型,可以赋值 分类: python基础学习 2013-09-12 11:01 366人阅读 评论(0) 收藏
- Python 内建函数 - map(function, iterable, ...)
- Python中Function(函数)和methon(方法)