Python-基础函数与常用模块考核
第二模块考核(2019/ 03/ 03)
### 第一模块内容
1.请写出 “路飞学城alex” 分别用utf - 8和gbk编码所占的位数(口述)
➜ ~ python3
>>> bytes("你", "gbk")
b'\xc4\xe3'
>>> bytes("a", "gbk")
b'a'
>>> bytes("你", "utf-8")
b'\xe4\xbd\xa0'
>>> bytes("a", "utf-8")
b'a'
2.python有哪几种数据类型,分别什么?
可变:Number,String,Tuple
不可变:List,Dictionary,Set
### 第二模块内容
1.创建一个闭包函数需要满足哪几点。(口述)
有内嵌函数,引用一个定义在闭合范围内的变量,外部函数必须返回内嵌函数
########## 闭包的原理解释
>>> def make_printer(msg1, msg2):
def printer():
print msg1, msg2
return printer
>>> printer = make_printer('Foo', 'Bar') # 形成闭包 >>> printer.__closure__ # 返回cell元组
(<cell at 0x03A10930: str object at 0x039DA218>, <cell at 0x03A10910: str object at 0x039DA488>) >>> printer.__closure__[0].cell_contents # 第一个外部变量
'Foo'
>>> printer.__closure__[1].cell_contents # 第二个外部变量
'Bar'
2.序列化模块json,xml,pickle的区别是什么?(口述)
参考文章:https://www.cnblogs.com/qing-add/p/5225048.html
3.迭代器和生成器的区别, 在python中它们的原理是什么。(口述)
参考文章:http://python.jobbole.com/87805/
http://www.runoob.com/python3/python3-iterator-generator.html
https://www.cnblogs.com/wj-1314/p/8490822.html
https://www.cnblogs.com/cicaday/p/python-decorator.html
4.解释一下包和模块的含义。 (口述)
参考文章:https://www.cnblogs.com/935415150wang/p/7091227.html
5.请阐述一下代码含义
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
6.解释以下代码含义 (口述)
from functools import reduce
reduce(lambda x, y: x + y, range(10))
7. 字符串“Luffy”,将小写字母全部转换成大写字母,将大写字幕转换成小写字母。(编程)
word = "Luffy"
new_word = word.swapcase()
print("new_word:",new_word)
str = "www.runoob.com"
print(str.upper()) # 把所有字符中的小写字母转换成大写字母
print(str.lower()) # 把所有字符中的大写字母转换成小写字母
print(str.capitalize()) # 把第一个字母转化为大写字母,其余小写
print(str.title()) # 把每个单词的第一个字母转化为大写,其余小写
8. 编写装饰器,为每个函数加上统计运行时间的功能。(编程)
import time
def timmer(func):
def inner():
star_time = time.time()
func()
wait_time = time.time() - star_time
print("%s运行时间为:%s" %(func.__name__,wait_time))
return inner
date = time.localtime() @timmer
def log_1():
print(date,date.tm_year,date.tm_mon) log_1()
import time
def timmer(func):
def wrapper(*args,**kwargs):
start= time.time()
func(*args,**kwargs)
stop = time.time()
print('执行时间是%s'%(stop-start))
return wrapper
@timmer
def exe():
print('你愁啥!')
exe()
import time
def timmer(func): def inner():
start_time = time.time()
func()
wait_time = time.time() - start_time
print("%s 运行时间:" % func.__name__, wait_time)
return inner a = time.localtime() @timmer
def log_1():
print('%s-%s-%s'%(a.tm_year, a.tm_mon, a.tm_mday))
@timmer
def log_2():
time.sleep(2)
print('%s-%s-%s' % (a.tm_year, a.tm_mon, a.tm_mday))
@timmer
def log_3():
time.sleep(4)
print('%s-%s-%s' % (a.tm_year, a.tm_mon, a.tm_mday))
log_1()
log_2()
log_3()
"""
2018-3-21
log_1 运行时间: 3.0994415283203125e-05
2018-3-21
log_2 运行时间: 2.0049030780792236
2018-3-21
log_3 运行时间: 4.004503965377808
"""
9. 递归实现斐波那契函数。(编程)
list = []
def fib(max):
n,a,b = 0,0,1
while n < max:
a,b = b,a+b
#yield b
n = n+1
list.append(b)
#print(b)
#return 'done'
num = fib(5)
print(list)
'''
print(next(num))
print(next(num))
print(next(num))
'''
10. random模块,写一个包含大小写字母和数字的6位随机验证码。(编程)
import random
import string wrong_word = "".join(random.sample('a-z'+'A-Z'+'0-9',6))
right_word = "".join(random.sample(string.ascii_uppercase+string.ascii_lowercase+string.digits,6))print(right_word )
print(wrong_word )
print(wrong_word )
其他同学的模块考核总结:https://www.cnblogs.com/wj-1314/p/8534245.html
扩展
【闭包实现快速给不同项目记录日志】
import logging
def log_header(logger_name):
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s [%(name)s] %(levelname)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
logger = logging.getLogger(logger_name) def _logging(something,level):
if level == 'debug':
logger.debug(something)
elif level == 'warning':
logger.warning(something)
elif level == 'error':
logger.error(something)
else:
raise Exception("I dont know what you want to do?" )
return _logging project_1_logging = log_header('project_1') project_2_logging = log_header('project_2') def project_1(): #do something
project_1_logging('this is a debug info','debug')
#do something
project_1_logging('this is a warning info','warning')
# do something
project_1_logging('this is a error info','error') def project_2(): # do something
project_2_logging('this is a debug info','debug')
# do something
project_2_logging('this is a warning info','warning')
# do something
project_2_logging('this is a critical info','error') project_1()
project_2() ---------------------
作者:chaseSpace-L
来源:CSDN
原文:https://blog.csdn.net/sc_lilei/article/details/80464645
版权声明:本文为博主原创文章,转载请附上博文链接!
Python-基础函数与常用模块考核的更多相关文章
- 第六章:Python基础の反射与常用模块解密
本课主题 反射 Mapping 介绍和操作实战 模块介绍和操作实战 random 模块 time 和 datetime 模块 logging 模块 sys 模块 os 模块 hashlib 模块 re ...
- Python基础学习之常用模块
1. 模块 告诉解释器到哪里查找模块的位置:比如sys.path.append('C:/python') 导入模块时:其所在目录中除源代码文件外,还新建了一个名为__pycache__ 的子目录,这个 ...
- python基础,函数,面向对象,模块练习
---恢复内容开始--- python基础,函数,面向对象,模块练习 1,简述python中基本数据类型中表示False的数据有哪些? # [] {} () None 0 2,位和字节的关系? # ...
- Python基础-函数参数
Python基础-函数参数 写在前面 如非特别说明,下文均基于Python3 摘要 本文详细介绍了函数的各种形参类型,包括位置参数,默认参数值,关键字参数,任意参数列表,强制关键字参数:也介绍了调用函 ...
- python基础——函数的参数
python基础——函数的参数 定义函数的时候,我们把参数的名字和位置确定下来,函数的接口定义就完成了.对于函数的调用者来说,只需要知道如何传递正确的参数,以及函数将返回什么样的值就够了,函数内部的复 ...
- python基础—函数嵌套与闭包
python基础-函数嵌套与闭包 1.名称空间与作用域 1 名称空间分为: 1 内置名称空间 内置在解释器中的名称 2 全局名称空间 顶头写的名称 3 局部名称空间 2 找一个名称的查找顺序: ...
- python基础—函数装饰器
python基础-函数装饰器 1.什么是装饰器 装饰器本质上是一个python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能. 装饰器的返回值是也是一个函数对象. 装饰器经常用于有切 ...
- Python学习—基础篇之常用模块
常用模块 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要 ...
- python基础===正则表达式,常用函数re.split和re.sub
sub的用法: >>> rs = r'c..t' >>> re.sub(rs,'python','scvt dsss cvrt pocdst') 'scvt dss ...
随机推荐
- Yii2 设计模式——工厂方法模式
工厂方法模式 模式定义 工厂方法模式(Factory Method Pattern)定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个.工厂方法让类吧实例化推迟到子类. 什么意思?说起来有这么 ...
- redux源码解读(一)
redux 的源码虽然代码量并不多(除去注释大概300行吧).但是,因为函数式编程的思想在里面体现得淋漓尽致,理解起来并不太容易,所以准备使用三篇文章来分析. 第一篇,主要研究 redux 的核心思想 ...
- 18.11 ROM、RAM、DRAM、SRAM和FLASH区别
ROM(Read Only Memory)和RAM(Random Access Memory)指的都是半导体存储器.ROM在系统停止供电的时候仍然可以保持数据,而RAM通常都是在掉电之后就丢失数据,但 ...
- python selenium-webdriver 定位frame中的元素 (十三)
定位元素时经常会出现定位不到元素,这时候我们需要观察标签的上下文,一般情况下这些定位不到的元素存放在了frame或者放到窗口了,只要我们切入进去就可以很容易定位到元素. 处理frame时主要使用到sw ...
- Python2.X和Python3.X的w7同时安装使用
一.介绍 Python([ˈpaɪθən])是一种面向对象.解释型计算机程序设计语言.Python语法简洁.清晰,具有丰富和强大的类库. Python源代码遵循GPL(GNU General Publ ...
- [例子]Ubuntu虚拟机设置固定IP上网
宿主机器 win7 linux Ubuntu 14.04 LTS 参考: Linux系列:Ubuntu虚拟机设置固定IP上网(配置IP.网关.DNS.防止resolv.c ...
- vue.js安装过程(npm安装)
一.开发环境 vue推荐开发环境: Node.js: JavaScript运行环境(runtime),不同系统直接运行各种编程语言 npm: Nodejs下的包管理器. webpack: 它主要的用途 ...
- VS2017编译GDAL(64bit)+解决C#读取Shp数据中文路径的问题
编译GDAL过程比较繁琐,查阅了网上相关资料,同时通过实践,完成GDAL的编译,同时解决了SHP数据中文路径及中文字段乱码的问题,本文以“gdal-2.3.2”版本为例阐述整个编译过程. 一.编译准备 ...
- Java核心-多线程-并发控制器-Exchanger交换器
1.基本概念 Exchanger,从名字上理解就是交换.Exchanger用于在两个线程之间进行数据交换,注意也只能在两个线程之间进行数据交换. 线程会阻塞在Exchanger的exchange方法上 ...
- 知识点:Mysql 数据库索引优化实战(4)
知识点:Mysql 索引原理完全手册(1) 知识点:Mysql 索引原理完全手册(2) 知识点:Mysql 索引优化实战(3) 知识点:Mysql 数据库索引优化实战(4) 一:插入订单 业务逻辑:插 ...