def func_name(arg1[,arg2,...,argN])
fun_body
def plus(num):
print(num + 1)
""" 调用函数 """
plus(2) # 3
plus(5) # 6
"""
输出函数的引用,显示地址
f和plus指向同一个函数
"""
f = plus
print(plus)
print(f)
f(2) # 3
f(5) # 6
# 返回单个对象
def add1(left, right):
res = left + right
return res
# 返回一个表达式
def add2(left, right):
return left + right
# 返回多个对象, 自动打包成一个元组
def add3(left, right):
res1 = left + right
res2 = left * right
return res1, res2
# 返回多个表达式
def add4(left, right):
return left + right, left * right
# return None
def add5(left, right):
print(left + right)
return
# return None
def add6(left, right):
pass
print(add1(3, 4))
print(add2(3, 4))
print(add3(3, 4))
print(add4(3, 4))
print(add5(3, 4))
print(add6(3, 4))
def div_mod(x, y):
"""
Return the tuple (x//y, x%y).
:param x: number(except complex)
:param y: number(except complex)
:return: (x//y, x%y)
"""
# 上面的这段多行注释即文档注释
div = x // y
mod = x % y
return div, mod
# __doc__属性存放了文档注释
print(div_mod.__doc__)
print(divmod.__doc__)
help() # 启动交互式帮助系统
help("keywords") # 查看关键字
help(list) # 生成list的帮助页
print(abs(-6)) # 6,参数是整数
print(abs(6.9)) # 6.9,参数是浮点数
print(abs(3+4j)) # 5.0,参数是复数
print(divmod(7, 3)) # (2, 1)
print(7 // 3, 7 % 3)
print(divmod(-9, 2)) # (-5, 1)
print(-9 // 2, -9 % 2)
print(max([3, 1, 2]))
print(max(3, 1, 2))
print(max('1', '2', '3', '10'))
print(max([], default=666))
print(max([1, 2, -3], key=abs))
print(min([3, 1, 2]))
print(min(3, 1, 2))
print(min('20', '2', '3', '10'))
print(min([], default=666))
print(min([-9, 2, -3], key=abs))
print(pow(-2, 3)) # -2**3 = -8
print(pow(-2, 3, 3)) # -2**3 % 3 = 1
pi = 3.141592653
print(round(pi))
print(round(pi, ndigits=None))
print(round(pi, ndigits=1))
print(round(pi, ndigits=2))
print(round(pi, ndigits=3))
print(round(3.141592653, ndigits=0))
print(round(-3.141592653, ndigits=0))
print(round(0.141592653, ndigits=0))
print(round(-0.141592653, ndigits=0))
# 作为了解
print(round(0.5))
print(round(1.5))
print(round(2.5))
print(round(3.5))
print(round(2.665, ndigits=2))
print(round(2.675, ndigits=2))
print(sum([1, 2, 3]))
print(sum([1, 2, 3], start=100))
print(sum({1: 2, 3: 4, 5: 6}, start=100))
def func(b):
print(id(a),a)
print(id(b),b)
a = 789
func(a)
def func(b):
print(id(a), a)
print(id(b), b)
a = [789]
func(a)
def func(b):
b.append(345)
a = [789]
func(a)
print(a)
def func(a, b):
print(a - b)'
func(4, 3) # 1
func(3, 4) # -1
func(a=3, b=4) # -1
def func(a, b):
print(a - b)
func(a=3, b=4)
func(3, b=4)
def func(a, b=4):
print(a - b)
func(3)
func(3, 5)
def func(*args):
print(args)
func()
func(3, 1, 4, 6)
def func(**kwargs):
print(kwargs)
func()
func(a=3, b=2, c=4)
def func(*args, **kwargs):
print(args)
print(kwargs)
func()
func(1, 2, a=3, b=4)
def func(pos1, pos2, /, pos_or_kwd, *, kwd1, kwd2):
pass
func(1, 2, 3, kwd1=4, kwd2=5)
func(1, 2, pos_or_kwd=3, kwd1=4, kwd2=5)
print((lambda: 'It just returns a string')())
f = lambda: 'It just returns a string'
print(f())
(lambda x, y, z: print(x + y + z))(1, 2, 3)
f = lambda x, y, z: print(x + y + z)
f(1, 2, 3)
def call_f(function, a, b, c):
return function(a, b, c)
print(call_f(lambda x, y, z: x + y + z, 1, 2, 3)
tup = 345, 'hello', 789
print(tup)
解包是针对可迭代对象的操作
a, b, c = [4, 3, 'a']
print(a) # 4
print(b) # 3
print(c) # 'a'
a, *b, c = 'hello'
print(a) # 'h'
print(b) # ['e', 'l', 'l']
print(c) # 'o'
a, *b, c = 'he'
print(a) # 'h'
print(b) # []
print(c) # 'e'
*a, = 'hel'
print(a) # ['h', 'e', 'l']
_, *b, _ = [4, 3, 5, 7]
print(b) # [3, 5]
def func(a, b, c):
print(a, b, c)
"""
在函数传参时, *iterable可以将
该iterable解包成位置参数
"""
tup = (1, 2, 3)
func(*tup)
"""
在函数传参时, **dict可以将
该dict解包成关键字参数
"""
d = {'a': 1, 'b': 2, 'c': 3}
func(*d)
func(**d)
请先看以下程序:
list = [1, 2, 3, 4, 2, 2]
set1 = set(list)
print(list(set1)) # 报错TypeError: 'list' object is not callable
import builtins
print(dir(builtins)) # 输出builtins模块中的所有属性名、方法名、类名构成的列表
print(dir())
print(dir(list))
import random
import copy
def func1(arg1, arg2):
num = 666
print(locals()) # 函数会以字典类型返回当前位置的全部局部变量
def func2(arg1, arg2):
num = 777
print(locals())
num = 111
func1(222, 333)
func2(444, 555)
print(globals()) # 函数会以字典类型返回当前位置的全部全局变量。
# 在全局作用域, locals()等价于globals()
print(locals())
局部命名空间 >> 全局命名空间 >> 内置命名空间
eval(expression[, globals[, locals]])
exec(object[, globals[, locals]])
print(eval('abs(-3)'))
exec('print(abs(-3))')
string = """
import random
num = random.randint(1, 10)
print(num)
"""
exec(string)
a = 1
b = 2
def add():
a = 3
print(eval("a + b"))
print(eval("a + b", {'a': 4, 'b': 5}))
print(eval("a + b", {'a': 6, 'b': 7}, {'a': 8, 'b': 9}))
print(eval("a + b", {'a': 6, 'b': 7}, {'a': 8, 'c': 9}))
add()
def func():
a = 2 # 局部变量
b = 3 # 局部变量
print(a + b) # 局部作用域可以调用局部变量a,b
print(d) # 局部作用域可以调用全局变量d
d = 4 # 全局变量
func()
# print(a, b) # 全局变量不能调用局部变量
def outer():
b = 2 # Enclosing变量b,c
c = a + 3 # Enclosing可以调用全局变量a
def inner():
c = 5 # 局部变量c
print(a) # 局部作用域可以调用全局变量a
print(b) # 局部作用域可以调用Enclosing变量b
print(c) # 优先调用自己作用域内的变量c,而不调用Enclosing变量c
inner()
a = 1 # 全局变量
outer()
def outer():
a = c + 2 # Enclosing可以调用全局变量c
def inner():
b = c + 3 # 局部作用域可以调用全局变量c
print(a + b) # 局部作用域可以调用Enclosing变量a
inner()
c = 1 # 全局变量
outer()
print(c) # 调用全局变量c
# abs是内置函数、int是内置类,它们都在内建作用域builtins模块中
num1 = abs(-100)
num2 = int(3.141592653)
def outer():
global a, b # 声明当前作用域的a,b为全局变量
a, b, c, d = 3, 4, 5, 6
print(a, b)
def inner():
global a, b # 声明当前作用域的a,b为全局变量
nonlocal c, d # 声明当前作用域的c,d为Enclosing变量
a, b, c, d = 7, 8, 9, 0
inner()
print(c, d)
a, b = 1, 2
outer()
print(a, b)
def func():
# 局部有定义a, 则不能使用全局的a
b = a + 1
print(b)
a = 6
a = 9
func()