Вы находитесь на странице: 1из 20

Mathematics

for the Digital Age



Programming
in Python


>>> Second Edition:
with Python 3



Maria Litvin
Phillips Academy, Andover, Massachusetts

Gary Litvin
Skylight Software, Inc.







Skylight Publishing
Andover, Massachusetts
and




Skylight Publishing
9 Bartlet Street, Suite 70
Andover, MA 01810

web: http://www.skylit.com
e-mail: sales@skylit.com
support@skylit.com


Copyright 2010 by Maria Litvin, Gary Litvin, and
Skylight Publishing

All rights reserved. No part of this publication may be reproduced,
stored in a retrieval system, or transmitted, in any form or by any means,
electronic, mechanical, photocopying, recording, or otherwise, without
the prior written permission of the authors and Skylight Publishing.

Library of Congress Control Number: 2009913596

ISBN 978-0-9824775-8-8

The names of commercially available software and products mentioned in this book are
used for identification purposes only and may be trademarks or registered trademarks
owned by corporations and other commercial entities. Skylight Publishing and the authors
have no affiliation with and disclaim any sponsorship or endorsement by any of these
products manufacturers or trademarks owners.





1 2 3 4 5 6 7 8 9 10 15 14 13 12 11 10


Printed in the United States of America




1
1 Sets and Functions

1.1 Prologue

A function establishes a relation between a set of inputs (numbers, points, objects)
and a set of outputs.

A function associates one output with each input.

But the same output can be associated with two or more different inputs (Figure 1-1).




Figure 1-1. A function associates one output with each input

The concept of function is essential for most branches of mathematics and science.
A function defines a relationship between objects or quantities, and that is precisely
what math and science are concerned with: how things relate to each other.

We often use lowercase letters as names for functions. Given a function f, we often
use the letter x to denote the functions input. We call the corresponding output
( ) f x . In other words, a function f maps an input x onto the output ( ) f x .

A function is also called a mapping: it maps the set of inputs into the set
of outputs.











C
o
p
y
r
i
g
h
t


2
0
0
8
-
2
0
1
0

b
y

S
k
y
l
i
g
h
t

P
u
b
l
i
s
h
i
n
g

2 CHAPTER 1 ~ SETS AND FUNCTIONS
The input value passed to a function is called its argument.

In this chapter we will discuss different ways to define a function and at the end
experiment with simple Python functions.

1.2 Sets

A set of inputs... A set of outputs... You might be wondering: What exactly is a
set? The concept of a set is one of those fundamental mathematical concepts that
cannot be defined formally. A set is... well, any collection of things. For example, a
set of all the students in the classroom, a set of all the letters in the alphabet, a set of
all positive integers under 10.

The items that belong to a set are called its elements.

x S means x is an element of the set S.

A set can have a finite number of elements such a set is called a finite set. For a
finite set, we can list all its elements (although it may take some time if the set is
large, such as the set of all Chinese characters). A set can have only one element.
For convenience, mathematicians also define the empty set a set that has no
elements at all. The notation for the empty set is .

With a little imagination, we can also define infinite sets. For example, a set of all
positive integers, a set of all points on a straight line, a set of all finite sets of
integers... We cannot list all the elements of an infinite set. Defining an infinite set
can be tricky: infinite sets exist only in the abstract realm of mathematics.

We will use uppercase letters as names for sets and use curly braces to list the
elements of a set. For example, { } 1,2,3 A = states that the set A contains the three
elements, 1, 2, and 3. { } 1,2,3, ... = represents the set of all positive integers;
represents the set of all real numbers. (We couldnt possibly list all the elements
of .)

If the set B is made up of some of the elements of the set A, then B is called a subset
of A. The notation B A means B is a subset of A. For example, { } { } 2,3 1,2,3 .
For any set S, S and S S .










C
o
p
y
r
i
g
h
t


2
0
0
8
-
2
0
1
0

b
y

S
k
y
l
i
g
h
t

P
u
b
l
i
s
h
i
n
g

1.2 ~ SETS 3

The set of all the possible inputs of a function f is called the domain of f.
The set of all the outputs of f is called the range of f.

If x is an element of the domain of f, then ( ) f x is an element of the range of f.

Example 1

Let Z be the set of all integers and E the set of all even integers. The function that
takes any integer n as input and returns 2n as the output maps Z onto E:


0 -2 -1 2 1 -3 3 4
0 -2 2 4


