1.写一个生成式能够产生的数据为: 1, 4, 27, 256, 3125,…, 9**9
print(list(x ** x for x in range(1,10)))
2.写一个生成式能够产生1-10中所有半径是偶数的圆的面积
import math
print(list(math.pi * (x ** 2) for x in range(1,11) if not(x % 2)))
3.写一个生成式交换字段的键和值,产生一个新的字典
dict1 = {'a':1, 'b':2, 'c':3}
print(dict((dict1[key], key) for key in dict1))
4.为函数写一个装饰器,在函数执行之后输出 after
def add_after(fn):
def func(*args, **kwargs):
fn(*args, **kwargs)
print('after')
return func
@add_after
def test():
print('=====')
test()
5.为函数写一个装饰器,把函数的返回值 +100 然后再返回
def add_hundred(fn):
def func(*args, **kwargs):
return fn(*args, **kwargs) +100
return func
@add_hundred
def test_sum(x, y):
return x + y
print(test_sum(10, 20))
6.写一个装饰器@tag要求满足如下功能:
@tag
def render(text):
# 执行其他操作
return text
@tag
def render2():
return 'abc'
print(render('Hello')) # 打印出: <p>Hello</p>
print(render2()) # 打印出: <p>abc</p>
def tag(fn):
def func(*args, **kwargs):
return '<p>' + fn(*args, **kwargs) + '</p>'
return func
@tag
def render(text):
return text
@tag
def render2():
return 'abc'
print(render('Hello'))
print(render2())
7.写一个装饰器@tag要求满足如下功能:
@tag(name='p')
def render(text):
# 执行其他操作
return text
@tag(name='div')
def render2():
return 'abc'
print(render('Hello')) # 打印出: <p>Hello</p>
print(render2()) # 打印出: <div>abc</div>
def tag(name):
def adorn(fn):
def func(*args, **kwargs):
return '<' + name + '>' + fn(*args, **kwargs) + '</' + name + '>'
return func
return adorn
@tag(name='p')
def render(text):
return text
@tag(name='div')
def render2():
return 'abc'
print(render('Hello'))
print(render2())
8.为函数写一个装饰器,根据参数不同做不同操作。
flag为True,则 让原函数执行后返回值加100,并返回。
flag为False,则 让原函数执行后返回值减100,并返回。
def tag(flag:bool):
def adorn(fn):
def func(*args, **kwargs):
if flag:
return fn(*args, **kwargs) + 100
else:
return fn(*args, **kwargs) - 100
return func
return adorn
@tag(flag=True)
def sum(x, y):
return x + y
print(sum(10, 20))
@tag(flag=False)
def sum(x, y):
return x + y
print(sum(100,200))
9.写一个斗地主发牌器
import random
from collections import defaultdict
poker = {'hearts':['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'],
'spade':['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'],
'cube':['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'],
'plum_blossom':['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'],
'king':['large','little']
}
framer_a = defaultdict(list)
framer_b = defaultdict(list)
landlord = defaultdict(list)
number = 54
order = 0
while number > 3:
color = random.choice(list(poker.keys()))
count = random.randint(0,len(poker[color])-1)
i = order % 3
if i == 0:
landlord[color] .append(poker[color][count])
elif i == 1:
framer_a[color] .append(poker[color][count])
else:
framer_b[color] .append(poker[color][count])
del poker[color][count]
if len(poker[color]) == 0:
poker.pop(color)
number -= 1
order += 1
end_poker = poker.copy()
for x in poker:
for i in range(len(poker[x])):
landlord[x].append(poker[x][i])
print('农民1:',dict(framer_a))
print('农民2:',dict(framer_b))
print('地主:',dict(landlord))
print('底牌:',dict(end_poker))