Chapter 2. Arithmetic, Strings, and Variables

The first step in learning how to program is to understand the basic Python data types: integers (whole numbers), floating point numbers (numbers with a decimal point), and strings. All programs use these (and other) data types, so it is important to have a good grasp of their basic uses.

Strings, in particular, are used in so many different kinds of programs that Python provides a tremendous amount of support for them. In this chapter, we’ll just introduce the bare basics of strings, and then we’ll return to them in a later chapter.

We’ll also introduce the important concept of a programming variable. Variables are used to store and manipulate data, and it’s hard to write a useful program without employing at least a few of them.

Just like learning how to play the piano or speak a foreign language, the best way to learn how to program is to practice. Thus, we’ll introduce all of this using the interactive command shell IDLE, and ideally you should follow along on your own computer by typing the examples as we go.

The Interactive Command Shell

Let’s see how to interact with the Python shell. Start IDLE; you should find it listed as a program in your Start menu on Windows. On Mac or Linux you should be able to run it directly from a command line by typing python. The window that pops up is the Python interactive command shell, and it looks something like what’s shown in Figure 2.1.

Example 2.1. What you should see when you first launch the Python interactive command shell. The top two lines tell you what version of Python you are running. The version you see here is Python 3.0b1, and it was created a little before 3 p.m. on Thursday, June 19, 2008 (not that the creation date matters to us!).