If we call this function g, we can say that ( ) 2 g n n = . Z is the domain of g. E is the
range of g.

Example 2

{ } 0, 1, 2 A = is a set of three elements. The function 0 1, 1 2, 2 0 maps A
onto itself. A is both the domain and the range of this function.











C
o
p
y
r
i
g
h
t


2
0
0
8
-
2
0
1
0

b
y

S
k
y
l
i
g
h
t

P
u
b
l
i
s
h
i
n
g

4 CHAPTER 1 ~ SETS AND FUNCTIONS
It is often convenient to view the range of a function as a subset of a larger set
(Figure 1-2).

We can say that a function maps its domain onto its range and into a larger set that
contains the range. This way, if you have two sets, A and B, you can consider all
functions from A into B; for each such function its range is a subset of B (possibly
equal to the whole set B).


Domain
Range

Figure 1-2. The range of a function can be a subset of a larger set

Example 3

Suppose P is the set of all the students in the classroom, and the function
( ) birthday p takes a person p as its input and returns ps birthday as the output. It is
convenient to view this as a mapping from the set P (all students in the classroom)
into the set D (all 366 possible birthdays, including February 29 for those special
people who were born on that day in a leap year). The range of the function birthday
the days on which the actual birthdays of the students in the classroom happen to
fall is a subset of D.











C
o
p
y
r
i
g
h
t


2
0
0
8
-
2
0
1
0

b
y

S
k
y
l
i
g
h
t

P
u
b
l
i
s
h
i
n
g

1.2 ~ SETS 5
Exercises

1. { } , T a b = is a set of two elements. List all the different subsets of T. How
many are there? (Be sure to include the empty set and the subset that is the
whole set T.)

2. How many different subsets does a set of three elements have? A set of four
elements? A set of n elements?

3. Give an example of a function whose domain has five elements and whose
range has five elements.

4. Can you define a function whose domain has three elements and whose range
has two elements? If yes, give an example; if not, explain why not.

5. Can you define a function whose domain has three elements and whose range
has four elements? If yes, give an example; if not, explain why not.

6. Give an example of a function whose domain is the set of all integers and
whose range has three elements.

7. A is a set of 17 elements, and a function f is defined on A (that is, A is fs
domain). What is the smallest and the largest possible number of elements in
fs range?

8.

Suppose a set A has three elements and a set B has three elements. How
many different functions from A into B can be defined?

9. Give an example of a function that maps all integers onto the set of all odd
integers.

10. Devise a function that has the set of all the English words as its domain and a
set of all the letters of the English alphabet as its range.

11. Devise a function that maps the set of all the points inside (and on the
border) of a circle of radius 1 onto the set of real numbers 0 1 y .

12.

Devise a function whose domain is the set of real numbers x such that
0 1 x < and whose range is the set of real numbers y such that 1 y .











C
o
p
y
r
i
g
h
t


2
0
0
8
-
2
0
1
0

b
y

S
k
y
l
i
g
h
t

P
u
b
l
i
s
h
i
n
g

6 CHAPTER 1 ~ SETS AND FUNCTIONS

1.3 Ways to Define a Function

To define a function you need to define its domain and a way to find or calculate the
functions value (that is, the functions output) for each element in the domain.
There are several ways to define a function: in words, in a table, in a graph or a chart,
or using a formula.

Example 1

A function defined in words:

For any positive integer n, let ( ) s n be the sum of all integers from 1 to n.

The domain of this function is all positive integers, and we can calculate the value of
( ) s n for any n: (1) 1 s = , (2) 1 2 3 s = + = , (3) 1 2 3 6 s = + + = , and so on.

Example 2

Suppose we say something like this: ( ) f year is the number of fatal accidents in the
U.S. caused by drunk drivers in a given year for the years between 1990 and 1999.
This sentence describes the function in general terms, but does not give us its values.
To complete the definition, we need to look up the data and arrange it in a table
and/or a graph:


0
5,000
10,000
15,000
20,000
25,000
1990 91 92 93 94 95 96 97 98 99
1990 22,587
1991 20,159
1992 18,290
1993 17,908
1994 17,308
1995 17,732
1996 17,749
1997 16,711
1998 16,673
1999 16,572











C
o
p
y
r
i
g
h
t


2
0
0
8
-
2
0
1
0

b
y

S
k
y
l
i
g
h
t

P
u
b
l
i
s
h
i
n
g

