- # 函数补充
- # 位置参数 def hello (name,sex)
- # 默认值参数 def hello (countyu = 'china')
- # def send_report(*args) 可变参数, 参数组 不是必填, 不限制个数, 传入后保存为元组
- #*args,
- # 1, 不是必填参数
- # 2, 不限制参数的个数
- # 3, 传入多个参数的时候他把参数放到一个元组里面
- # def send_report(*args):
- # print('发邮件')
- # print(args)
- # send_report()
- # send_report(1,2,3,4)
- # **args
- # 1, 不是必填, 不限制参数个数
- # 2, 叫做关键字参数, 默认空字典, 传入多个参数把参数放入字典中
- # 3, 必须使用关键字调用, k=v,k1==v1
- # def send_report(**kwargs):
- # print('发邮件')
- # print(kwargs)
- #
- # send_report()
- # # send_report(1)
- # send_report(name='xiaohei',age=18)
- # 函数调用时, 若使用关键字参数, 后面就不能使用位置参数
- # 函数传参
- # def connect(ip,port,username,password):
- # print(ip)
- # print(port)
- # print(username)
- # print(password)
- # info = {
- '192.43.54.65',3318,'root','password123'
- }
- # li = ['123',345,654,'bvs']
- # s = '1234'
- # se = {
- 1,2,3,4
- }
- # connect(*li) #将数组中的数据解包, 传入方法
- # connect(*info) #将元组中的数据解包, 传入方法中
- # 元组与数据中的数据个数与方法所需参数个数要一致, 否则会报错 此传参方法, 除 key=value 方式保存的字典类型数据外均可使用
- # 字典使用需要使用 两个 * 号组合
- # dic={
- 'port':3306,'username':'root','password':'12345','ip':'127.0.0.1'
- }
- # connect(**dic)
- #
- # 全局变量
- # 如果是不可修改的全局变量 需要加 global
- # 如果是可修改的全局变量则不需要添加
- # def test ():
- # global a
- # a = 5
- #
- # def test1():
- # c = a +5
- # return c
- # test()
- # res = test1()
- # print(res)
- # 内置函数
- import random
- # print(dir(random))
- # dir() 查看可调用的方法
- # sorted() 排序 排序后存储一个 list
- # reversed()
- # list(reversed('需要反转的对象')) 反转 将对象反转, 返回一个 list
- # print(all([1,2,3,4]))# 判断可迭代的对象里面的值是否都为真
- # print(any([0,1,2,3,4]))# 判断可迭代的对象里面的值是否有一个为真
- # print(bin(10))# 十进制转二进制
- # print(bool('s'))# 把一个对象转换成布尔类型
- # print(bytearray('abcde',encoding='utf-8'))# 把字符串变成一个可修改的 bytes
- # print(callable('aa'))# 判断传入的对象是否可调用
- # print(chr(10))# 打印数字对应的 ascii
- # print(ord('b'))# 打印字符串对应的 ascii 码
- # print(dict(a=1,b=2))# 转换字典
- # print(dir(1))# 打印传入对象的可调用方法
- # print(eval('[]'))# 执行 python 代码, 只能执行简单的, 定义数据类型和运算
- # print(exec('def a():pass'))# 执行 python 代码 实现在线变成 将代码转换成字符串传入
- # s = '''# print('hello')
- # '''
- # exec (s)
- # eval() #只能执行简单的 python 代码 eval 可以将 JSON 转换为字典
- # res = eval('1+1')
- # print(res)
- # f = open('demo.txt',encoding='utf-8').read()
- # res = eval(f)
- # print(res)
- # res = eval('{
- 1,2,3,4,5,6
- }')
- # print(res)
- # print(filter(lambda x:x>5,[12,3,12,2,1,2,35]))# 循环调用函数, 过滤传入的参数, 函数结果返回 true 保存, 返回 false 就不要
- # stu_score = {'xiaobai':50,'xiaohei':30,'xiaolan':80,'xiaojun':100,
- # 'xiaoming':60}
- # # stu_score = [60,30,50,70,99,100,67]
- # result = sorted(list(filter(lambda score:score>= 60,stu_score.values())))
- # print(result)
- # stus = ['aa','bb','cc','dd','ee']
- # def add_prx(name):
- # return 'cnz-%s' %name
- # result = list(filter(add_prx,stus))
- # print(result)
- # print(map(lambda x:x>5,[1,2,3,4,5,6])) map 循环调用函数, 把每一次调用的返回值保存, 最后返回
- import random,string,os
- # stus = ['aa','bb','cc','dd','ee']
- # def add_prx(name):
- # return 'cnz-%s' %name
- # result = list(map(add_prx,stus))
- # print(result)
- # def choice(a):
- # return a+1
- # # map(choice,[1,2,3,4,5,6])
- #
- # result = list(map(choice,[1,2,3,4,5,6]))
- #
- # print(result)
- # stus = ['aa','bb','cc','dd','ee']
- # # list(map(os.mkdir,stus)) #不转 list 不会创建
- # result = map(os.mkdir,stus)
- # print(result)
- # 匿名函数
- # abc = lambda x:x+1 # 冒号前是传入参数, 冒号后是返回值
- # 等同于
- # def abc2(x):
- # return x+1
- # print(abc(1))
- # print(abc2(1))
- # 递归 自己调用自己 递归最多循环 999 次 递归一定要指定一个结束条件, 否则会报错 效率没有循环效率高, 能使用循环不使用递归
- def test1():
- num = int(input('please enter a number:'))
- if num%2==0:# 判断输入的数字是不是偶数
- return True #如果是偶数的话, 程序就退出了, 返回 true
- print('不是偶数请重新输入!')
- return test1()# 如果不是偶数的话继续调用自己, 输入值
- # print(test1())# 调用 test
- # print(frozenset({1,2,3,3}))# 定义一个不可修改的集合
- # print(globals())# 返回程序内所有的变量, 返回的是一个字典
- # print(locals())# 返回局部变量
- # print(hash('aaa'))# 把一个字符串哈希成一个数字
- # print(hex(111))# 数字转成 16 进制
- # print(max(111,12))# 取最大值
- # print(oct(111))# 把数字转换成 8 进制
- # print(round(11.11,2))# 取几位小数
- # print(sorted([2,31,34,6,1,23,4]))# 排序
- # dic={1:2,3:4,5:6,7:8}
- # print(sorted(dic.items()))# 按照字典的 key 排序
- # print(sorted(dic.items(),key=lambda x:x[1]))# 按照字典的 value 排序
- # __import__('decorator')# 导入一个模块
- # JSON 处理
- # import JSON
- # dic = {"name":"niuniu","age":18}
- # print(JSON.dumps(dic))# 把字典转成 JSON 串
- # fj = open('a.json','w')
- # print(JSON.dump(dic,fj))# 把字典转换成的 JSON 串写到一个文件里面
- # s_json = '{
- "name":"niuniu","age":20,"status":true
- }'
- # print(JSON.loads(s_json))# 把 JSON 串转换成字典
- # fr = open('b.json','r')
- # print(JSON.load(fr))# 从文件中读取 JSON 数据, 然后转成字典
- # 常用模块
- # 模块相关的知识
- import os
- # print(os.listdir(r'F:\PythonWorker\day4')) #获取文件夹内的所有文件名
- # os.path.isfile() #判断是否是文件
- # os.path.isdir() #判断是否是文件夹
- # os.mkdir('a') #创建文件夹
- # os.makedirs('b')
- # os.makedirs('cnz/b') #父目录不存在会创建
- # os.mkdir('cnz/a') #父目录不存会报错
- # os.rename('a','c') #重命名, a 重命名为 c
- # os.getcwd() #获取当前所在目录
- # os.chdir() #进入到哪个目录 操作文件在当前所进入的目录操作
- # os.cpu_count() #获取当前电脑 cup 个数
- # print(os.system('ipconfig')) #执行操作系统命令 只能执行系统命令, 不能获取结果
- # result = os.popen('ipconfig').read() #执行操作系统命令 只能执行系统命令, 能获取返回结果
- # print("输出结果 ---------------\n",result)
- # os.rmdir('123')# 删除文件夹 (删除空文件夹)
- # os.remove('a.txt') #删除文件
- # os.path.getsize() #获取文件大小
- # os.walk() #获取指定目录下所有文件, 包括文件夹内的文件
- # for cur_dir,dirs,files in os.walk(r'F:\PythonWorker\day4'):
- # print(cur_dir) #当前文件夹
- # print(dirs) #当前目录下的文件夹
- # print(files) #当前目录下的文件
- # 指定一个目录, 找到目录下有那些文件,.mp4 .avi
- # 1, 获取这个目录下有哪些文件 os.walk()
- # 2, 判断文件名是否 .mp4 .avi 结尾
- # def find_move(path):
- # for cur_dir, dirs, files in os.walk(path):
- # print('当前查找目录:%s' %cur_dir)
- # for file in files:
- # if file.endswith('.mp4') or file.endswith('.avi'):
- # print('目录:%s' %cur_dir)
- # break #找到存在此类文件输出当前目录后结束查找进入下一个目录
- # os.path.dirname() #获取父目录
- #
- # os.path.join('users','aa','a.txt') #拼接路径
- #
- # print(os.path.abspath('../day3')) #获取文件绝对路径
- # os.path.exists() #判断文件是否存在
- # print(os.path.split('F:\PythonWorker\day4')) #将路径与文件名分割, 以最后一个 \ 作为切割位置
- # os.path.getmtime() #获取文件的修改时间
- # os.path.getctime() #获取文件的创建时间
- # os.path.getatime() #获取最近一次访问时间
- # 时间戳 从计算机诞生的那一天到现在过了多少秒
- # 格式化好的时间
- import time
- # print(time.strftime('%Y-%m-%d %H:%M:%S')) #获取当前时间
- # print(time.time() + 60*60*24*365) #获取时间戳
- # 时间戳与格式化时间互相转换
- # 时间元组, 将时间戳转换为时间元组
- # time = time.time() + (60*60*24*365)
- # print(time.gmtime(time.time())) #gmtime 取标准时区
- # print(time.localtime(time.time()))
- # times = time.localtime(time.time())
- # print(time.strftime('%Y-%m-%d %H:%M:%S',times))
- # 格式化与时间戳互相转换
- # times = time.strptime('2019-09-13 18:33:55','%Y-%m-%d %H:%M:%S') #将格式化好的时间转换为时间元组
- # print(time)
- # print(time.mktime(times)) #把时间元组转化为时间戳
- # 格式化好的时间转时间戳的, 如果不传格式化好的时间, 就返回当前的时间戳
- # def get_timestamp(str = None,format='%Y-%m-%d %H:%M:%S'):
- # if str :
- # times = time.strptime(str, format)
- # return int(time.mktime(times))
- # return int(time.time())
- #
- # def timestamp_to_str(timestamp=None,format='%Y-%m-%d %H:%M:%S'):
- # '''这个是把时间戳转换成格式化好的实际, 如果不传时间戳, 那么就返回当前的时间'''
- # if timestamp:
- # time_tuple = time.localtime(timestamp) # 取本地时区的时间
- # return time.strftime(format, time_tuple)
- # return time.strftime(format)
来源: http://www.bubuko.com/infodetail-3100162.html