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


Download 4.21 Mb.
Pdf ko'rish
bet147/344
Sana31.01.2024
Hajmi4.21 Mb.
#1818553
1   ...   143   144   145   146   147   148   149   150   ...   344
Bog'liq
Python Crash Course, 2nd Edition

155
try it yourselF
8-15. Printing Models:
Put the functions for the example printing_models.py in a 
separate file called printing_functions.py. Write an import statement at the top 
of printing_models.py, and modify the file to use the imported functions.
8-16. Imports:
Using a program you wrote that has one function in it, store that 
function in a separate file. Import the function into your main program file, and 
call the function using each of these approaches:
import module_name
from module_name import function_name
from module_name import function_name as fn
import module_name as mn
from module_name import *
8-17. Styling Functions:
Choose any three programs you wrote for this chapter, 
and make sure they follow the styling guidelines described in this section.
Summary
In this chapter you learned how to write functions and to pass arguments 
so that your functions have access to the information they need to do their 
work. You learned how to use positional and keyword arguments, and how 
to accept an arbitrary number of arguments. You saw functions that display 
output and functions that return values. You learned how to use functions 
with lists, dictionaries, 
if
statements, and 
while
loops. You also saw how to 
store your functions in separate files called modules, so your program files 
will be simpler and easier to understand. Finally, you learned to style your 
functions so your programs will continue to be well-structured and as easy 
as possible for you and others to read.
One of your goals as a programmer should be to write simple code that 
does what you want it to, and functions help you do this. They allow you to 
write blocks of code and leave them alone once you know they work. When 
you know a function does its job correctly, you can trust that it will continue 
to work and move on to your next coding task.
Functions allow you to write code once and then reuse that code as 
many times as you want. When you need to run the code in a function, 
all you need to do is write a one-line call and the function does its job. 
When you need to modify a function’s behavior, you only have to modify 
one block of code, and your change takes effect everywhere you’ve made a 
call to that function.
Using functions makes your programs easier to read, and good func-
tion names summarize what each part of a program does. Reading a series 
of function calls gives you a much quicker sense of what a program does 
than reading a long series of code blocks.


156
Chapter 8
Functions also make your code easier to test and debug. When the bulk 
of your program’s work is done by a set of functions, each of which has a 
specific job, it’s much easier to test and maintain the code you’ve written. 
You can write a separate program that calls each function and tests whether 
each function works in all the situations it may encounter. When you do 
this, you can be confident that your functions will work properly each time 
you call them.
In Chapter 9 you’ll learn to write classes. Classes combine functions and 
data into one neat package that can be used in flexible and efficient ways.



Download 4.21 Mb.

Do'stlaringiz bilan baham:
1   ...   143   144   145   146   147   148   149   150   ...   344




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