- 寫一個生成式能夠產(chǎn)生的數(shù)據(jù)為: 1, 2, 27, 256, 3125,…, 9**9
gen = (n**n for n in range(1, 10))
for n in range(9):
print(next(gen), end=' ') # 1 4 27 256 3125 46656 823543 16777216 387420489
- 寫一個生成式能夠產(chǎn)生1-10中所有半徑是偶數(shù)的圓的面積
import math
gen_s = (math.pi*(r**2) for r in range(1, 11) if not r & 1)
for n in range(5):
print(next(gen_s), end=' ')
# 12.566370614359172 50.26548245743669 113.09733552923255 201.06192982974676 314.1592653589793
- 寫一個生成式交換字段的鍵和值某饰,產(chǎn)生一個新的字典
dic1 = {'sno': 2, 'age': 22}
gen_dic = dict((dic1[key], key) for key in dic1)
print(gen_dic) # {2: 'sno', 22: 'age'}
- 為函數(shù)寫一個裝飾器,在函數(shù)執(zhí)行之后輸出 after
def p_after(fn):
def test(*args, **kwargs):
fn(*args, **kwargs)
print('after')
return test
@p_after
def func1():
print('這是函數(shù)主體')
func1()
# 這是函數(shù)主體
# after
- 為函數(shù)寫一個裝飾器,把函數(shù)的返回值 +100 然后再返回迅栅。
def cla(fn):
def test(*args, **kwargs):
return fn(*args, **kwargs) + 100
return test
@cla
def sums(x: int, y: int):
return x + y
print(sums(3, 20)) # 123
- 寫一個裝飾器@tag要求滿足如下功能:
@tag
def render(text):
# 執(zhí)行其他操作
return text
@tag
def render2():
return 'abc'
print(render('Hello')) # 打印出: <p>Hello</p>
print(render2()) # 打印出: <p>abc</p>
?```python
def cla_p(fn):
def test(*args, **kwargs):
return '<p>' + fn(*args, **kwargs) + '</p>'
return test
@cla_p
def func1(text):
return text
@cla_p
def func2():
return 'hello'
print(func1('python')) # <p>python</p>
print(func2()) # <p>hello</p>
- 寫一個裝飾器@tag要求滿足如下功能(需要使用帶參的裝飾器,自己先自學正在一下):
@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 cla(fn):
def test(*args, **kwargs):
return '<' + name + '>' + fn(*args, **kwargs) + '</' + name + '>'
return test
return cla
@tag(name='p')
def func1(text):
return text
@tag(name='div')
def func2():
return 'Python'
print(func1('Hello')) # <p>Hello</p>
print(func2()) # <div>Python</div>
- 為函數(shù)寫一個裝飾器晴玖,根據(jù)參數(shù)不同做不同操作读存。
flag為True为流,則 讓原函數(shù)執(zhí)行后返回值加100,并返回让簿。
flag為False敬察,則 讓原函數(shù)執(zhí)行后返回值減100,并返回拜英。
def tag(flag):
def cla(fn):
def test(*args, **kwargs):
if flag:
return fn(*args, **kwargs) + 100
else:
return fn(*args, **kwargs) - 100
return test
return cla
@tag(flag=True)
def func1(x, y):
return x + y
@tag(flag=False)
def func2():
return 223
print(func1(21, 200)) # 321
print(func2()) # 123
- 寫一個斗地主發(fā)牌器
import random
list1 = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
list2 = ['黑桃', '紅桃', '方塊', '梅花']
cards = []
for col in list2:
for num in list1:
cards.append(col + num)
cards.append('大王')
cards.append('小王')
random.shuffle(cards) # 隨機打亂
print('user1:', cards[:17])
print('user2:', cards[17:34])
print('user3:', cards[34:51])
print('底牌:', cards[51:])
user1: ['紅桃K', '方塊A', '黑桃3', '黑桃K', '方塊Q', '梅花8', '梅花6', '紅桃2', '方塊9', '紅桃J', '大王', '黑桃8', '梅花3', '梅花2', '小王', '方塊J', '方塊3']
user2: ['紅桃7', '黑桃9', '黑桃6', '黑桃J', '黑桃4', '梅花7', '梅花9', '梅花J', '方塊4', '紅桃8', '方塊6', '黑桃7', '方塊2', '方塊K', '黑桃2', '梅花10', '紅桃3']
user3: ['梅花K', '黑桃5', '紅桃A', '方塊5', '紅桃4', '方塊8', '紅桃5', '梅花Q', '方塊7', '紅桃Q', '黑桃Q', '方塊10', '黑桃10', '紅桃9', '梅花5', '黑桃A', '紅桃6']
底牌: ['紅桃10', '梅花4', '梅花A']