This page intentionally left blank Python for Software Design


MATH FUNCTIONS Python has a math module that provides most of the familiar mathematical functions. A module



tải về 1.38 Mb.
Chế độ xem pdf
trang15/83
Chuyển đổi dữ liệu13.08.2023
Kích1.38 Mb.
#55046
1   ...   11   12   13   14   15   16   17   18   ...   83
- Python for Software Design How to Think Like a Computer Scientist-Cambridge University Press (2009)

3.3
MATH FUNCTIONS
Python has a math module that provides most of the familiar mathematical functions.
module is a file that contains a collection of related functions.
Before we can use the module, we have to import it:
>>> import math
This statement creates a module object named math. If you print the module object,
you get some information about it:
>>> print math

The module object contains the functions and variables defined in the module. To
access one of the functions, you have to specify the name of the module and the name


3.4 Composition
23
of the function, separated by a dot (also known as a period). This format is called
dot notation.
>>> ratio = signal_power / noise_power
>>> decibels = 10 * math.log10(ratio)
>>> radians = 0.7
>>> height = math.sin(radians)
The first example computes the logarithm base 10 of the signal-to-noise ratio. The
math module also provides a function called log that computes logarithms base e.
The second example finds the sine of radians. The name of the variable is a hint that
sin
and the other trigonometric functions (cos, tan, etc.) take arguments in radians.
To convert from degrees to radians, divide by 360 and multiply by 2
π:
>>> degrees = 45
>>> radians = degrees / 360.0 * 2 * math.pi
>>> math.sin(radians)
0.707106781187
The expression math.pi gets the variable pi from the math module. The value of
this variable is an approximation of
π, accurate to about 15 digits.
If you know your trigonometry, you can check the previous result by comparing it
to the square root of two divided by two:
>>> math.sqrt(2) / 2.0
0.707106781187
3.4
COMPOSITION
So far, we have looked at the elements of a program – variables, expressions, and
statements – in isolation, without talking about how to combine them.
One of the most useful features of programming languages is their ability to take
small building blocks and compose them. For example, the argument of a function
can be any kind of expression, including arithmetic operators:
x = math.sin(degrees / 360.0 * 2 * math.pi)


24
Functions
And even function calls:
x = math.exp(math.log(x+1))
Almost anywhere you can put a value, you can put an arbitrary expression, with one
exception: the left side of an assignment statement has to be a variable name. Any
other expression on the left side is a syntax error.

>>> minutes = hours * 60
# right
>>> hours * 60 = minutes
# wrong!
SyntaxError: can't assign to operator
3.5
ADDING NEW FUNCTIONS
So far, we have only been using the functions that come with Python, but it is also
possible to add new functions. A function definition specifies the name of a new
function and the sequence of statements that execute when the function is called.
Here is an example:
def print_lyrics():
print "I'm a lumberjack, and I'm okay."
print "I sleep all night and I work all day."
def
is a keyword that indicates that this is a function definition. The name of the
function is print_lyrics. The rules for function names are the same as for variable
names: letters, numbers and some punctuation marks are legal, but the first character
can’t be a number. You can’t use a keyword as the name of a function, and you should
avoid having a variable and a function with the same name.
The empty parentheses after the name indicate that this function doesn’t take any
arguments.
The first line of the function definition is called the header; the rest is called the body.
The header has to end with a colon and the body has to be indented. By convention,
the indentation is always four spaces (see Section 3.13). The body can contain any
number of statements.
The strings in the print statements are enclosed in double quotes. Single quotes and
double quotes do the same thing; most people use single quotes except in cases like
this where a single quote (which is also an apostrophe) appears in the string.

We will see exceptions to this rule later.


3.5 Adding New Functions
25
If you type a function definition in interactive mode, the interpreter prints ellipses
(...) to let you know that the definition isn’t complete:
>>> def print_lyrics():
...
print "I'm a lumberjack, and I'm okay."
...
print "I sleep all night and I work all day."
...
To end the function, you have to enter an empty line (this is not necessary in a script).
Defining a function creates a variable with the same name.
>>> print print_lyrics

>>> print type(print_lyrics)

