H a n d s o n, p r o j e c t b a s e d


xvi Contents in Detail 8


Download 4.21 Mb.
Pdf ko'rish
bet13/344
Sana31.01.2024
Hajmi4.21 Mb.
#1818553
1   ...   9   10   11   12   13   14   15   16   ...   344
Bog'liq
Python Crash Course, 2nd Edition

xvi
Contents in Detail

functionS 129
Defining a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Passing Information to a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Arguments and Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Exercise 8-1: Message. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Exercise 8-2: Favorite Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Passing Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Positional Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Keyword Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Equivalent Function Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Avoiding Argument Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Exercise 8-3: T-Shirt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Exercise 8-4: Large Shirts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Exercise 8-5: Cities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Returning a Simple Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Making an Argument Optional . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Returning a Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Using a Function with a while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Exercise 8-6: City Names. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Exercise 8-7: Album. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Exercise 8-8: User Albums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Passing a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Modifying a List in a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Preventing a Function from Modifying a List . . . . . . . . . . . . . . . . . . . . . . . . 145
Exercise 8-9: Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Exercise 8-10: Sending Messages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Exercise 8-11: Archived Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Passing an Arbitrary Number of Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Mixing Positional and Arbitrary Arguments . . . . . . . . . . . . . . . . . . . . . . . . 148
Using Arbitrary Keyword Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Exercise 8-12: Sandwiches. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Exercise 8-13: User Profile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Exercise 8-14: Cars. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Storing Your Functions in Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Importing an Entire Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Importing Specific Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Using as to Give a Function an Alias . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Using as to Give a Module an Alias . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Importing All Functions in a Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Styling Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Exercise 8-15: Printing Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Exercise 8-16: Imports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Exercise 8-17: Styling Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155


Contents in Detail

Download 4.21 Mb.

Do'stlaringiz bilan baham:
1   ...   9   10   11   12   13   14   15   16   ...   344




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling