PDF Google Drive Downloader v1.1


Báo lỗi sự cố

Nội dung text 1.Τιμές και μεταβλητές.pdf

11 Chapter 2 Values and Variables In this chapter we explore some building blocks that are used to develop Python programs. We experiment with the following concepts: • numeric values • variables • assignment • identifiers • reserved words In the next chapter we will revisit some of these concepts in the context of other data types. 2.1 Integer Values The number four (4) is an example of a numeric value. In mathematics, 4 is an integer value. Integers are whole numbers, which means they have no fractional parts, and they can be positive, negative, or zero. Examples of integers include 4, −19, 0, and −1005. In contrast, 4.5 is not an integer, since it is not a whole number. Python supports a number of numeric and non-numeric values. In particular, Python programs can use integer values. The Python statement print(4) prints the value 4. Notice that unlike Listing 1.1 (simple.py) and Listing 1.2 (arrow.py) no quotation marks (") appear in the statement. The value 4 is an example of an integer expression. Python supports other types of expressions besides integer expressions. An expression is part of a statement. The number 4 by itself is not a complete Python statement and, therefore, cannot be a program. The interpreter, however, can evaluate a Python expression. You may type the enter 4 directly into the interactive interpreter shell: ©2011 Richard L. Halterman Draft date: November 13, 2011
2.1. INTEGER VALUES 12 Python 3.2.1 (default, Jul 10 2011, 21:51:15) [MSC v.1500 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "license" for more information. >>> 4 4 >>> The interactive shell attempts to evaluate both expressions and statements. In this case, the expression 4 evaluates to 4. The shell executes what is commonly called the read, eval, print loop. This means the interactive shell’s sole activity consists of 1. reading the text entered by the user, 2. attempting to evaluate the user’s input in the context of what the user has entered up that point, and 3. printing its evaluation of the user’s input. If the user enters a 4, the shell interprets it as a 4. If the user enters x = 10, a statement has has no overall value itself, the shell prints nothing. If the user then enters x, the shell prints the evaluation of x, which is 10. If the user next enters y, the shell reports a error because y has not been defined in a previous interaction. Python uses the + symbol with integers to perform normal arithemtic addition, so the interactive shell can serve as a handy adding machine: >>> 3 + 4 7 >>> 1 + 2 + 4 + 10 + 3 20 >>> print(1 + 2 + 4 + 10 + 3) 20 The last line evaluated shows how we can use the + symbol to add values within a print statement that could be part of a Python program. Consider what happens if we use quote marks around an integer: >>> 19 19 >>> "19" ’19’ >>> ’19’ ’19’ Notice how the output of the interpreter is different. The expression "19" is an example of a string value. A string is a sequence of characters. Strings most often contain non-numeric characters: ©2011 Richard L. Halterman Draft date: November 13, 2011
2.1. INTEGER VALUES 13 >>> "Fred" ’Fred’ >>> ’Fred’ ’Fred’ Python recognizes both single quotes (’) and double quotes (") as valid ways to delimit a string value. If a single quote marks the beginning of a string value, a single quote must delimit the end of the string. Similarly, the double quotes, if used instead, must appear in pairs. You may not mix the quotes when representing a string: >>> ’ABC’ ’ABC’ >>> "ABC" ’ABC’ >>> ’ABC" File "", line 1 ’ABC" ˆ SyntaxError: EOL while scanning string literal >>> "ABC’ File "", line 1 "ABC’ ˆ SyntaxError: EOL while scanning string literal The interpreter’s output always uses single quotes, but it accepts either single or double quotes as valid input. Consider the following interaction sequence: >>> 19 19 >>> "19" ’19’ >>> ’19’ ’19’ >>> "Fred" ’Fred’ >>> ’Fred’ ’Fred’ >>> Fred Traceback (most recent call last): File "", line 1, in NameError: name ’Fred’ is not defined ©2011 Richard L. Halterman Draft date: November 13, 2011

Tài liệu liên quan

x
Báo cáo lỗi download
Nội dung báo cáo



Chất lượng file Download bị lỗi:
Họ tên:
Email:
Bình luận
Trong quá trình tải gặp lỗi, sự cố,.. hoặc có thắc mắc gì vui lòng để lại bình luận dưới đây. Xin cảm ơn.