1. 写一个生成式能够产生的数据为: 1, 4, 27, 256, 3125,…, 9**9
print({'{}**{}'.format(n, n): n ** n for n in range(1, 10)})
# {'1**1': 1, '2**2': 4, '3**3': 27, '4**4': 256, '5**5': 3125, '6**6': 46656, '7**7': 823543,'8**8': 16777216, '9**9': 387420489}
2. 写一个生成式能够产生1-10中所有半径是偶数的圆的面积
from math import pi
print({'r={}时圆的面积'.format(n): int(pi * n ** 2 * 100) / 100 for n in range(2, 11, 2)})
# {'r=2时圆的面积': 12.56, 'r=4时圆的面积': 50.26, 'r=6时圆的面积': 113.09, 'r=8时圆的面积': 201.06, 'r=10时圆的面积': 314.15}
3. 写一个生成式交换字典的键和值,产生一个新的字典
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {dict1[key]: key for key in dict1}
print(dict2) # {1: 'a', 2: 'b', 3: 'c'}
4. 为函数写一个装饰器,在函数执行之后输出 after
def after(fn):
def do(*args, **kwargs):
fn(*args, **kwargs)
print('after')
return do
@after
def func1():
print('函数执行...')
func1()
5. 为函数写一个装饰器,把函数的返回值 +100 然后再返回。
def add_result(fn):
def add(*args, **kwargs):
re = fn(*args, **kwargs)
return re + 100
return add
@add_result
def sum1(x, y):
return x + y
print(sum1(100, 200)) # 400
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 do(*args, **kwargs):
re = fn(*args, **kwargs)
return ''.join(['<p>', str(re), '</p>'])
return do
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=None):
def decorator(fn):
def do(*args, **kwargs):
re = fn(*args, **kwargs)
return '<{}>{}</{}>'.format(name, str(re), name)
return do
return decorator
8. 为函数写一个装饰器,根据参数不同做不同操作。
flag为True,则 让原函数执行后返回值加100,并返回。
flag为False,则 让原函数执行后返回值减100,并返回。
def do_flag(flag=False):
def decorator(fn):
def do(*args, **kwargs):
re = fn(*args, **kwargs)
if flag:
return re + 100
else:
return re - 100
return do
return decorator
@do_flag(flag=True)
def sum2(x, y):
return x + y
@do_flag(flag=False)
def sum3(x, y):
return x + y
print(sum2(100, 150)) # 350
print(sum3(100, 150)) # 150
9. 写一个斗地主发牌器
def set_cards(fn):
def do(*args, **kwargs):
re = fn(*args, **kwargs)
# 0-12 13-25 26-38 39-51
colors = ['♥️', '♠️', '♦️', '♣️']
nums = ['3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A', '2']
for part_cards in re:
for index in range(len(part_cards)):
if part_cards[index] == 52:
part_cards[index] = 'Black Joker'
elif part_cards[index] == 53:
part_cards[index] = 'Red Joker'
else:
# 按花色排
# part_cards[index] = colors[part_cards[index] // 13] + nums[part_cards[index] % 13]
# 按大小排
part_cards[index] = colors[part_cards[index] % 4] + nums[part_cards[index] // 4]
deal_list = {'top card': re[0], 'player1 card': re[1], 'player2 card': re[2], 'player3 card': re[3]}
return deal_list
return do
@set_cards
def fight_against_landlords():
cards_list = [i for i in range(54)]
# 洗牌
shuffle(cards_list)
# print(cards_list)
top_card = cards_list[:3]
p1 = sorted(cards_list[3:20])
p2 = sorted(cards_list[20:37])
p3 = sorted(cards_list[37:])
part_sort_cards = [top_card, p1, p2, p3]
return part_sort_cards
cards = fight_against_landlords()
for key in cards:
print(key, cards[key])
# top card ['♣️7', '♦️9', '♠️A']
# player1 card ['♥️3', '♠️3', '♣️3', '♠️4', '♦️5', '♣️5', '♣️6', '♦️7', '♥️8', '♦️8', '♣️8', '♠️9', '♣️9', '♣️10', '♠️Q', '♥️A', '♥️2']
# player2 card ['♦️3', '♠️5', '♥️6', '♠️6', '♦️6', '♥️7', '♠️7', '♥️9', '♥️10', '♦️10', '♥️J', '♥️Q', '♥️K', '♠️K', '♦️K', '♠️2', 'Black Joker']
# player3 card ['♥️4', '♦️4', '♣️4', '♥️5', '♠️8', '♠️10', '♠️J', '♦️J', '♣️J', '♦️Q', '♣️Q', '♣️K', '♦️A', '♣️A', '♦️2', '♣️2', 'Red Joker']