Python 3.0b1 (r30b1:64403M, Jun 19 2008, 14:56:09) [MSC v.1500 32
bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

****************************************************************
Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer's internal loopback
interface. This connection is not visible on any external
interface and no data is sent to or received from the Internet.
****************************************************************

IDLE 3.0b1
>>>

The shell prompt

In a Python transcript, >>> is the Python shell prompt. A >>> marks a line of input from you, the user, while lines without a >>> are generated by Python. Thus it is easy to distinguish at a glance what is from Python and what is from you.

Transcripts

A shell transcript is a snapshot of the command shell showing a series of user inputs and Python replies. We’ll be using them frequently; they’re a great way to learn Python by seeing real examples in action.

Integer Arithmetic

An integer is a whole number, such as 25, −86, or 0. Python supports the four basic arithmetic operations: + (addition), (subtraction), * (multiplication), and / (division). Python also uses ** for exponentiation and % to calculate remainders (for example, 25 % 7 is 4 because 7 goes into 25 three times, with 4 left over). For example:

>>> 5 + 9
14
>>> 22 - 6
16
>>> 12 * 14
168
>>> 22 / 7
3.1428571428571428
>>> 2 ** 4
16
>>> 25 % 7
4
>>> 1 + 2 * 3
7
>>> (1 + 2) * 3
9

Integer division

Python also has an integer division operator, //. It works like /, except that it always returns an integer. For example, 7 // 3 evaluates to the integer 2; the digits after the decimal are simply chopped off (// does no rounding!).

Order of evaluation

Table 2.1 summarizes Python’s basic arithmetic operators. They are grouped from lowest precedence to highest precedence. For example, when Python evaluates the expression 1 + 2 * 3, it does * before + because * has higher precedence (so it evaluates to 7—not 9!). Operators at the same level of precedence are evaluated in the order they are written. You can use round brackets, (), to change the order of evaluation—so, for example, (1 + 2) * 3 evaluates to 9. In other words, Python arithmetic follows the same evaluation rules as regular arithmetic.

Table 2.1. Basic Arithmetic Operators

NAME

OPERATOR

EXAMPLE

addition

+

>>> 3 + 4

  

7

subtraction

>>> 5 − 3

  

2

multiplication

*

>>> 2 * 3

  

6

division

/

>>> 3 / 2

  

1.5

integer division

//

>>> 3 // 2

  

1

remainder

%

>>> 25 % 3

  

1

exponentiation

**

>>> 3 ** 3

  

27

Unlimited size

Unlike most other programming languages, Python puts no limit on the size of an integer. You can do calculations involving numbers with dozens (or hundreds, or thousands) of digits:

>>> 27 ** 100
1368914790585883759913260273820883159664
6369562533743647148019007836899717749907
6593800206155688941388250484440597994042
813512732765695774566001

Floating Point Arithmetic

Floating point arithmetic is done with floating point numbers, which in Python are numbers that contain a decimal point. For instance, −3.1, 2.999, and −4.0 are floating point numbers. We’ll call them floats for short.

All the basic arithmetic operations that work with integers also work with floats, even % (remainder) and // (integer division). See Figure 2.2 for some examples.

Example 2.2. Examples of basic floating point arithmetic using the Python command shell. Notice that approximation errors are quite common, so exact values are rarely printed.

>>> 3.8 + -43.2
-39.400000000000006
>>> 12.6 * 0.5
6.2999999999999998
>>> 12.6 + 0.01
12.609999999999999
>>> 365.0 / 12
30.416666666666668
>>> 8.8 ** -5.4
7.939507629591553e-06
>>> 5.6 // 1.2
4.0
>>> 5.6 % 3.2
2.3999999999999995

Float literals

Very large or small floats are often written in scientific notation:

>>> 8.8 ** -5.4
7.939507629591553e-06

The e-06 means to multiply the preceding number by 10−6. You can use scientific notation directly if you like:

>>> 2.3e02
230.0

Python is quite forgiving about the use of decimal points:

>>> 3.
3.0
>>> 3.0
3.0

You can write numbers like 0.5 with or without the leading 0:

>>> .5
0.5
>>> 0.5
0.5

✓ Tips

  • It’s usually clearer to write 5.0 instead of 5., as the latter notation can be quite confusing—it looks like the end of a sentence.

  • The difference between 5 and 5.0 matters: 5 is an integer, while 5.0 is a floating point number. Their internal representations are significantly different.

Overflow

Unlike integers, floating point numbers have minimum and maximum values that, if exceeded, will cause overflow errors. An overflow error means you’ve tried to calculate a number that Python cannot represent as a float, because it is either too big or too small (Figure 2.3). Overflow errors can be silent errors, meaning that Python may just do the calculation incorrectly without telling you that anything bad has happened. Generally speaking, it is up to you, the programmer, to avoid overflow errors.

Example 2.3. Floating point overflow: Python cannot store the result of 500.0 ** 10000 as a float.

>>> 500.0 ** 10000
Traceback (most recent call last):
    File "<pyshell#38>", line 1, in <module>
       500.0 ** 10000
OverflowError: (34, 'Result too large')

Limited precision

Precision (that is, accuracy) is a fundamental difficulty with floats on all computers. Numbers are represented in binary (base 2) in a computer, and it turns out that not all floating point numbers can be represented precisely in binary. Even the simplest examples can have problems:

>>> 1 / 3
0.33333333333333331

This should have an infinite number of 3s after the decimal, but there are only (!) 17 digits here. Plus, the last digit is wrong (the 1 should be a 3).

These small errors are not usually a problem: 17 digits after the decimal is enough for most programs. However, little errors have a nasty habit of becoming big errors when you are doing lots of calculations. If you are, say, computing the stresses on a newly designed bridge, it is necessary to take these floating point approximations into account to ensure that they don’t balloon into significant errors.

✓ Tip

  • In general, you should prefer integers to floating point numbers. They are always accurate and never suffer overflow.

Complex numbers

Python has built-in support for complex numbers—that is, numbers that involve the square root of −1. In Python, 1j denotes the square root of −1:

>>> 1j
1j
>>> 1j * 1j
(-1+0j)

Complex numbers are useful in certain engineering and scientific calculations; we won’t be using them again in this book.

Other Math Functions

Python comes with many different modules of prewritten code, including the math module. Table 2.2 lists some of the most commonly used math module functions.

Table 2.2. Some math Module Functions

NAME

DESCRIPTION

ceil(x)

Ceiling of x

cos(x)

Cosine of x

degrees(x)

Converts x from radians to degrees

exp(x)

e to the power of x

factorial(n)

Calculates n! = 1*2*3*...*n n must be an integer

log(x)

Base e logarithm of x

log(x, b)

Base b logarithm of x

pow(x, y)

x to the power of y

radians(x

Converts x from degrees to radians

sin(x)

Sine of x

sqrt(x)

Square root of x

tan(x)

Tangent of x

Using return values

We say that these functions return a value. That means they evaluate to either an integer or a floating point number, depending on the function.

You can use these functions anywhere that you can use a number. Python automatically evaluates the function and replaces it with its return value.

Importing a module

To use the math module, or any existing Python module, you must first import it:

>>> import math

You can now access any math function by putting math. in front of it:

>>> math.sqrt(5)
2.2360679774997898
>>> math.sqrt(2) * math.tan(22)
0.012518132023611912

An alternative way of importing a module is this:

>>> from math import *

Now you can call all the math module functions without first appending math.:

>>> log(25 + 5)
3.4011973816621555
>>> sqrt(4) * sqrt(10 * 10)
20.0

✓ Tips

  • When using the from math import * style of importing, if you have functions with the same name as any of the functions in the math module, the math functions will overwrite them!

  • Thus, it’s generally safer to use the import math style of importing. This will never overwrite existing functions.

  • You can also import specific functions from the math module. For example, from math import sqrt, tan imports just the sqrt and tan functions.

Strings

A string is a sequence of one or more characters, such as "cat!", "567-45442", and "Up and Down". Characters include letters, numbers, punctuation, plus hundreds of other special symbols and unprintable characters.

Indicating a string

Python lets you indicate string literals in three main ways:

  • Single quotes, such as 'http', 'open house', or 'cat'

  • Double quotes, such as "http", "open house", or "cat"

  • Triple quotes, such as """http""", or multiline strings, such as

    """
    
    Me and my monkey
    Have something to hide
    """

✓ Tips

  • Many Python programmers prefer using single quotes to indicate strings, simply because they involve less typing than double quotes (which require pressing the Shift key).

  • One of the main uses of single and double quotes is to conveniently handle " and ' characters inside strings:

    "It's great"
    'She said "Yes!"'
  • You’ll get an error if you use the wrong kind of quote within a string.

  • Triple quotes are useful when you need to create long, multiline strings. They can also contain " and ' characters at the same time.

String length

To determine the number of characters in a string, use the len(s) function:

>>> len('pear')
4
>>> len('up, up, and away')
16
>>> len("moose")
5
>>> len("")
0

The last example uses the empty string, usually denoted by '' or "". The empty string has zero characters in it.

Since len evaluates to (that is, returns) an integer, we can use len anywhere that an integer is allowed—for example:

>>> 5 + len('cat') * len('dog')
14

String Concatenation

You can create new strings by “adding” together old strings:

>>> 'hot ' + 'dog'
'hot dog'
>>> 'Once' + " " + 'Upon' + ' ' + "a Time"
'Once Upon a Time'

This operation is known as concatenation.

There’s a neat shortcut for concatenating the same string many times:

>>> 10 * 'ha'
'hahahahahahahahahaha'
>>> 'hee' * 3
'heeheehee'
>>> 3 * 'hee' + 2 * "!"
'heeheehee!!'

The result of string concatenation is always another string, so you can use concatenation anywhere that requires a string:

>>> len(12 * 'pizza pie!')
120
>>> len("house" + 'boat') * '12'
'121212121212121212'

Getting Help

Python is a largely self-documenting language. Most functions and modules come with brief explanations to help you figure out how to use them without resorting to a book or Web site.

Listing functions in a module

Once you’ve imported a module, you can list all of its functions using the dir(m) function:

>>> import math
>>> dir(math)
['__doc__', '__name__', '__package__', 'acos',
'acosh', 'asin', 'asinh', 'atan',
'atan2', 'atanh', 'ceil',
'copysign', 'cos', 'cosh', 'degrees',
'e', 'exp', 'fabs', 'factorial',
'floor', 'fmod', 'frexp', 'hypot',
'isinf', 'isnan', 'ldexp', 'log',
'log10', 'log1p', 'modf', 'pi', 'pow',
'radians', 'sin', 'sinh', 'sqrt', 'sum',
'tan', 'tanh', 'trunc']

This gives you a quick overview of the functions in a module, and many Python programmers use dir(m) all the time.

For now, you can ignore the names beginning with a double underscore __; they are used only in more advanced Python programming.

✓ Tips

  • To see a list of all the built-in functions in Python, type dir(__builtins__) at the command prompt.

  • An alternative way to see the doc string for a function f is to use the help(f) function.

  • You can run the Python help utility by typing help() at a prompt. This will provide you with all kinds of useful information, such as a list of all available modules, help with individual functions and keywords, and more.

  • You can also get help from the Python documentation (www.python.org/doc/). There you’ll find helpful tutorials, plus complete details of all the Python language and standard modules.

Printing documentation strings

Another useful trick is to print a function’s documentation string (doc string for short):

>>> print(math.tanh.__doc__)
tanh(x)
Return the hyperbolic tangent of x.

Most built-in Python functions, along with most functions in Python’s standard modules (such as math), have short doc strings you can access in this way.

As another example, here’s the doc string for the built-in function bin:

>>> print(bin.__doc__)
bin(number) -> string
Return the binary representation of an integer or long integer.
>>> bin(25)
'0b11001'

Converting Between Types

Converting from one type of data to another is a common task, and Python provides a number of built-in functions to make this easy.

Converting integers and strings to floats

To convert the number 3 to a float, use the float(x) function:

>>> float(3)
3.0

Converting a string to a float is similar:

>>> float('3.2')
3.2000000000000002
>>> float('3')
3.0

Converting integers and floats to strings

The str(n) function converts any number to a corresponding string:

>>> str(85)
'85'
>>> str(-9.78)
'-9.78'

Converting a float to an integer

This is a little tricky because you must decide how to handle any digits after the decimal in your float. The int(x) function simply chops off extra digits, while round(x) does the usual kind of rounding off:

>>> int(8.64)
8
>>> round(8.64)
9
>>> round(8.5)
8

Converting strings to numbers

This is easily done with the int(s) and float(s) functions:

>>> int('5')
5
>>> float('5.1')
5.0999999999999996

✓ Tips

  • For most applications, you should be able to handle numeric conversions using int(x), float(x), and round(x). However, for more specific conversions, the Python math module has a number of functions for removing digits after decimals: math.trunc(x), math.ceil(x), and math.floor(x).

  • The int(s) and float(s) conversions from strings to floats/integers assume that the string s “looks” like a Python float/integer. If not, you’ll get an error message saying the conversion could not be done.

Variables and Values

Variables are one of the most important concepts in all of programming: In Python, variables label, or point to, a value.

For example:

>>> fruit = "cherry"
>>> fruit
'cherry'

Here, fruit is a variable name, and it points to the string value "cherry". Notice that variables are not surrounded by quotation marks.

The line fruit = "cherry" is called an assignment statement. The = (equals sign) is called the assignment operator and is used to make a variable point to a value.

When Python encounters a variable, it replaces it with the value it points to. Thus:

>>> cost = 2.99
>>> 0.1 * cost
0.29900000000000004
>>> 1.06 * cost + 5.99
9.1594000000000015

Rules for making variable names

Variable names must follow a few basic rules (see Table 2.3 for some examples):

  • A variable name can be of any length, although the characters in it must be either letters, numbers, or the underscore character (_). Spaces, dashes, punctuation, quotation marks, and other such characters are not allowed.

  • The first character of a variable name cannot be a number; it must be a letter or an underscore character.

  • Python is case sensitive—it distinguishes between uppercase and lowercase letters. Thus TAX, Tax, and tax are three completely different variable names.

  • You cannot use Python keywords as variable names. For example, if, else, while, def, or, and, not, in, and is are some of Python’s keywords (we’ll learn what these are used for later in the book). If you try to use one as a variable, you’ll get an error (Figure 2.4).

    Table 2.3. Legal and Illegal Variable Names

    LEGAL

    ILLEGAL

    M

    “m”

    x1

    1x

    tax_rate

    tax rate

    taxRate

    taxRate!

    Else

    else

    Example 2.4. else is a Python keyword, so it cannot be used as a variable.

    >>> else = 25
    SyntaxError: invalid syntax (<pyshell#3>, line 1)

Assignment Statements

Assignment statements have three main parts: a left-hand side, an assignment operator, and a right-hand side (Figure 2.5). Assignment statements have two purposes: They define new variable names, and they make already-defined variables point to values. For instance:

>>> x = 5
>>> 2 * x + 1
11
>>> x = 99
Anatomy of an assignment statement. This makes var point to value. The left-hand side must always be a variable, while the right-hand side can be a variable, value, or any expression that evaluates to a value.

Figure 2.5. Anatomy of an assignment statement. This makes var point to value. The left-hand side must always be a variable, while the right-hand side can be a variable, value, or any expression that evaluates to a value.

The first assignment statement, x = 5, does double duty: It is an initialization statement. It tells Python that x is a variable and that it should be assigned the value 5. We can now use x anywhere an integer can be used.

The second assignment statement, x = 99, reassigns x to point to a different value. It does not create x, because x was already created by the previous assignment statement.

If you don’t initialize a variable, Python complains with an error:

>>> 2 * y + 1
Traceback (most recent call last):
   File "<pyshell#6>", line 1, in <module>
      2 * y + 1
NameError: name 'y' is not defined

This error message tells you that the variable y has not been defined, and so Python does not know what value to replace it with in the expression 2 * y + 1.

A variable can be assigned any value, even if it comes from other variables. Consider this sequence of assignments:

>>> x = 5
>>> x
5
>>> y = 'cat'
>>> y
'cat'
>>> x = y
>>> x
'cat'
>>> y
'cat'

How Variables Refer to Values

When Python sees an assignment statement x = expr, it treats it like the following command: Make x point to the value that expr evaluates to.

Keep in mind that expr can be any Python expression that evaluates to a value.

There’s a nice way of drawing diagrams to help understand sequences of assignments. For example, after the assignment rate = 0.04, you can imagine that RAM looks like Figure 2.6. Then, after rate_2008 = 0.06, we get Figure 2.7. Finally, rate = rate_2008 gives us Figure 2.8.

After running the statement rate = 0.04.

Figure 2.6. After running the statement rate = 0.04.

After rate_2008 = 0.06.

Figure 2.7. After rate_2008 = 0.06.

After rate = rate_2008. Notice that the value 0.04 no longer has any variable pointing to it. Thus Python automatically deletes it, a process known as garbage collection.

Figure 2.8. After rate = rate_2008. Notice that the value 0.04 no longer has any variable pointing to it. Thus Python automatically deletes it, a process known as garbage collection.

When a value no longer has any variable pointing to it (for example, 0.04 in Figure 2.7), Python automatically deletes it. In general, Python keeps track of all values and automatically deletes them when they are no lingered referenced by a variable. This is called garbage collection, and so Python programmers rarely need to worry about deleting values themselves.

Assignments don’t copy

It’s essential to understand that assignment statements don’t make a copy of the value they point to. All they do is label, and relabel, existing values. Thus, no matter how big or complex the object a variable points to, assignment statements are always exceedingly efficient.

Numbers and strings are immutable

An important feature of Python numbers and strings is that they are immutable—that is, they cannot be changed in any way, ever. Whenever it seems that you are modifying a number or string, Python is in fact making a modified copy (Figure 2.9).

Example 2.9. Whenever it appears that you are modifying a string, Python is in fact making a copy of the string. There is no way to modify a number or a string. Thus, Python stops you from doing crazy things like setting the value of 5 to be 1.

>>> s = 'apple'
>>> s + 's'
'apples'
>>> s
'apple'
>>> 5 = 1
SyntaxError: can't assign to literal (<pyshell#3>, line 1)

Multiple Assignment

Python has a convenient trick that lets you assign more than one variable at a time:

>>> x, y, z = 1, 'two', 3.0
>>> x
1
>>> y
'two'
>>> z
3.0
>>> x, y, z
(1, 'two', 3.0)

As the last statement shows, you can also display multiple values on one line by writing them as a tuple. Tuples always begin with an open round bracket (() and end with a closed round bracket ()).

Swapping variable values

A useful trick you can do with multiple assignment is to swap the values of two variables:

>>> a, b = 5, 9
>>> a, b
(5, 9)
>>> a, b = b, a
>>> a, b
(9, 5)

The statement a, b = b, a is said to assign values to a and b in parallel. Without using multiple assignment, the standard way to swap variables is like this:

>>> a, b = 5, 9
>>> temp = a
>>> a = b
>>> b = temp
>>> a, b
(9, 5)

Multiple assignment doesn’t do anything you can’t already do with regular assignment. It is just a convenient shorthand that we will sometimes be using.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.232.71