Integer and String Values

Một phần của tài liệu AMECO python book 2017 FUNDAMENTALS OF PYTHON Industry 4 0 Innovation LABIndustry (Trang 25 - 29)

The number four (4) is an example of anumeric value. In mathematics, 4 is anintegervalue. 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 nonnumeric 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 integerexpression. Python supports other types of expressions besides integer expressions. An expression is a basic building block of a Python 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:

2.1. INTEGER AND STRING VALUES 14

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AM

D64)] on win32

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 theread, eval, print loop. This means the interactive shell’s sole activity consists of

1. readingthe text entered by the user,

2. attempting toevaluatethe user’s input in the context of what the user has entered up that point, and 3. printingits evaluation of the user’s input.

If the user enters a4, the shell interprets it as a 4. If the user entersx = 10, a statement has has no overall value itself, the shell prints nothing. If the user then entersx, the shell prints the evaluation ofx, which is 10.

If the user next entersy, the shell reports a error becauseyhas not been defined in a previous interaction.

Python uses the+symbol with integers to perform normal arithmetic 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 aprintstatement 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 astringvalue.

A string is a sequence of characters. Strings most often contain nonnumeric characters:

>>> "Fred"

'Fred'

>>> 'Fred' 'Fred'

Python recognizes both single quotes (') and double quotes (") as valid ways to delimit a string value. The worddelimitmeans to determine the boundaries or limits of something. The left'symbol determines the

2.1. INTEGER AND STRING VALUES 15 beginning of a string, and the right'symbol that follows specifies the end of the string. 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 two kinds of quotation marks when used to delimit a particular string, as the following interactive sequence shows:

>>> 'ABC' 'ABC'

>>> "ABC"

'ABC'

>>> 'ABC"

File "<stdin>", line 1 'ABC"

SyntaxError: EOL while scanning string literalˆ

>>> "ABC'

File "<stdin>", 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 "<stdin>", line 1, in <module>

NameError: name 'Fred' is not defined

Notice that with the missing quotation marks the interpreter does not accept the expressionFred.

It is important to note that the expressions4and'4'are different. One is an integer expression and the other is a string expression. All expressions in Python have atype. The type of an expression indicates the kind of expression it is. An expression’s type is sometimes denoted as itsclass. At this point we have considered only integers and strings. The built intypefunction reveals the type of any Python expression:

>>> type(4)

<class 'int'>

>>> type('4')

<class 'str'>

Python associates the type nameintwith integer expressions andstrwith string expressions.

The built-inintfunction creates an actual integer object from a string that looks like an integer, and the strfunction creates a string object from the digits that make up an integer:

2.1. INTEGER AND STRING VALUES 16

>>> 4 4>>> str(4) '4'>>> '5' '5'>>> int('5') 5

The expressionstr(4)evaluates to the string value'4', andint('5') evaluates to the integer value 5.

Theintfunction applied to an integer evaluates simply to the value of the integer itself, and similarlystr applied to a string results in the same value as the original string:

>>> int(4) 4

>>> str('Judy') 'Judy'

As you might guess, there is little reason for a programmer to tansform an object into itself—the expression int(4)is more easily expressed as4, so the utility of thestrandintfunctions will not become apparent until we introduce variables (Section 2.2) and need to process user input (Section 2.6).

Any integer has a string representation, but not all strings have an integer equivalent:

>>> str(1024) '1024'

>>> int('wow')

Traceback (most recent call last):

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

ValueError: invalid literal for int() with base 10: 'wow'

>>> int('3.4')

Traceback (most recent call last):

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

ValueError: invalid literal for int() with base 10: '3.4'

In Python, neitherwow nor 3.4 represent valid integer expressions. In short, if the contents of the string (the characters that make it up) look like a valid integer number, you safely can apply theintfunction to produce the represented integer.

The plus operator (+) works differently for strings; consider:

>>> 5 + 10 15>>> '5' + '10' '510'

>>> 'abc' + 'xyz' 'abcxyz'

As you can see, the result of the expression5 + 10is very different from'5' + '10'. The plus operator splices two strings together in a process known asconcatenation. Mixing the two types directly is not allowed:

>>> '5' + 10

Traceback (most recent call last):

Một phần của tài liệu AMECO python book 2017 FUNDAMENTALS OF PYTHON Industry 4 0 Innovation LABIndustry (Trang 25 - 29)

Tải bản đầy đủ (PDF)

(670 trang)