1.寫一個(gè)生成式能夠產(chǎn)生的數(shù)據(jù)為: 1, 4, 27, 256, 3125,…, 9**9
nums = [(i ** i) for i in range(1, 10)]
print(nums)
# [1, 4, 27, 256, 3125, 46656, 823543, 16777216, 387420489]
2.寫一個(gè)生成式能夠產(chǎn)生1-10中所有半徑是偶數(shù)的圓的面積
area = [(i * 3.14 * i) for i in range(1, 11) if i % 2 == 0]
print(area)
# [12.56, 50.24, 113.03999999999999, 200.96, 314.0]
3.寫一個(gè)生成式交換字段的鍵和值,產(chǎn)生一個(gè)新的字典
dict1 = {'s': 'b1', '2': 'b2', 'g': 'k3'}
dict2 = dict(((dict1[key], key) for key in dict1))
print(dict2)
# {'b1': 's', 'b2': '2', 'k3': 'g'}
4.為函數(shù)寫一個(gè)裝飾器,在函數(shù)執(zhí)行之后輸出 after
def after(fn):
def test(*args, **kwargs):
fn(*args, **kwargs)
print('after')
return test
@after
def ss():
print('ss函數(shù)')
ss()
5.為函數(shù)寫一個(gè)裝飾器凰棉,把函數(shù)的返回值 +100 然后再返回
def add_100(fn):
def test(*args, **kwargs):
return fn(*args, **kwargs) + 100
return test
@add_100
def f1(x):
return x
print(f1(10))
6.寫一個(gè)裝飾器@tag要求滿足如下功能:
@tag
def render(text):
# 執(zhí)行其他操作
return text
@tag
def render2():
return 'abc'
print(render('Hello')) # 打印出: <p>Hello</p>
print(render2()) # 打印出: <p>abc</p>
def tag(fn):
def test(*args, **kwargs):
str1 = str(fn(*args, **kwargs))
return '<p>' + str1 + '</p>'
return test
@tag
def render(text):
# 執(zhí)行其他操作
return text
@tag
def render2():
return 'abc'
print(render('Hello'))
# <p>Hello</p>
print(render2())
# <p>abc</p>
寫一個(gè)裝飾器@tag要求滿足如下功能
(需要使用帶參的裝飾器,自己先自學(xué)一下):
"""
@tag(name='p')
def render(text):
# 執(zhí)行其他操作
return text
@tag(name='div')
def render2():
return 'abc'
print(render('Hello')) # 打印出: <p>Hello</p>
print(render2()) # 打印出: <div>abc</div>
"""
def tag(name=''):
def test0(fn):
def test(*args, **kwargs):
str2 = str(fn(*args, **kwargs))
return '<%s>' % name + str2 + '</%s>' % name
return test
return test0
@tag(name='p')
def render(text):
# 執(zhí)行其他操作
return text
@tag(name='div')
def render2():
return 'abc'
print(render('Hello'))
# <p>Hello</p>
print(render2())
# <div>abc</div>
為函數(shù)寫一個(gè)裝飾器行冰,根據(jù)參數(shù)不同做不同操作。
flag為True伶丐,則 讓原函數(shù)執(zhí)行后返回值加100悼做,并返回。
flag為False哗魂,則 讓原函數(shù)執(zhí)行后返回值減100肛走,并返回。
def add_difer(flag):
if flag == True:
def add(fn):
def test(*args, **kwargs):
return int(fn(*args, **kwargs)) + 100
return test
return add
if flag == False:
def differ(fn):
def test(*args, **kwargs):
return int(fn(*args, **kwargs)) - 100
return test
return differ
@add_difer(flag=True)
def f1(x):
return x
print(f1(10))
@add_difer(flag=False)
def f2(x):
return x
print(f2(10))
寫一個(gè)斗地主發(fā)牌器
import random
# 1)創(chuàng)建一副牌
def new_poker():
"""創(chuàng)建一副新牌"""
pokers = []
nums = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
colors = ['??', '??', '??', '??']
for num in nums:
for color in colors:
pokers.append('%s%s' % (color, num))
pokers.extend(['小王', '大王'])
return pokers
def shuffle(pokers):
"""洗牌"""
random.shuffle(pokers)
def deal(pokers):
poker_iter = iter(pokers)
p1 = []
p2 = []
p3 = []
num = 1
for _ in range(17*3):
if num == 1:
p1.append(next(poker_iter))
elif num == 2:
p2.append(next(poker_iter))
elif num == 3:
p3.append(next(poker_iter))
num = 0
num += 1
return p1, p2, p3, list(poker_iter)
pokers1 = new_poker()
shuffle(pokers1)
# print(pokers1)
p1, p2, p3, hole = deal(pokers1)
print(p1)
print(p2)
print(p3)
print(hole)
nums = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
colors = ['??', '??', '??', '??']
pokers1 = list('%s%s' % (num, color) for num in nums for color in colors)
# print('===:', pokers1)