第二模块考核(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-基础函数与常用模块考核的更多相关文章

  1. 第六章:Python基础の反射与常用模块解密

    本课主题 反射 Mapping 介绍和操作实战 模块介绍和操作实战 random 模块 time 和 datetime 模块 logging 模块 sys 模块 os 模块 hashlib 模块 re ...

  2. Python基础学习之常用模块

    1. 模块 告诉解释器到哪里查找模块的位置:比如sys.path.append('C:/python') 导入模块时:其所在目录中除源代码文件外,还新建了一个名为__pycache__ 的子目录,这个 ...

  3. python基础,函数,面向对象,模块练习

    ---恢复内容开始--- python基础,函数,面向对象,模块练习 1,简述python中基本数据类型中表示False的数据有哪些? #  [] {} () None 0 2,位和字节的关系? # ...

  4. Python基础-函数参数

    Python基础-函数参数 写在前面 如非特别说明,下文均基于Python3 摘要 本文详细介绍了函数的各种形参类型,包括位置参数,默认参数值,关键字参数,任意参数列表,强制关键字参数:也介绍了调用函 ...

  5. python基础——函数的参数

    python基础——函数的参数 定义函数的时候,我们把参数的名字和位置确定下来,函数的接口定义就完成了.对于函数的调用者来说,只需要知道如何传递正确的参数,以及函数将返回什么样的值就够了,函数内部的复 ...

  6. python基础—函数嵌套与闭包

    python基础-函数嵌套与闭包 1.名称空间与作用域 1 名称空间分为: 1 内置名称空间   内置在解释器中的名称 2 全局名称空间   顶头写的名称 3 局部名称空间 2 找一个名称的查找顺序: ...

  7. python基础—函数装饰器

    python基础-函数装饰器 1.什么是装饰器 装饰器本质上是一个python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能. 装饰器的返回值是也是一个函数对象. 装饰器经常用于有切 ...

  8. Python学习—基础篇之常用模块

    常用模块 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要 ...

  9. python基础===正则表达式,常用函数re.split和re.sub

    sub的用法: >>> rs = r'c..t' >>> re.sub(rs,'python','scvt dsss cvrt pocdst') 'scvt dss ...

随机推荐

  1. Spring BOOT的学习笔记

    1,静态文件夹src/main/resources/static下的,图片必须放在images文件夹下才能访问,直接放在static下不能访问 2,配置热部署,否则修改下Html,图片都得重启 htt ...

  2. 【Oracle】ORA-14400: 插入的分区关键字未映射到任何分区

    问题描述: 工作中使用kettle将原始库中的数据抽取到标准库中,在抽取过程中报错:[ORA-14400: 插入的分区关键字未映射到任何分区]/[ORA-14400: inserted partiti ...

  3. java socket 基础操作

    服务端: public class Server { public static void main(String[] args) throws Exception { //1.创建一个服务器端Soc ...

  4. CleanMyMac X教程之-安装卸载

    Mac清理软件CleanMyMac X的出现成功的吸引了Mac用户的注意,CleanMyMac X是2018年发布的,深受许多Mac用户的青睐.windows操作端有360等众多清洁软件,那么Mac端 ...

  5. 在linux中输出每个group的用户成员

    先提供使用文件一步一步获取相关信息: 1. 获取所有的用户: awk -F: '{print $1 > "1.txt"}' /etc/passwd 2. 获取每个用户, 及其 ...

  6. myeclipse2018安装教程(附下载地址)

    这两天在网上找myeclipse2018的安装包和破解工具,很分散,并且破解失败很多,现在总结下 myeclipse2018下载地址: https://pan.baidu.com/s/1NV7cAsN ...

  7. [UE4]让箭头保持水平的第二种方法:Combinrotators、Delta(Rotator)

    一.手柄在世界坐标系中有一个绝对朝向,我们可以知道箭头相对于手柄的朝向,相对于手柄的旋转角度. 可以通过手柄绝对朝向.箭头的相对于手柄的朝向计算得到箭头的绝对朝向. 在得到箭头的相对于手柄的角度,在这 ...

  8. PVID和VID彻底研究(上) ——PVID的作用及和VID的区别

    http://blog.csdn.net/cybertan/article/details/8348752 另外一篇 netgear的官方文档: http://club.netgear.cn/Know ...

  9. mysql通过now()获取的时间不对

    先用now()获取系统时间,发现时间不对(差8个小时): mysql> select now(); +---------------------+ | now() | +------------ ...

  10. BAT开发中,ChromeDriver版本兼容性检查

    打开解决方案的Nuget包管理器,选择合适的版本,安装即可.版本的兼容性检查,见上一篇blog(初次使用BAT,请检查Chrome浏览器和ChromeDriver兼容性 https://www.cnb ...