help(print)
# 运行结果:print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
b = 456
def fun1():
a = 123
print('函数内部', a)
print('函数内部', b)
fun1()
print('函数外部', b) # 打印结果 函数内部 123 函数内部 456 函数外部 456
print('函数外部', a) # 会报错,因为a为局部作用域,函数外面访问不到
def fun2():
global a # global :声明 :a为全局变量
a = 20
def fun3():
print('fun3里面', a)
fun3()
fun2()
print(a) # 打印结果:fun3里面 20 20
# 实现10的阶乘
# 方法1
print(1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10)
# 方法2
n = 10
for i in range(1, 10):
n *= i
print(n)
# 方法3
def fun(x):
for i in range(1, x):
x *= i
return x
fun(10)
print(fun(10))
# 方法4:用递归函数来实现
def fun1(x):
if x == 1:
return 1
return x * fun1(x - 1)
print(fun1(10))
递归函数的练习
# 实现n的m次幂
def fun(n, m):
if m == 1:
return n
return n * fun(n, m - 1)
print(fun(10, 5))