01_函数嵌套与闭包
函数也是一个对象 def func(a,b): return a+b abc = func 这么做abc就和func一样了 num1 = func(10,20) num2 = abc(10,20) print(num1,num2)# python允许函数的内部再定义函数# 一般这种情况,内部函数都会作为返回值返回出来# 在返回一个函数的时候,也会将函数当前的变量等信息携带出来# 这种机制称之为【闭包】def outer(factor): def inner(num): return factor*num return innertest1 = outer(10)test2 = outer(100)print(test1(20)) # 这里输出200print(test2(5)) # 这里输出500
02_装饰器
def w1(func): def inner(): print("开始检查") # 一堆检查的代码..... print("检查完毕") re = func() print("函数调用完毕") return re return inner# 装饰器# @w1的意思就是把下面的函数传递到w1中,并返回w1的# 返回值给下面的函数对象。# 相当于修改了函数对象指向了inner# 通过这种方式,实现了不修改函数代码,就增加函数功能的目的# 符合对于修改关闭,对于扩展开放的原则。@w1 # fun1 = w1(fun1)def fun1(): print("我实现了一个大功能1")@w1def fun2(): print("我实现了一个大功能2")@w1def fun3(): print("我实现了一个大功能3")# 面还有100个函数....fun1()fun2()
03_关于时间
# 时间 是time模块中的函数# python中,有三种表示时间的方式:# 1. 时间戳# 2. 字符串# 3. 时间结构体import time# 获取时间戳time_temp = time.time()print(time_temp)# 将时间戳转为能看懂的时间字符串str_time = time.ctime(time_temp)print(str_time)# 将时间戳转为标准时间元组tup_time = time.gmtime(time_temp)print(tup_time)# 将时间戳转为本地时间元组tup_time = time.localtime(time_temp)print(tup_time)# 提取时间元组中的信息print(time.strftime("%Y-%m-%d %H:%M:%S",tup_time))
04_关于日期类
# python还能够获取日期# 使用的是 datatime模块# 这个模块 基本兼容time模块 同时还有日期功能import datetime# 其中包含着 datetime.timedt1= datetime.date.today()print(dt1)# 制定一个日期,得到日期对象dt2 = datetime.date(2017,8,15)print(dt2)# 分别输出年月日print(dt1.year,dt1.month,dt1.day)# 获取星期几print(dt1.isoweekday())# 日期和时间统一使用 可以同时设置日期和时间datetime1 = datetime.datetime(2018,12,15,8,10,55)print(datetime1.date(),datetime1.time())datetime2 = datetime.datetime.now()print(datetime2.date(),datetime2.time())# 日期加1天或者减1天datetime2 = datetime.datetime.now()# 明天dt1 = datetime2 + datetime.timedelta(days=1)# 昨天dt1 = datetime2 + datetime.timedelta(days=-1)# 前天dt1 = datetime2 + datetime.timedelta(days=-2)
05_随机数
import random# 生成一个0到1之间的随机小数num = random.random()print(num)# 生成一个范围内的随机整数for i in range(0,5): rand_num1 = random.randint(10,50) rand_num2 = random.randrange(10,50) print(rand_num1,rand_num2)# 生成一个随机验证码
06_生成随机验证码
import randomtmp = ""for i in range(6): rad1 = random.randrange(4) if rad1 ==1 or rad1 ==3: rad2 = random.randrange(0,9) tmp += str(rad2) else: rad3 = random.randrange(65,90) tmp += chr(rad3)print(tmp)
07_文件操作
list1 = ["hello\n","world\n","nihao\n","15pb\n"]# 打开文件file = open(".\\123.txt","r+")# file.write("jintiantianqibucuo")#file.writelines(list1)# 可以一次性将数据全部获取到#str_file = file.read()# 可以一行一行的读取数据str_line = file.readline()while str_line!='': print(str_line) str_line = file.readline()file.close()
08_多线程
import threadingimport timedef thread_proc(): n = 0 thread_name = threading.current_thread().name while n<100000: n = n+1 print("线程名为%s 当前的数字为%d"%(thread_name,n)) time.sleep(1)# 创建一个线程t = threading.Thread(target=thread_proc,name= "15pb")# 启动这个线程t.start()n = 0thread_name = threading.current_thread().namewhile n < 100000: n = n + 1 print("线程名为%s 当前的数字为%d" % (thread_name, n)) time.sleep(0.5)# 等待线程结束t.join()print("大家结束了")
09_线程同步
import threadingg_Num = 0lock = threading.Lock()def threadProc(): global g_Num for i in range(1000000): lock.acquire() # 锁住 g_Num = g_Num+1 # 修改数据: lock.release() # 开锁thread1 = threading.Thread(name = "15PB",target=threadProc)thread2 = threading.Thread(name = "hello",target=threadProc)thread1.start()thread2.start()thread1.join()thread2.join()print(g_Num)
10_线程回调函数传递参数
import threadingimport timedef thread_proc(*arg): n = 0 outer_arg = arg while n<100000: n = n+1 print(outer_arg) time.sleep(1)# 创建一个线程t = threading.Thread(target=thread_proc,args=(1,2,3,4))# 启动这个线程t.start()n = 0thread_name = threading.current_thread().namewhile n < 100000: n = n + 1 print("线程名为%s 当前的数字为%d" % (thread_name, n)) time.sleep(0.5)# 等待线程结束t.join()print("大家结束了")