我是靠谱客的博主 淡然冥王星,这篇文章主要介绍Python学习笔记——函数,现在分享给大家,希望可以做个参考。

今天学习了python中函数的基本操作,包括简单的函数定义,传递实参,返回实参,以及对参数的一些操作,以下是关于函数这一章节的部分练习题,本章中的内容基本都有涉及(模块部分没有涉及):

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#8-1 定义函数 def display_message(): print('Chapter 8 functionn') display_message() #8-2 函数参数,函数返回值 def read_title(): book=input('Plz enter your favorite book: ') return book def favorite_book(book): print('One of your favorite books is',book,'.n') book=read_title() favorite_book(book) #8-3 8-4 位置实参,关键字实参,默认实参 def make_shirt(word='I love python',size='L'): print('word:',word,' size:',size,'n') def shirt(): print('Plz enter following information(or enter default):') shirt_word=input('word on the shirt:') shirt_size=input('size:') if shirt_word=='default': if shirt_size=='default': make_shirt() else: make_shirt(size=shirt_size) else: if shirt_size=='default': make_shirt(shirt_word) else: make_shirt(shirt_word,shirt_size) shirt() #8-6 8-7 8-8 返回字典 def make_album(singer='Jay',title='床边故事',num=0): album={} if num==0: album['singer']=singer album['title']=title else: album['singer']=singer album['title']=title album['quantity']=num return album def user_album(): information=['singer:','title:','quantity(enter 0 to skip):'] i=0 inf=[] while i<3: sentence='Plz enter '+information[i] temp=input(sentence) if i!=2: inf.append(temp) else: if int(temp)!=0: inf.append(temp) i+=1 if len(inf)==3: a=make_album(inf[0],inf[1],inf[2]) else: a=make_album(inf[0],inf[1]) print(a) user_album() print('n') #8-9 8-10 8-11 函数传递列表 def show_magicians(mgcs): for mgc in mgcs: print(mgc) def make_great(mgcs): print('do not change the list:') i=0 for mgc in mgcs: mgcs[i]='the great '+mgc i+=1 show_magicians(mgcs) mgcs=['XiaoMing','XioaoHong','XiaoMa'] make_great(mgcs[:]) print('magicians:') show_magicians(mgcs) print('n') #8-12 任意数量实参 def sandwich(*sws): for sw in sws: print(sw) print('n') sandwich('a sandwich','b sanwich','c sandwich') #8-14 任意数量实参——字典 def make_car(brand,model,**others): car={} car['brand']=brand car['model']=model for key,value in others.items(): car[key]=value return car car=make_car('奥迪','A8',color='red',tow_package=True) print(car) print('n')

2018/3/28

最后

以上就是淡然冥王星最近收集整理的关于Python学习笔记——函数的全部内容,更多相关Python学习笔记——函数内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(93)

评论列表共有 0 条评论

立即
投稿
返回
顶部