• Tidak ada hasil yang ditemukan

Chapter 2 Chapter 2

12.31 Elements of Programs

2.3. Elements of Programs

You can see that Susan used the values ofO and 100 to test her program. It looks pretty good, and she is satisfied with her solution. She is especially pleased that no debugging seems necessary (which is very unusual).

False None True and as

assert break

class finally is

continue for lambda

def from nonlocal

del global not

elif if or

else import pass

except 1n . ra1se . Table 2.1: Python keywords

return try

while with yield

Python also includes quite a number of built-in functions, such as the print function that we've already been using. While it's technically legal to (re)use the built-in function-name identifiers for other purposes, it's generally a very bad

idea to do so. For example, if you redefine the meaning of print, then you will no longer be able to print things out. You will also seriously confuse any Python programmers who read your program; they expect print to refer to the built-in function. A complete list of the built-in functions can be found in Appendix A.

12.3.21 Expressions

Programs manipulate data. So far, we have seen two different kinds of data in our example programs: numbers and text. We'll examine these different data types in great detail in later chapters. For now, you just need to keep in mind that all data has to be stored on the computer in some digital format, and different types of data are stored in different ways.

The fragments of program code that produce or calculate new data values are called expressions. The simplest kind of expression is a literal. A literal is used to indicate a specific value. In chaos . py you can find the numbers 3 . 9 and 1. The convert. py program contains 9, 5, and 32. These are all examples of numeric literals, and their meaning is obvious: 32 represents, well, 32 (the number 32).

Our programs also manipulated textual data in some simple ways. Com­

puter scientists refer to textual data as strings. You can think of a string as just a sequence of printable characters. A string literal is indicated in Python by en­

closing the characters in quotation marks (" "). If you go back and look at our example programs, you will find a number of string literals such as: "Hello"

and "Enter a number between 0 and 1: ". These literals produce strings

2.3. Elements of Programs

containing the quoted characters. Note that the quotes themselves are not part of the string. They are just the mechanism to tell Python to create a string.

The process of turning an expression into an underlying data type is called

evaluation. When you type an expression into a Python shell, the shell evaluates the expression and prints out a textual representation of the result. Consider this small interaction:

>>> 32 32

>>> "Hello"

'Hello'

>>> "3211 '32'

Notice that when the shell shows the value of a string, it puts the sequence of characters in single quotes. This is a way of letting us know that the value is actually text, not a number (or other data type). In the last interaction, we see that the expression "32" produces a string, not a number. In this case, Python is actually storing the characters "3" and "2," not a representation of the number 32. If that's confusing right now, don't worry too much about it; it will become clearer when we discuss these data types in later chapters.

A simple identifier can also be an expression. We use identifiers as variables to give names to values. When an identifier appears as an expression, its value is retrieved to provide a result for the expression. Here is an interaction with the Python interpreter that illustrates the use of variables as expressions:

>>> X = 5

>>> X 5

>>> print ( x) 5

>>> print (spam)

Traceback (most recent call last) :

File "<stdin>", line 1, in <module>

NameError: name 'spam' is not defined

First the variable x is assigned the value

5

(using the numeric literal 5). In the second line of interaction, we are asking Python to evaluate the expression x. In response, the Python shell prints out 5, which is the value that was just assigned to x. Of course, we get the same result when we explicitly ask Python to print x

33

using a print statement. The last interaction shows what happens when we try to use a variable that has not been assigned a value. Python cannot find a value, so it reports a NameError. This says that there is no value with that name. The important lesson here is that a variable must always be assigned a value before it can be used in an expression.

More complex and interesting expressions can be constructed by combin­

ing simpler expressions with operators. For numbers, Python provides the nor­

mal set of mathematical operations: addition, subtraction, multiplication, divi­

sion, and exponentiation. The corresponding Python operators are +, -, *, I,

and **· Here are some examples of complex expressions from chaos. py and convert . py:

3.9 * x * (1 - x) 915 * celsius + 32

Spaces are irrelevant within an expression. The last expression could have been written 9l5 *celsius+32 and the result would be exactly the same. Usually it's a good idea to place some spaces in expressions to make them easier to read.

Python's mathematical operators obey the same rules of precedence and as­

sociativity that you learned in your math classes, including using parentheses to modify the order of evaluation. You should have little trouble constructing complex expressions in your own programs. Do keep in mind that only the round parentheses are allowed in numeric expressions, but you can nest them if necessary to create expressions like this:

((x1 - x2) I 2 *n) + (spam I k **3)

By the way, Python also provides operators for strings. For example, you can

"add" strings.

>>> "Bat" + "man"

'Batman'

This is called concatenation. As you can see, the effect is to create a new string that is the result of "gluing" the strings together. You'll see a lot more string operations in Chapter 5.