文章目录
for——else
#for——else用法:
for i in range(10):
print(i)
break
print("123")
#结果 : 0
# 123
for i in range(10):
print(i)
break
else:
print("123")
#结果: 0
求质数
#求10-20以内的质数
for i in range(10,20):
for j in range(2,i): #除数不为0,也不可以是1和它本身
if i%j == 0:
break
else:
print(i)
#结果:
# 11
# 13
# 17
# 19
多级菜单——循环嵌套
dir1={
"学习":{
"语文":["古诗文","文言文","阅读","作文"],
"数学":["高等数学","离散数学","线性代数","概率论"]
},
"娱乐":{
"电影":["影","肖申克的救赎","飞驰人生"],
"音乐":["小半","虚拟","矜持"],
"游戏":["王者荣耀","开心消消乐"]
}
}
while True:
for x1 in dir1:
print(x1)
choice1 = input("请选择>>>[b][q]:")
if choice1 in dir1:
while True:
for x2 in dir1[choice1]:
print("\t\t",x2)
choice2 = input("请选择>>>[b][q]:")
if choice2 in dir1[choice1]:
while True:
for x3 in dir1[choice1][choice2]:
print("\t\t\t",x3)
choice3 = input("请选择>>>[b][q]:")
if choice3 in dir1[choice1][choice2]:
while True:
for x4 in dir1[choice1][choice2][choice3]:
print("\t\t\t\t", x4)
choice4 = input("请选择[b]或者[q]:")
if choice4 == 'b':
break
elif choice4 == 'q':
exit()
elif choice3 == 'b':
break
elif choice3 == 'q':
exit()
elif choice2 == 'b':
break
elif choice2 == 'q':
exit()
函数
面向对象:——类【class】
面向过程:——过程【def】
函数式编程:——函数【def】
过程和函数
在python里,过程和函数已经不再区分。过程也有返回值,返回值为None,函数返回值为0
#没有返回值的函数即过程。
'''过程'''
def gc():
print("我是面向过程")
'''函数'''
def hs():
print("我是函数式编程")
return 0
a = gc()
b = hs()
print(a)
print(b)
结果:我是面向过程
我是函数式编程
None
0
函数调用——实时打印时间戳
import time
def log():
'''打印时间戳'''
time_format = '%y-%m-%d %X'
time_current = time.strftime(time_format)
print(time_current)
def test1():
'''测试函数1'''
print("this is a test1...")
log()
def test2():
'''测试函数2'''
print("this is a test2...")
log()
def test3():
'''测试函数3'''
print("this is a test3...")
log()
test1()
test2()
test3()
返回值作用:
- 返回函数值
- 结束函数执行
返回值的类型
-
当没有返回值时,返回0。
-
当返回值时一个特定值时,返回这个值。
-
当返回值是多个值时,返回的是一个元组。
-
当返回值是一个函数名时,返回的是函数的内存地址。
其后加()调用其函数,返回存储的内容结果。
-
当返回值是一个函数【函数名+()】时,返回的是函数的执行结果。
函数调用
位置参数 1
关键字参数 a = 1
-
位置实参调运,形参和实参是一一对应
-
关键字参数调运,与形参位置无关。
-
关键参数不能写在位置参数之前。
-
位置参数和关键字参数同时存在,以位置参数调运为主。
-
关键字参数变量值要和形式参数同名
-
默认参数的特点:调运函数的时候,默认参数非必须传递
用途:参数在不修改其值的情况下,使用默认值 -
实参不能大于形参数目。
-
实际 上,生产环境中,实参是需要存在多个,如何去定义实参呢?局需要参数组的概念。
def test(*args): print(args) test(1,3,4,5,6,7,8,9,) test(*[1,2,2,4,5,7,])
执行结果:
(1, 3, 4, 5, 6, 7, 8, 9)
(1, 2, 2, 4, 5, 7)
只想结果是元组。def test(x,y,*args): print(x) print(y) print(args) test(1,3,4,5,6,7,8,9,) test(*[1,2,2,4,5,7,])
参数组和形式参数可以混合使用。
def test(**kwargs): print(kwargs) #test(name='anliu',age=8,sex='F') test(**{'name':"anliu","age":18}) #把N个关键字参数,转换为字典的形式
执行结果:
{‘name’: ‘anliu’, ‘age’: 8, ‘sex’: ‘F’}
{‘name’: ‘anliu’, ‘age’: 18}
可将关键字或者字典传递,并得到一个字典的输出结果。
参数和参数组同时使用也是没有问题的。def test(name,age=8,**kwargs): print(name) print(age) print(kwargs) test("anliu",age=28,hobby='paoniu',sex='F')
执行结果:
anliu
28
{‘hobby’: ‘paoniu’, ‘sex’: ‘F’}*args : 接受N个位置参数,转化成元组的形式
****** kwargs :接受N个关键字参数,转化成字典的形式
装饰器
定义:
装饰器:装饰器本质是函数,(函数是有特定的功能),装饰器的功能就是装饰他功能。就是为
其他函数添加附加功能。
原则:
1.不能修改被装饰函数的源代码
2.不能修改被装饰的函数的调运方式。
(装饰器对被装饰函数来说是透明的)
实现装饰器知识储备
1.函数即“变量”
2.高阶函数
(1)把一个函数名当做实参传递给另一个函数。
(2)返回值中包含函数名。
3嵌套函数
装饰器=高阶函数+嵌套函数
装饰器示例1:
import time
def test1():
print("good..")
def logges(func):
format_tme = "%Y-%m-%d %X"
time_current = time.strftime(format_tme)
print(time_current)
return func
test1 = logges(test1)
test1()
结果:2019-07-17 16:42:30
good…
被装饰函数实在装饰器中调用的。
装饰器示例2:
import time
def test1():
print("good..")
def timer(func):
def logges():
func()
format_tme = "%Y-%m-%d %X"
time_current = time.strftime(format_tme)
print(time_current)
return logges
test1 = timer(test1)
test1()
结果:good…
2019-07-17 16:40:54
装饰器示例3:
import time
def test1():
print("good..")
def timer(func):
def logges():
format_tme = "%Y-%m-%d %X"
time_current = time.strftime(format_tme)
print(time_current)
func() #test1()
return logges
test1 = timer(test1)
test1() #logges()
结果:2019-07-17 16:42:30
good…
装饰器示例4:
import time
def timer(func):
def logges(*args,**kwargs):
format_tme = "%Y-%m-%d %X"
time_current = time.strftime(format_tme)
print(time_current)
func(*args,**kwargs) #test1() 放在def logges()首局,改变结果顺序
return logges
@timer # test1 = timer(test1)
def test1():
print("good..")
test1() #logges()
@timer
def test2():
print("see you later..")
test2()
结果:2019-07-17 16:56:14
good…
2019-07-17 16:56:14
see you later…