The value of print_lyrics is a function object, which has type 'function'.
The syntax for calling the new function is the same as for built-in functions:
>>> print_lyrics()
I'm a lumberjack, and I'm okay.
I sleep all night and I work all day.
Once you have defined a function, you can use it inside another function. For exam-
ple, to repeat the previous refrain, we could write a function called repeat_lyrics:
def repeat_lyrics():
print_lyrics()
print_lyrics()
And then call repeat_lyrics:
>>> repeat_lyrics()
I'm a lumberjack, and I'm okay.
I sleep all night and I work all day.
I'm a lumberjack, and I'm okay.
I sleep all night and I work all day.
But that’s not really how the song goes.


26
Functions
3.6
DEFINITIONS AND USES
Pulling together the code fragments from the previous section, the whole program
looks like this:
def print_lyrics():
print "I'm a lumberjack, and I'm okay."
print "I sleep all night and I work all day."
def repeat_lyrics():
print_lyrics()
print_lyrics()
repeat_lyrics()
This program contains two function definitions: print_lyrics and repeat_lyrics.
Function definitions get executed just like other statements, but the effect is
to create function objects. The statements inside the function do not get exe-
cuted until the function is called, and the function definition generates no
output.
As you might expect, you have to create a function before you can execute it. In
other words, the function definition has to be executed before the first time it is
called.
Exercise 3.1
Move the last line of this program to the top, so the function call appears before the
definitions. Run the program and see what error message you get.
Exercise 3.2
Move the function call back to the bottom and move the definition of print_lyrics
after the definition of repeat_lyrics. What happens when you run this program?
3.7
FLOW OF EXECUTION
In order to ensure that a function is defined before its first use, you have to know the
order in which statements are executed, which is called the flow of execution.
Execution always begins at the first statement of the program. Statements are
executed one at a time, in order from top to bottom.
Function definitions do not alter the flow of execution of the program, but remem-
ber that statements inside the function are not executed until the function is
called.
A function call is like a detour in the flow of execution. Instead of going to the next
statement, the flow jumps to the body of the function, executes all the statements
there, and then comes back to pick up where it left off.


3.8 Parameters and Arguments
27
That sounds simple enough, until you remember that one function can call another.
While in the middle of one function, the program might have to execute the state-
ments in another function. But while executing that new function, the program might
have to execute yet another function!
Fortunately, Python is good at keeping track of where it is, so each time a function
completes, the program picks up where it left off in the function that called it. When
it gets to the end of the program, it terminates.
What’s the moral of this sordid tale? When you read a program, you don’t always
want to read from top to bottom. Sometimes it makes more sense if you follow the
flow of execution.
3.8
PARAMETERS AND ARGUMENTS
Some of the built-in functions we have seen require arguments. For example, when
you call math.sin you pass a number as an argument. Some functions take more
than one argument: math.pow takes two, the base and the exponent.
Inside the function, the arguments are assigned to variables called parameters. Here
is an example of a user-defined function that takes an argument:
def print_twice(bruce):
print bruce
print bruce
This function assigns the argument to a parameter named bruce. When the function
is called, it prints the value of the parameter (whatever it is) twice.
This function works with any value that can be printed.
>>> print_twice('Spam')
Spam
Spam
>>> print_twice(17)
17
17
>>> print_twice(math.pi)
3.14159265359
3.14159265359
The same rules of composition that apply to built-in functions also apply to
user-defined functions, so we can use any kind of expression as an argument


28
Functions
for print_twice:
>>> print_twice('Spam'*4)
Spam Spam Spam Spam
Spam Spam Spam Spam
>>> print_twice(math.cos(math.pi))
-1.0
-1.0
The argument is evaluated before the function is called, so in the examples the
expressions 'Spam '*4 and math.cos(math.pi) are only evaluated once.
You can also use a variable as an argument:
>>> michael = 'Eric, the half a bee.'
>>> print_twice(michael)
Eric, the half a bee.
Eric, the half a bee.
The name of the variable we pass as an argument (michael) has nothing to do with
the name of the parameter (bruce). It doesn’t matter what the value was called back
home (in the caller); here in print_twice, we call everybody bruce.

tải về 1.38 Mb.

Chia sẻ với bạn bè của bạn:
1   ...   11   12   13   14   15   16   17   18   ...   83




Cơ sở dữ liệu được bảo vệ bởi bản quyền ©hocday.com 2024
được sử dụng cho việc quản lý

    Quê hương