import time
def cal(l):
start_time = time.time()
time.sleep(0.5)
res = 0
for i in l:
res+=i
stop_time = time.time()
print('函数运行时间是%s'%(stop_time-start_time))
return res
num = cal(range(100))
print(num)
#高阶函数: 函数接受的参数是一个函数名;函数的返回值是一个函数名;满足任意一个条件都可以称之为高阶函数。
# def foo():
# print('hello world')
#
# def test(func):
# print(func)
# func()
#
# test(foo)
# def foo():
# print('from the foo')
# def test(func):
# return func
#
# foo = test(foo)
# print(foo())
#函数嵌套
#在函数内部调用另一个函数
def father(name):
#print('from father %s'%name)
def son():
print('son.....')
son()
print(locals())
father('alex')
def timmer(func):
def wrapper(*args,**kwargs):
start_time = time.time()
res = func(*args,**kwargs)
end_time = time.time()
print('运行时间是%s' %(end_time-start_time))
return res
return wrapper
#@timmer相当于test=timmer(test)
@timmer
def test(name,age):
time.sleep(3)
print('test函数运行完毕,Name:%s,Age:%d' %(name,age))
return 'test的返回值'
test('fengzi',18)
import time
#时间戳
print(time.time())
#结构化时间
print(time.localtime(time.time()))
t=time.localtime()
print(t.tm_year)
#参数时间不传默认当前时间
#----标准时间,差8小时
print(time.gmtime())
#---结构化时间转化为时间戳
print(time.mktime(time.localtime()))
#-----将结构化时间转化为字符串时间
print(time.strftime('%Y-%m-%d %X',time.localtime()))
#-----将字符串时间转化为结构化时间
print(time.strptime('2018-02-28 09:44:05','%Y-%m-%d %X'))
print(time.asctime())#Wed Feb 28 09:50:35 2018
print(time.ctime())#Wed Feb 28 09:50:35 2018
import datetime
print(datetime.datetime.now())#2018-02-28 09:53:20.083877
import random
ret = random.random()#随机数
ret = random.randint(1,3)
ret = random.randrange(1,3)
ret = random.choice([1,2,33])
ret = random.sample([1,2,3,4],2)
ret = random.uniform(1,3)
item = [1,2,3,4,5,6]
ret = random.shuffle(item)#打乱顺序
print(item)
import json
#
# dic = {"name":"alex"}
#
# data = json.dumps(dic)
# print(data)
# print(type(data))
# f=open('new_hello','w')
# f.write(data)
f_read = open('new_hello','r')
data = json.loads(f_read.read())
print(data)
print(type(data))
print(data['name'])
#json.dump(dic,f)#文件处理的时候使用
#json.load(f)
# import cal,time
# from cal import add
# from cal import sub
# from cal import * #不推荐
# print(add(3,5))
# print(sub(5,1))
# from my-module import cal
#
# print(cal.add(1,3))
# from my-module import cal
# print(cal.add(3,4))
print(__name__)
import shelve
f = shelve.open(r'shelve')#目的:将一个字典放入文本
# f['stu1_info'] = {'name':'alex','age':'18'}
# f['stu2_info'] = {'name':'xxx','age':'20'}
# f['scholl_info'] = {'website':'oldboyedu.com','city':'beijing'}
#
# f.close()
print(f.get('stu1_info')['age'])
网友评论