WORKING WITH NUMBERS
Python provides arithmetic operations for manipulating numbers in a straightforward manner that is similar to other programming languages. The following examples involve arithmetic operations on integers:
>>> 2+2 4 >>> 4/3 1 >>> 3*8 24
The following example assigns numbers to two variables and computes their product:
>>> x = 4 >>> y = 7 >>> x * y 28
The following examples demonstrate arithmetic operations involving integers:
>>> 2+2 4 >>> 4/3 1 >>> 3*8 24
Notice that division (“/”) of two integers is actually truncation in which only the integer result is retained. The following example converts a floating point number into exponential form:
>>> fnum = 0.00012345689000007 >>> "%.14e"%fnum '1.23456890000070e-04'
You can use the int() function and the float() function to convert strings to numbers:
word1 = "123"
word2 = "456.78"
var1 = int(word1)
var2 = float(word2)
print("var1: ",var1," var2: ",var2)
The output from the preceding code block is here:
var1: 123 var2: 456.78
Alternatively, you can use the eval() function:
word1 = "123"
word2 = "456.78"
var1 = eval(word1)
var2 = eval(word2)
print("var1: ",var1," var2: ",var2)
If you attempt to convert a string that is not a valid integer or a floating point number, Python raises an exception, so it is advisable to place your code in a try/except block.
Working with Other Bases
Numbers in Python are in base 10 (the default), but you can easily convert numbers to other bases. For example, the following code block initializes the variable x with the value 1234, and then displays that number in base 2, 8, and 16:
>>> x = 1234 >>> bin(x) '0b10011010010' >>> oct(x) '0o2322' >>> hex(x) '0x4d2' >>>
Use the format() function if you want to suppress the 0b, 0o, or 0x prefixes, as shown here:
>>> format(x, 'b') '10011010010' >>> format(x, 'o') '2322' >>> format(x, 'x') '4d2'
Negative integers are displayed with a negative sign:
>>> x = -1234 >>> format(x, 'b') '-10011010010' >>> format(x, 'x') '-4d2'
The chr() Function
The chr() function takes a positive integer as a parameter and converts it to its corresponding alphabetic value (if one exists). The letters A through Z have decimal representation of 65 through 91 (which corresponds to hexadecimal 41 through 5b), and the lowercase letters a through z have decimal representation 97 through 122 (hexadecimal 61 through 7b).
Here is an example of using the chr() function to print uppercase A:
>>> x=chr(65) >>> x 'A'
The following code block prints the ASCII values for a range of integers:
result = ""
for x in range(65,91):
print(x, chr(x))
result = result+chr(x)+' '
print("result: ",result)
NOTE Python 2 uses ASCII strings whereas Python 3 uses Unicode.
You can represent a range of characters with the following line:
for x in range(65,91):
However, the following equivalent code snippet is more intuitive:
for x in range(ord('A'), ord('Z')):
If you want to display the result for lowercase letters, change the preceding range from (65,91) to either of the following statements:
for x in range(65,91):
for x in range(ord('a'), ord('z')):
The round() Function
The round() function enables you to round decimal values to the nearest precision:
>>> round(1.23, 1) 1.2 >>> round(-3.42,1) -3.4
Formatting Numbers
Python allows you to specify the number of decimal places of precision to use when printing decimal numbers, as shown here:
>>> x = 1.23456
>>> format(x, '0.2f')
'1.23'
>>> format(x, '0.3f')
'1.235'
>>> 'value is {:0.3f}'.format(x) 'value is 1.235'
>>> from decimal import Decimal
>>> a = Decimal('4.2')
>>> b = Decimal('2.1')
>>> a + b
Decimal('6.3')
>>> print(a + b)
6.3
>>> (a + b) == Decimal('6.3')
True
>>> x = 1234.56789
>>> # Two decimal places of accuracy
>>> format(x, '0.2f')
'1234.57'
>>> # Right justified in 10 chars, one-digit accuracy
>>> format(x, '>10.1f')
' 1234.6'
>>> # Left justified
>>> format(x, '<10.1f') '1234.6 '
>>> # Centered
>>> format(x, '^10.1f') ' 1234.6 '
>>> # Inclusion of thousands separator
>>> format(x, ',')
'1,234.56789'
>>> format(x, '0,.1f')
'1,234.6'