Python就业班——Python函数与模块
#!/usr/bin/env python3 # coding=utf-8 # Version:python3.6.1 import random from functools import reduce from datetime import datetime import os __date__ = '2020/6/26 23:48' __author__ = 'LGSP_Harold' # 序列传参 # def calc(a, b, c): # return (a + b) * c # # # lst = [1, 5, 10] # print(calc(*lst)) # ———————————————————————— # 字典传参 # def person(name, age, height): # print('{},{},{}'.format(name, age, height)) # # # param = {'name': 'Harold', 'age': 18, 'height': 180} # person(**param) # ———————————————————————— # 返回值包含多个数据 # def get_detail_info(): # dict1 = { # 'employee': [ # {'name': 'Jim', 'age': 18}, # {'name': 'Leslie', 'age': 18} # ], # 'device': [ # {'id': 999, 'title': '笔记本'}, # {'id': 9999, 'title': '台式机'} # ], # 'asset': [{},{}], # 'project': [{}, {}], # } # return dict1 # # # print(get_detail_info()) # d = get_detail_info() # print(type(d)) # name = d.get('employee')[1].get('name') # print(name) # ———————————————————————— # 包的引用 # 引入整个包:import module # 只引入所需要的属性和方法:from module.xx.xx import xx # 指定别名:from module.xx.xx import xx as rename # 引入所有:from module.xx.xx import * # __init__.py:一般为空,可以批量导入所需的模块 # ———————————————————————— # os模块 # 创建多级目录 # import os # f = os.path.join('.//imooc', 'i', 'love', 'you') # os.makedirs(f) # 获取目录下所有文件 # filePath = ".\\" # dirList = os.listdir(filePath) # print(dirList) # ———————————————————————— # 虚拟环境 # virtualenv # 安装:pip install virtualenv # 创建 # 新建虚拟环境目录:mkdir envs # 进入目录:cd envs # 创建所需的环境(如:django1.11) # 创建一个虚拟环境(Python):virtualenv django1.11 # 进入Scripts:cd django1.11/Scripts # 进入虚拟环境:activate # 安装所需的环境:pip install django==1.11 # 测试 # python # import django # quit() # 退出:deactivate.bat # 多个虚拟环境切换 # 安装virtualenvwrapper:pip install virtualenvwrapper-win # 设置环境变量: # 变量名:WORKON_HOME # 变量值:D:\ProgramFiles\Development\PythonEnvs\PythonVirtualenv # 使用(在django虚拟环境中):workon 虚拟环境名称 # 如:workon flask # pipenv # 安装:pip install pipenv # 安装需要的python版本 # 创建 # 新建虚拟环境目录:mkdir py27 # 进入目录:cd py27 # 了解pipenv:pipenv -h # 创建所需的Python环境:pipenv --python 2.7 # 进入虚拟环境:pipenv shell # 安装所需的环境(需要在前面新创建的目录【py27】内执行):pipenv install flask # 测试 # python # import flask # quit() # 退出:exit # ———————————————————————— # # 函数进阶 # # lambda函数 # def f(n): # return n % 2 != 0 # # # # filter函数:函数用于过滤序列,过滤掉不符合条件的元素,返回一个迭代器对象,如果要转换为列表,可以使用 list() 来转换。该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判,然后返回 True 或 False,最后将返回 True 的元素放到新列表中。 # def use_filer(l): # """ # 获取指定列表/元组中的奇数 # :param l: list/tuple要过滤的数据 # :return: 过滤好的奇数列表 # """ # # rest = filter(f, l) # rest = filter(lambda n: n % 2 != 0, l) # return rest # # # # map函数:第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。 # def pow_number(l): # """ # 根据给定的列表数据,计算里面每一项的立方 # :param l: list/type int类型的列表或者是元组 # :return:原来列表中每一项的立方 # """ # rest_list = [] # for x in l: # rest_list.append(x * x * x) # return rest_list # # # def map_f(n): # return n * n * n # # # def pow_num_use_map(l): # """ # 使用map函数计算给定列表的每一项立方 # :param l: list/type int类型的列表或者是元组 # :return:原来列表中每一项的立方 # """ # # return map(map_f, l) # # return map(lambda n: n * n * n, l) # return map(lambda n: pow(n, 3), l) # # # # reduce函数:函数将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给 reduce 中的函数 function(有两个参数)先对集合中的第 1、2 个元素进行操作,得到的结果再与第三个数据用 function 函数运算,最后得到一个结果。 # def get_sum(l): # rest = 0 # for i in l: # rest += i # return rest # # # def get_sum_use_py(l): # return sum(l) # # # def reduce_f(m, n): # return m + n # # # def get_sum_use_reduce(l): # return reduce(reduce_f, l) # # # def get_sum_use_reduce_lambda(l): # return reduce(lambda m, n: m + n, l) # 文件读写 # f = open('file.txt') # f.close() # # with open('file.txt') as f: # pass # read() # def read_file(): # file_name = 'test.txt' # file_path1 = 'D:\\ProgramFiles\\Development\\PycharmProjects\\practice\\test.txt' # file_path2 = 'D:/ProgramFiles/Development/PycharmProjects/practice/test.txt' # # f = open(file_name, encoding='utf-8') # # result = f.read() # # result = f.read(8) # # print(result) # # print(f.read(9)) # # f.seek(20) # # print(f.read(9)) # # # print(f.readline(9)) # # print(f.readline(9)) # # print(f.readline()) # # # rest = f.readlines() # # print(rest) # # print(len(rest)) # # for i in rest: # # print(i) # # # rest = f.readlines(19) # 参数为字符数 # # for i in rest: # # print(i) # # f.close() # # with open(file_path1, encoding='utf-8') as f: # pass # 文件写入 # def test_write_file(): # file_name = 'write_test.txt' # with open(file_name, 'w', encoding='utf-8') as f: # f.write('hello') # f.write('\n') # f.write('world') # def write_mult_line(): # file_name = 'write_mult_line.txt' # with open(file_name, 'w', encoding='utf-8') as f: # l = ['第1行', '\n', '第2行', '\r\n', '第3行', '\r', '第4行'] # f.writelines(l) # 登录日志 # def write_user_log(): # file_name = 'user_log.txt' # rest = 'id:{0}-访问时间:{1}'.format(random.randint(1000, 9999), datetime.now()) # print(rest) # with open(file_name, 'a', encoding='utf-8') as f: # f.write(rest + '\n') # 先读后写 # def read_and_write(): # file_name = 'read_and_write.txt' # with open(file_name, 'r+', encoding='utf-8') as f: # read_rest = f.read() # if '1' in read_rest: # f.write('ccc\n') # else: # f.write('aaa\n') if __name__ == '__main__': # # filter函数 # filter_list = [1, 2, 3, 4, 5, 6] # rest = use_filer(filter_list) # print(list(rest)) # # # map函数 # map_list = [1, 2, 3, 4, 5, 6] # rest = pow_number(map_list) # print(rest) # # rest_map = pow_num_use_map(map_list) # # print(list(rest_map)) # rest_map_lambda = pow_num_use_map(map_list) # print(list(rest_map_lambda)) # # reduce函数 # reduce_list = [1, 2, 3, 4] # print(get_sum(reduce_list)) # print(get_sum_use_py(reduce_list)) # print(get_sum_use_reduce(reduce_list)) # print(get_sum_use_reduce_lambda(reduce_list)) # read_file() # test_write_file() # write_mult_line() # write_user_log() # read_and_write() pass
略懂,略懂....