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 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 integer expression. 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:
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 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 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 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 nonnumeric characters:
>>> "Fred"
'Fred'
>>> 'Fred'
'Fred'
Python recognizes both single quotes (') and double quotes (") as valid ways to delimit a string value. The
word delimit means to determine the boundaries or limits of something. The left ' symbol determines the
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 expression Fred.
It is important to note that the expressions 4 and '4' are different. One is an integer expression and
the other is a string expression. All expressions in Python have a type. The type of an expression indicates
the kind of expression it is. An expression’s type is sometimes denoted as its class. At this point we have
considered only integers and strings. The built in type function reveals the type of any Python expression:
>>> type(4)
<class 'int'>
>>> type('4')
<class 'str'>
Python associates the type name int with integer expressions and str with string expressions.
The built-in int function creates an actual integer object from a string that looks like an integer, and the
str function creates a string object from the digits that make up an integer:
>>> 4
4
>>> str(4)
'4'
>>> '5'
'5'
>>> int('5')
5
The expression str(4) evaluates to the string value '4', and int('5') evaluates to the integer value 5.
The int function applied to an integer evaluates simply to the value of the integer itself, and similarly str
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 transform an object into itself—the expression
int(4) is more easily expressed as 4, so the utility of the str and int functions 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, neither wow 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 the int function 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 expression 5 + 10 is very different from '5' + '10'. The plus operator
splices two strings together in a process known as concatenation. Mixing the two types directly is not
allowed:
>>> '5' + 10
Traceback (most recent call last):
>>> 4
4
>>> str(4)
'4'
>>> '5'
'5'
>>> int('5')
5
The expression str(4) evaluates to the string value '4', and int('5') evaluates to the integer value 5.
The int function applied to an integer evaluates simply to the value of the integer itself, and similarly str
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 transform an object into itself—the expression
int(4) is more easily expressed as 4, so the utility of the str and int functions 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, neither wow 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 the int function 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 expression 5 + 10 is very different from '5' + '10'. The plus operator
splices two strings together in a process known as concatenation. Mixing the two types directly is not
allowed:
>>> '5' + 10
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't convert 'int' object to str implicitly
>>> 5 + '10'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
but the int and str functions can help:
>>> 5 + int('10')
15
>>> '5' + str(10)
'510'
The type function can determine the type of the most complicated expressions:
>>> type(4)
<class 'int'>
>>> type('4')
<class 'str'>
>>> type(4 + 7)
<class 'int'>
>>> type('4' + '7')
<class 'str'>
>>> type(int('3') + int(4))
<class 'int'>
Commas may not appear in Python integer values. The number two thousand, four hundred sixty-eight
would be written 2468, not 2,468.
In mathematics, integers are unbounded; said another way, the set of mathematical integers is infinite. In
Python, integers may be arbitrarily large, but the larger the integer, the more memory required to represent
it. This means Python integers theoretically can be as large or as small as needed, but, since a computer
has a finite amount of memory (and the operating system may limit the amount of memory allowed for a
running program), in practice Python integers are bounded by availabe memory.
0 Comments