benqian=1000000
year=0
while benqian<2000000:
benqian=benqian*(1+0.067)
year+=1
else:
print(year,benqian) # 11 2040838.3830545251
#九九乘法表
for o in range(1,10):
for i in range(1,o+1):
print(str(o)+"*"+str(i)+"="+str(o*i),end=" ")
#换行
print()
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
def normal_para(one,two,three):
print(one+two)
return None
def default_para(one,two,three=200):
print(one+two)
return None
def keys_para(one,two,three):
print(one+two)
return None
normal_para(1,2,3) # 3
default_para(1,2) # 3
keys_para(two=2,one=1,three=3) # 3
def stu(name,age,*args,hobby="没有",**kwargs):
print("Hello,大家好!")
print("我叫{0},我今年{1}岁了".format(name,age))
if hobby=="没有":
print("没爱好")
else:
print("我的爱好是{0}".format(hobby))
print("*"*20)
print(type(args))
for i in args:
print(i)
print("#"*20)
print(type(kwargs))
for k,v in kwargs.items():
print(k,"---",v)
#开始调用函数
name="liuliu"
age=18
stu(name,age,"睡觉","睡觉","吃饭",hobby="数据",hobby1="三分法",weight="50kg")
'''
Hello,大家好!
我叫liuliu我今年18岁了
我的爱好是数据
********************
睡觉
睡觉
吃饭
####################
hobby1 --- 三分法
weight --- 50kg'''
def stu1(*args):
'''
这是函数文档
'''
print("hahaha")
for i in args:
print(i)
stu1("liu","ze","h",19)
l=["liu","ze","h",19]
stu1(l)
stu1(*l) #用*解包。同理dict,只是dict需要使用两个*号
'''
hahaha
liu
ze
h
19
hahaha
['liu', 'ze', 'h', 19]
hahaha
liu
ze
h
19'''
help(stu1)
stu1.__doc__
Help on function stu1 in module __main__:
stu1(*args)
这是函数文档
'\n 这是函数文档\n '
a1=100
def fun():
global b1 # 提升局部变量为全局变量
b1=100
b2=99
print(a1,b1,b2)
return None
fun() # 100 100 99
print(b1) # 100
print(eval("100+200")) # 300
print(exec("100+200")) # None
zz=exec("print('x+y:',100+200)") # 300
a = 10 # 全局变量
def outer(): # 外部函数
a = 5 # outer函数定义的局部变量
def inner(): # 内部函数
nonlocal a # 声明a是outer的局部变量
a = 20
print('inner函数中的a值:', a)
inner() # 调用inner函数
print('outer函数中的a值:', a)
outer()
# inner函数中的a值: 20
# outer函数中的a值: 20
print(' 全局变量a值:',a) # 全局变量a值: 10
def funa(n):
if n==1:
return 1
s=n*funa(n-1)
return s
def funb():
funa(3)
print("haha")
result=funa(4)
print(result) # 24
s=funb()
print(s) # gaha None
# 斐波那契数列
def fib(n):
if n==1 or n==2:
return 1
return fib(n-1)+fib(n-2)
rst=fib(10)
print(rst) # 55
#汉诺塔
a,b,c="A","B","C"
def hano(a,b,c,n):
if n==1:
print("{}-->{}".format(a,c))
return 1
if n==2:
print("{}-->{}".format(a,c))
print("{}-->{}".format(a,b))
print("{}-->{}".format(b,c))
hano(a,c,b,n-1)
print("{}-->{}".format(a,c))
hano(b,a,c,n-1)
hano(a,b,c,n=2)
A-->C
A-->B
B-->C
A-->B
A-->C
B-->C
hano(a,b,c,n=3)
A-->B
A-->C
C-->B
A-->C
A-->B
C-->B
A-->C
B-->C
B-->A
A-->C
B-->A
B-->C
A-->C
# 匿名函数
# 函数名 = lambda 形参: 返回值
res = lambda a, b: a * b
print(res(3,4)) # 12
print(3) if 3 < 4 else print(4) # 13
# 内置函数
# 查看所有的内置函数
import builtins
print(dir(builtins))
zip() # 拉链函数 函数将可迭代对象作为参数,将里面对应的元素打包成一个个元组
map(函数, 对象) # 映射函数 将可迭代对象中每一个元素来进行映射,分别执行函数
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '__IPYTHON__', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'display', 'divmod', 'enumerate', 'eval', 'exec', 'filter', 'float', 'format', 'frozenset', 'get_ipython', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']
# 3.拆包,元组,字典,列表皆可拆包
tu = (1, 2, 3, 4)
a, b, c, d = tu
print(a, b, c, d) # 1 2 3 4
a, *b = tu
print(a) # 取到开头的值1
print(b) # [2, 3, 4]
*c, d = b
print(c) # [2, 3]
print(d) # 4 取到结尾的值
a, *b, c, d = tu
print(a) # 1
print(b) # [2]
print(c) # 3
print(d) # 4
# reduce: 先把对象中的前两个元素取出,计算出一个值然后保存着, 接下来把这个值跟第三个元素进行计算
# reduce(函数, 对象)
# 函数: 必须接收两个参数
# 对象: 可迭代对象
li2 = [1, 2, 3, 4]
from functools import reduce
def add(x, y):
return x - y
res = reduce(add, li2) # -8
print(res)
print(reduce(lambda a, b: a * b, [3, 6, 4])) # 72
在嵌套函数的前提下,内部函数使用了外部函数的变量,而且外部函数返回了内部函数,我们就把使用了外部函数变量的内部函数称为闭包
构成条件:
def outer(): # 外部函数
n = 10
def inner(): # 内部函数
# 在内函数中,用到了外部函数的变量
print(n)
# 外部函数的返回值是内部函数的函数名
return inner
print(outer()) # 返回的是内部函数的地址 .inner at 0x0000020706DB89D8>
ot = outer() # 返回内部函数
ot() # 调用内部函数 10