本文共 4806 字,大约阅读时间需要 16 分钟。
在 Python 中,函数是通过 def 语句定义的。需要注意的是,函数不会在定义时就被创建,而是只有当 def 语句被执行后,函数对象才被创建,并赋值给变量名。函数的定义格式如下:
def function_name(arg1, arg2[...]): statement[return value]
如果没有 return 语句,函数默认返回 None。函数名的命名规则包括:
Python 使用命名空间来存储对象,并遵循以下作用域规则:
global 关键字声明。如下示例:
a = 2b = 2def test(b): a = a * b return testprint(test(10)) # 输出 20
在上述代码中,b 在局部作用域中找到,a 在全局作用域中找到。
a = 1def test(a): a = a + 1 print(a)print(a) # a 值不变test(a)
a = 1b = [1, 2]def test(a, b): a = 5 b[0] = 4 print(a, b)print(a, b) # b 值已被更改test(a, b)
参数是对象指针,Python 不需要在 advance 定义传递的对象类型。例如:
def test(a, b): return a + btest(1, 2) # 返回 3test("a", "b") # 返回 'ab'test([12], [11]) # 返回 [12, 11] def function(ARG=VALUE)
def function(*ARG)
def function(**ARG)
*ARG 和一个 **ARG。*ARG 必须在连接参数和默认参数之后。**ARG 必须在最后定义。abs(x)
示例:
print(abs(-100)) # 100print(abs(1 + 2j)) # 2.2360679775
callable(object)
示例:
print(callable("123")) # 0print(callable(chr)) # 1 cmp(x, y)
示例:
a = 1b = 2c = 2print(cmp(a, b)) # -1print(cmp(b, a)) # 1print(cmp(b, c)) # 0
divmod(x, y)
示例:
print(divmod(10, 3)) # (3, 1)print(divmod(9, 3)) # (3, 0)
isinstance(object, class-or-type-or-tuple) # bool
示例:
a = 'isinstance test'b = 1234print(isinstance(a, str)) # Trueprint(isinstance(a, int)) # Falseprint(isinstance(b, str)) # Falseprint(isinstance(b, int)) # True
len(object) # integer
示例:
print(len("aa")) # 2print(len([1, 2])) # 2 pow(x, y[, z])
示例:
print(pow(2, 4)) # 16print(pow(2, 4, 2)) # 0print(pow(2.4, 3)) # 13.824
range([lower,]stop[,step])
示例:
print(range(10)) # 0, 1, 2, 3, 4, 5, 6, 7, 8, 9print(range(1, 10)) # 1, 2, 3, 4, 5, 6, 7, 8, 9print(range(1, 10, 2)) # 1, 3, 5, 7, 9
round(x[, n])
示例:
print(round(3.333)) # 3.0print(round(3)) # 3.0print(round(5.9)) # 6.0
chr(i)
示例:
print(chr(65)) # Aprint(chr(66)) # Bprint(chr(65) + chr(66)) # AB
complex(real[, imaginary])
示例:
print(complex("2+1j")) # (2+1j)print(complex("2")) # (2+0j)print(complex(2, 1)) # (2+1j)print(complex(2L, 1)) # (2+1j) float(x)
示例:
print(float("12")) # 12.0print(float(12L)) # 12.0print(float(12.2)) # 12.2 hex(x)
示例:
print(hex(16)) # 0x10print(hex(123)) # 0x7b
long(x[, base])
示例:
print(long("123")) # 123Lprint(long(11)) # 11L list(x)
示例:
print(list("hello world")) # ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']print(list((1, 2, 3, 4))) # [1, 2, 3, 4] int(x[, base])
示例:
print(int(3.3)) # 3print(int(3L)) # 3print(int("13")) # 13print(int("14", 15)) # 19 min(x[, y, z, ...])
示例:
print(min(1, 2, 3, 4)) # 1print(min((1, 2, 3), (2, 3, 4))) # (1, 2, 3)
max(x[, y, z, ...])
示例:
print(max(1, 2, 3, 4)) # 4print(max((1, 2, 3), (2, 3, 4))) # (2, 3, 4)
oct(x)
示例:
print(oct(8)) # 010print(oct(123)) # 0173
type(obj)
示例:
print(type(a)) #print(type(copy)) # print(type(1)) #
filter(function, list)
示例:
def nobad(s): return s.find("bad") == -1s = ["bad", "good", "bade", "we"]print(filter(nobad, s)) # ['good', 'we'] map(function, list[, list])
示例:
import strings = ["python", "zope", "linux"]print(map(string.capitalize, s)) # ['Python', 'Zope', 'Linux']
reduce(function, seq[, init])
示例:
import operatorprint(reduce(operator.mul, [2, 3, 4, 5])) # ((2 * 3) * 4) * 5 = 120print(reduce(operator.mul, [2, 3, 4, 5], 1)) # (((1 * 2) * 3) * 4) * 5 = 120
zip(seq[, seq, ...])
示例:
print(zip([1, 2, 3], [4, 5], [7, 8, 9])) # [(1, 4, 7), (2, 5, 8)]
def 语句可以出现在任何语句可以出现的地方,甚至可以嵌套。callable() 判断对象是否可调用。def fibs(num): result = [0, 1] for i in range(num - 2): result.append(result[-2] + result[-1]) return resultprint(fibs(10)) # [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
def hello(greeting='hello', name='world!'): print(f"{greeting}, {name}!")hello() # 输出 "hello, world!"hello(greeting='Greetings', name='universe') # 输出 "Greetings, universe!" def print_params(title, *params): print(title) print(params)print_params('params:', 1, 2, 3) # 输出 'params: (1, 2, 3)'print_params_2('Nothing:',) # 输出 'Nothing: ()' 通过以上内容,可以看出 Python 的函数与参数机制非常灵活且强大,适用于多种场景和需求。
转载地址:http://vxofk.baihongyu.com/