1.3 ~ WAYS TO DEFINE A FUNCTION 7

In math and physics, by far the most common and useful way to define a
function is with a formula.

Example 3

Suppose we drop a rock from a cliff. The vertical distance it travels in t seconds is
given by the formula
2
( ) 16 h t t = (feet). The domain of this function is all non-
negative real numbers: for any 0 t (any time after we drop the rock), we can
calculate the value of ( ) h t from the formula. For example, (5) 16 25 400 h = =
(feet). If we measure the time it takes the rock to reach the ground, we can find the
height of the cliff.

It is often convenient to agree ahead of time on a universal set on which we
consider functions, for example, the set of all real numbers, or the set of all points
on a plane (for plane geometry), or the set of all integers (for number theory). The
domains of all functions we consider are then subsets of that universal set. We have
to also agree on the universal set to which the outputs of the functions belong.

Suppose we are working with real numbers and functions whose values are also real
numbers, so both the domain and the range of a function are subsets of . We can
write a formula, for example
1
( ) f x
x
= . This formula makes sense (that is, ( ) f x can
be calculated) for all 0 x , so it defines a function with the domain all real
numbers except 0 (or, in mathematical notation, {0} ).

If we define a function by a formula and do not specify the domain
explicitly, it is assumed that the domain is the set of all numbers for
which the formula makes sense.

This is called the natural domain of a function defined by a formula.











C
o
p
y
r
i
g
h
t


2
0
0
8
-
2
0
1
0

b
y

S
k
y
l
i
g
h
t

P
u
b
l
i
s
h
i
n
g

8 CHAPTER 1 ~ SETS AND FUNCTIONS
Example 4

For a real number x, x makes sense (produces a real number) if and only if 0 x .
Therefore, the natural domain of the function defined by the formula ( ) f x x = is
the set of all non-negative real numbers. (The range of this function is also all
non negative real numbers.)

Sometimes the same function can be described in two different ways. When we
prove that two definitions describe the same function, we usually obtain an
interesting mathematical fact. Consider for example the function described in
Example 1: for any positive integer n, let ( ) s n be the sum of all integers from 1 to n.
Consider another function, defined by the formula
( 1)
( )
2
n n
f n
+
= . Let us prove that
( ) s n and ( ) f n actually represent the same function: that is, for any positive
integer n, ( ) ( ) s n f n = .

You may be tempted to take a few values of n and compare ( ) s n with ( ) f n . For
example, for 2 n = ,
2 3
1 2 3
2

+ = = ; for 5 n = ,
5 6
1 2 3 4 5 15
2

+ + + + = = . This
indicates that we are on the right track, but it is not a mathematical proof. How can
we be sure that this property holds for every positive integer n? There are infinitely
many possible values of n, and we cannot check every one of them.

In a mathematical proof, we have to show that the fact is true beyond
any doubt.

A mathematical proof of the fact that ( ) ( ) s n f n = for any positive integer n can go
like this. By definition,

( ) s n = 1 + 2 + ... + (n- 1) + n

Let us rewrite this sum in reverse order (we know that the result of addition does not
depend on the order of the operands there is a postulate or a theorem to that
effect):

( ) s n = n + (n- 1) + ... + 2 + 1











C
o
p
y
r
i
g
h
t


2
0
0
8
-
2
0
1
0

b
y

S
k
y
l
i
g
h
t

P
u
b
l
i
s
h
i
n
g

1.3 ~ WAYS TO DEFINE A FUNCTION 9
Now let us add the two lines together termwise (the term above plus the term below):


( ) s n = 1 + 2 + ... + (n- 1) + n
( ) s n = n + (n- 1) + ... + 2 + 1


We get:


2 ( ) s n =n+1 + n+1 + ... + n+1 + n+1
n times


Therefore, 2 ( ) ( 1) s n n n = + and
( 1)
( )
2
n n
s n
+
= , Q.E.D. (This is from Latin: Quod
Erat Demonstrandum what we needed to show.)

Ideally, a mathematical proof should follow from the most basic agreed-on
mathematical facts (postulates) or from facts that have already been proven earlier
(theorems). It would be way too tedious, though, to do that for every proof. So
mathematicians often skip the earlier steps that depend on established mathematical
facts. The above proof is an example of that. Mathematicians know that someone
somewhere had worked out the needed steps down to the postulates.

Exercises

1. What is the natural domain of the function defined for real numbers by the
formula
1
( )
2
f x
x
=

