python学习day11

一:迭代器2 & 装饰器

#codeing:UTF-8
#__author__:Duke
#date:2018/3/10/010

# 今天讲迭代器

# 生成器都是迭代器,迭代器不一定是生成器
l = [1,2,3,4]
data = iter(l)
print(data)  # <list_iterator object at 0x000002ABBF1E9780>
#什么是迭代器?   1  有iter方法   2 有next方法


#练习  找出文件中,最长的一行
file = open("data","r",encoding="UTF-8")
maxlength_data = ''
def max_line():
    while True:
        data_line = file.readline().strip()
        print(data_line)
        if not data_line:
            break
        global maxlength_data
        if len (data_line) > len (maxlength_data):
            maxlength_data = data_line
        else:
            pass
        yield
for i in max_line():
    None
print("==========")
print(maxlength_data)
file.close()

 二:time 模块

# time 模块
import time

print(time.time())   #获取当前的时间戳  *****
print(help(time))    #获取模块的帮助信息
print(time.clock())    #计算CPU的执行时间
print(time.sleep(3))    #睡眠时间  *******
print(time.gmtime()) # UTC 世界标准时间   #time.struct_time(tm_year=2018, tm_mon=3, tm_mday=10, tm_hour=12, tm_min=6, tm_sec=56, tm_wday=5, tm_yday=69, tm_isdst=0)
# UTC 世界标准时间
print(time.localtime())    #本地时间
struct_time = time.localtime()
print(time.strftime("%Y-%m-%d  %H:%M:%S",struct_time))    #时间的格式化输出
print(time.strptime('2016-09-08 18:48:35','%Y-%m-%d  %H:%M:%S'))   #将时间赋值到变量
#同时也是将时间结构化
print(time.ctime())   #不能自定义格式方式的 获取当前时间的方式
print(time.ctime(0))   #打印时间戳的详细时间
print(time.mktime(time.localtime()))   #  结构化时间转化为时间戳

 

三: random 模块

#codeing:UTF-8
#__author__:Duke
#date:2018/3/14/014 19:29
import random
print(help(random))
print(random.random())    #打印一个随机数  in [0.1)
print(random.randint(1,8))  #包括右边的数
print(random.choice('hello'))  #随机选择字符串中的字符
print(random.choice(['123',45,'ok']))  #随机选择字符串中的字符
print(random.choices(['123',45,'ok',3,4,5,7]))  #
print(random.sample(['123',45,'ok',3,4,5,7],2))  #随机选多个数
print(random.randrange(1,7))   #不包括最后一个数

# random 模块应用 随机数的生成 def v_code(): code = '' for i in range(6): x = random.choice([1,2,3]) if x == 1: code_num = random.randrange(0,10) code += str(code_num) elif x == 2: code_small_word = random.randrange(65,91) code += chr(code_small_word) else: code_big_word = random.randrange(97,123) code += chr(code_big_word) return code print(v_code())

 

posted @ 2018-03-14 20:14  Duke777  阅读(156)  评论(0编辑  收藏  举报