Unlicensed-7-PDF23-24_Haltermanpythonbook

advertisement
2.1.
14
INTEGER VALUES
The expression Fred (without quotes) was not accepted by the interpreter because of the missing quotation
marks.
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 converts the string representation of an integer to an actual integer, and the
str function converts an integer expression to a string:
>>>
4
>>>
'4'
>>>
'5'
>>>
5
4
str(4)
'5'
int('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 perform these kinds of transformations. In
fact, the expression str('4') is more easily expressed as 4, so the utility of the str and int functions will
not become apparent until we introduce variables in Section 2.2.
Any integer has a string representation, but not all strings have an integer equivalent:
©2011 Richard L. Halterman
Draft date: November 13, 2011
2.1.
15
INTEGER VALUES
>>> str(1024)
'1024'
>>> int('wow')
Traceback (most recent
File "<stdin>", line 1,
ValueError: invalid literal
>>> int('3.4')
Traceback (most recent
File "<stdin>", line 1,
ValueError: invalid literal
call last):
in <module>
for int() with base 10: 'wow'
call last):
in <module>
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'
©2011 Richard L. Halterman
Draft date: November 13, 2011
Download