? What is the range of this function?



2. What is the natural domain of ( ) 1 f x x = + ? What is the range?

3.

What is the natural domain of


2
( ) 1 f x x = ? What is the range?











C
o
p
y
r
i
g
h
t


2
0
0
8
-
2
0
1
0

b
y

S
k
y
l
i
g
h
t

P
u
b
l
i
s
h
i
n
g

10 CHAPTER 1 ~ SETS AND FUNCTIONS
4. Consider a function defined on the set of all three-digit positive integers; for
each integer, the function returns the sum of its digits. For example,
(243) 9 f = . How many elements do the domain and the range of this
function have?

5.

The table below shows a few values of the function ( ) h n defined on the set
of all positive integers:

n 1 2 3 4 5 6
( ) h n
2 5 10 17 26 37

Come up with a simple formula that matches the function for the values
shown in the table.

6. Describe a function f whose domain and range are both the set of all the
points on a plane and that has exactly one fixed point, that is, a point P such
that ( ) f P P = .

7. Given a set A of three elements, how many functions from A onto itself (that
is, such functions that both the domain and the range of the function is A)
have no fixed points?

8.

Come up with a formula that defines a function on with the natural


domain 1 1 x < < and the range .

9.

Consider the functions f and g, defined by the following tables:



x - 1 0 1 x - 1 0 1
( ) f x
3 1 - 1
( ) g x
- 2 - 1 0

What are the values of ( ) (1) f g and ( ) (1) g f ?

10.

Consider the following function d defined for all positive integers: ( ) d n is


the sum of all odd positive integers below 2n. Prove that for any positive n,
2
( ) d n n = .











C
o
p
y
r
i
g
h
t


2
0
0
8
-
2
0
1
0

b
y

S
k
y
l
i
g
h
t

P
u
b
l
i
s
h
i
n
g

1.4 ~ ALGORITHMS 11

1.4 Algorithms

Another way to define a function is to describe an algorithm or to write a computer
program for calculating its values.

An algorithm is a precise description of the steps necessary to calculate a
function or to accomplish a certain task.

A recipe in a cookbook is a type of algorithm. But interesting algorithms usually
involve several steps that are repeated many times. This allows us to fold a long
sequence of steps into a relatively short algorithm which gives algorithms their
power.

Example 1

The following algorithm takes a positive integer n and returns the number of digits in
it:

Set count to 1
Set power to 10
While power n, repeat the following steps:
Increment count by 1
Multiply power by 10
Return count

Algorithms are often described not in words but in a more concise notation, called
pseudocode. In pseudocode the above algorithm might look like this:

count 1
power 10
whi l e power n:
count count + 1
power power * 10
r et ur n count

Pseudocode is not a computer language, so the algorithm above is not real computer
code it is less formalized. But we could easily implement this pseudocode
algorithm in many programming languages.










C
o
p
y
r
i
g
h
t


2
0
0
8
-
2
0
1
0

b
y

S
k
y
l
i
g
h
t

P
u
b
l
i
s
h
i
n
g

12 CHAPTER 1 ~ SETS AND FUNCTIONS

Another way to describe an algorithm is with a flowchart. A flowchart for the above
algorithm might look like this:


count 1
power 10
Yes
No
count count +1
power power * 10
power n?
Input: n
Output: count


Flowcharts belong in a computer museum they are hardly used nowadays.

Example 2

Let ( ) p n be the highest power of 2 that divides n. That is, ( ) p n is the largest k such
that n is evenly divisible by 2
k
(that is, the remainder when n is divided by 2
k
is 0).
For example, (3) 0 p = , (6) 1 p = , (8) 3 p = , (12) 2 p = , and (14) 1 p = . It might seem,
at first, that the only way to calculate ( ) p n is to try every value of 2
k
under n, take
the largest of them, and return the corresponding k. However, it turns out there is a
more economical algorithm:

Set k to 0
While n is even, repeat the following steps:
Add 1 to k
Divide n by 2
Return k










C
o
p
y
r
i
g
h
t


2
0
0
8
-
2
0
1
0

b
y

S
k
y
l
i
g
h
t

P
u
b
l
i
s
h
i
n
g

1.4 ~ ALGORITHMS 13
The same in pseudocode:

k 0
whi l e n i s even:
k k + 1
n n/ 2
r et ur n k

Exercises

1. Name a couple of algorithms that you know from arithmetic and algebra.

2. Devise an algorithm for calculating ( ) 1 2 ... s n n = + + + without using the
formula
( 1)
( )
2
n n
s n
+
= . Present your algorithm in pseudocode.

3. Suppose you only have the operations of addition and subtraction. Devise an
algorithm that takes two positive integers, m and n, and calculates the
quotient and the remainder when m is divided by n. (For example, when 17
is divided by 5, the quotient is 3 and the remainder is 2.) Present your
algorithm in pseudocode.

4.

Devise an algorithm for calculating 3


n
, where n is a non-negative integer.
Write your algorithm in pseudocode.

5.

Devise an algorithm that for a given positive integer n finds the largest
integer k such that 2
k
n . Write pseudocode and draw a flowchart for this
algorithm.

6.

Suppose wor d is a string of letters (all lower case). Suppose wor d[ i ] refers
to the i-th letter of wor d (starting from 0: wor d[ 0] is the first letter of
wor d). Suppose l en( wor d) refers to the number of letters in wor d. Devise
an algorithm that compares two words and decides which one goes first in a
dictionary.











C
o
p
y
r
i
g
h
t


2
0
0
8
-
2
0
1
0

b
y

S
k
y
l
i
g
h
t

P
u
b
l
i
s
h
i
n
g

14 CHAPTER 1 ~ SETS AND FUNCTIONS

1.5 Exploring Functions in Python



In Chapter 2, we will talk about computer programs and programming languages.
Meanwhile, we can start playing with Python a little. Dont worry if you dont
understand all the technical notation right away. Youll get used to it very soon.
Meanwhile, just follow along and see what you notice. When you start to write your
own little programs in the exercises, just copy the format of the sample programs.

If you havent done so already, go ahead and download Python and
other tools from www.python.org. This books web site,
www.skylit.com/python, has instructions for getting started with
Python: downloading the interpreter and running programs
(Appendix A).

Like almost all programming languages, Python allows you to define functions. The
concept of a function in a program is similar to the concept of a function in math: a
function in Python can take an input (one or more arguments) and return an output
(the result). You can also view a function as a fragment of program code that can be
called from different places in the program, with different arguments.

Example 1

Start the Python interpreter and type:

>>> def double(n):
return 2*n

This defines a function, called doubl e, which returns its argument multiplied by 2.
def is a reserved word in Python (a word with a special meaning in a programming
language). def indicates that we are defining a function. Note the colon at the end
of the def line: it is required by Pythons syntax rules. Statements within the
function definition are indented (shifted to the right, here by four spaces). r et ur n is
another Python reserved word it indicates what value the function returns to the
caller.











C
o
p
y
r
i
g
h
t


2
0
0
8
-
2
0
1
0

b
y

S
k
y
l
i
g
h
t

P
u
b
l
i
s
h
i
n
g

1.5 ~ EXPLORING FUNCTIONS IN PYTHON 15
Once a function is defined, you can call it with different arguments:

>>> double(3)
6
>>> double(0.5)
1. 0

In the above example, we named our function double. This self-explanatory name
indicates what the function computes. We could have called it s or f or d, as
we do in math Python doesnt care. But imagine a program with dozens of
functions named f, g, a, z, s,... Even the author would have trouble remembering
which function does what. And other programmers would find such a program
totally incomprehensible.

When you are writing a computer program, always keep in mind that
you are writing it not only for yourself, but also for others to read.

Choose meaningful names for functions, even if they take longer to type never be
stingy with keystrokes.

In a program, a definition of a function can do much more than just return a value of
an expression (formula). A function can implement an algorithm for computing a
value.

Example 2

Here is a Python function for computing the function ( ) p n from Example 2 in
Section 1.4. ( ) p n returns the largest k, such that n is evenly divisible by 2
k
. This
Python code corresponds to the algorithm discussed in that example, but instead of
calling this function p we have called it largestPower2Divisor. Notice the
comments set off by the #sign the computer doesnt read them, but they help the
reader of the program to follow whats going on:

>>> def l ar gest Power 2Di vi sor ( n) :
k = 0
whi l e n %2 == 0: # whi l e n i s even
k += 1 # k <- k + 1
n / / = 2 # n <- n / / 2 - -
# di vi de and t r uncat e t o an i nt
r et ur n k











C
o
p
y
r
i
g
h
t


2
0
0
8
-
2
0
1
0

b
y

S
k
y
l
i
g
h
t

P
u
b
l
i
s
h
i
n
g

16 CHAPTER 1 ~ SETS AND FUNCTIONS
You can test this function by calling it with different arguments:

>>> largestPower2Divisor(6)
1
>>> largestPower2Divisor(8)
3
>>> largestPower2Divisor(12)
2
>>> largestPower2Divisor(14)
1

Exercises

1. Type in the function

def mod3( n) :
r et ur n n %3

and call it with various integer arguments. Can you figure out what the %
operator means in Python? How does it work for positive and negative
integers?

2. Define a function i ncr ement ByOne that takes an argument x and returns
1 x + . Test your function for 3 x = , 5 x = , and 1.5 x = .

3. Python has a number of built-in (predefined) functions. One of them is
called abs. Try calling this function with various arguments and figure out
what it returns.

4. Define a Python function sum1ToN that returns 1 2 ... n + + + using the
formula
( 1)
1 2 ...
2
n n
n
+
+ + + = . Test your function for several values of n.
Hint: You can omit the multiplication sign in algebraic expressions, but
not in Python! Use / / for division so that the function returns an integer.

5. Define a function r eci pr ocal ( x) that returns 1/ x. Try calling it for
0 x = . How does Python deal with natural domains of functions?

6. Suppose you have defined the functions doubl e (from Example 1) and
i ncr ement ByOne from Question 2. What is the result of
doubl e( i ncr ement ByOne( 5) ) ? What is the result of
i ncr ement ByOne( doubl e( 5) ) ? Explain these results.











C
o
p
y
r
i
g
h
t


2
0
0
8
-
2
0
1
0

b
y

S
k
y
l
i
g
h
t

P
u
b
l
i
s
h
i
n
g

1.5 ~ EXPLORING FUNCTIONS IN PYTHON 17
7. In Python, a word, a phrase, or any string of letters, digits, or other characters
written within single or double quotes represents a literal string: an object
whose value is that string of characters. For example, ' Hel l o, Wor l d! '
represents a string whose value is Hello, World!. Try calling the function
i ncr ement ByOne from Question 2 with the argument ' 123' . Can you add
a number to a string in Python?

8. Call the function doubl e from Example 1 with the argument ' 123' . What
does Pythons * operator do when one operand is an integer and the other is
a string?

9. In Python, if s is a string, s[ 0] refers to its first character. Define and test a
function that takes a word and returns its first character.

10. In Python, [ a, b, c, . . . , x] represents a list of objects. For example,
[ 1, 5, 2] represents a list of three numbers: 1, 5, and 2. What does the
function defined in Question 9 return when, instead of a string, a list is
passed to it as an argument?

11. Question 10 describes how a list of numbers can be represented in Python.
Experiment and find out what Pythons built-in functions sum, mi n, and max
do when applied to a list of numbers.

12.

What do mi n( r ange( n) ) and max( r ange( n) ) return when n is a positive


integer?

13.

Rewrite the function sum1ToN from Question 4 using only one + and no
other arithmetic operators. Hint: use two of Pythons built-in functions;
see Questions 11 and 12.

14.

Using the function from Example 2 as a prototype, write a function


numDi gi t s that returns the number of digits in a positive integer. The
algorithm is described in Example 1 in Section 1.4 Hint: the statement

whi l e p <= n:

will repeat the statements indented below it as long as p remains less than or
equal to n.











C
o
p
y
r
i
g
h
t


2
0
0
8
-
2
0
1
0

b
y

S
k
y
l
i
g
h
t

P
u
b
l
i
s
h
i
n
g

18 CHAPTER 1 ~ SETS AND FUNCTIONS

1.6 Review

Terms and notation introduced in this chapter:

Set
Finite set
Infinite set
Subset
Empty set
Function
Mapping
Domain

Range
Argument
Natural domain
Algorithm
Pseudocode
Flowchart
Reserved word
Literal string
{ } , , A a b c =
x A

A B
( ) f x

Some of the Python features mentioned in this chapter:

reserved words def and r et ur n
+ operator
* operator
%operator
Single or double quotes designate literal strings, as in ' Hel l o' or " Hel l o" .
Square brackets designate a list of objects, as in [ a, b, c]
Built-in functions: abs, sum, max, mi n, r ange











C
o
p
y
r
i
g
h
t


2
0
0
8
-
2
0
1
0

b
y

S
k
y
l
i
g
h
t

P
u
b
l
i
s
h
i
n
g

Вам также может понравиться