3 Part 3 Python Workbook
3.4 Builtin Data Types
3.4.1 Numbers
The numbers you will use most commonly are likely to be integers and floats. Python also has long integers and complex numbers.
A few facts about numbers (in Python):
● Python will convert to using a long integer automatically when needed. You do not need to worry about exceeding the size of a (standard) integer.
● The size of the largest integer in your version of Python is in sys.maxint. To learn what it is, do:
>>> import sys
>>> print sys.maxint 9223372036854775807
The above show the maximum size of an integer on a 64bit version of Python.
● You can convert from integer to float by using the float constructor. Example:
>>> x = 25
>>> y = float(x)
>>> print y 25.0
● Python does "mixed arithmetic". You can add, multiply, and divide integers and floats. When you do, Python "promotes" the result to a float.
3.4.1.1 Literal representations of numbers
An integer is constructed with a series of digits or the integer constructor (int(x)). Be aware that a sequence of digits beginning with zero represents an octal value. Examples:
>>> x1 = 1234
>>> x2 = int('1234')
>>> x3 = 25
>>> x1 1234
>>> x2 1234
>>> x3
25
A float is constructed either with digits and a dot (example, 12.345) or with
engineering/scientific notation or with the float constructor (float(x)). Examples:
>>> x1 = 2.0e3
>>> x1 = 1.234
>>> x2 = 1.234
>>> x3 = float('1.234')
>>> x4 = 2.0e3
>>> x5 = 2.0e3
>>> print x1, x2, x3, x4, x5 1.234 1.234 1.234 2000.0 0.002
Exercises:
Construct these numeric values:
1. Integer zero
2. Floating point zero
3. Integer one hundred and one 4. Floating point one thousand
5. Floating point one thousand using scientific notation
6. Create a positive integer, a negative integer, and zero. Assign them to variables 7. Write several arithmetic expressions. Bind the values to variables. Use a variety
of operators, e.g. +, , /, *, etc. Use parentheses to control operator scope.
8. Create several floats and assign them to variables.
9. Write several arithmetic expressions containing your float variables.
10. Write several expressions using mixed arithmetic (integers and floats). Obtain a float as a result of division of one integer by another; do so by explicitly
converting one integer to a float.
Solutions:
1. 0
2. 0.0, 0., or .0 3. 101
4. 1000.0 5. 1e3 or 1.0e3
6. Asigning integer values to variables:
In [7]: value1 = 23 In [8]: value2 = 14 In [9]: value3 = 0 In [10]: value1 Out[10]: 23 In [11]: value2 Out[11]: 14 In [12]: value3 Out[12]: 0
7. Assigning expression values to variables:
value1 = 4 * (3 + 5)
value2 = (value1 / 3.0) 2
8. Assigning floats to variables:
value1 = 0.01 value2 = 3.0 value3 = 3e4
9. Assigning expressions containing varialbes:
value4 = value1 * (value2 value3)
value4 = value1 + value2 + value3 value4
10. Mixed arithmetic:
x = 5 y = 8
z = float(x) / y
You can also construct integers and floats using the class. Calling a class (using parentheses after a class name, for example) produces an instance of the class.
Exercises:
1. Construct an integer from the string "123".
2. Construct a float from the integer 123.
3. Construct an integer from the float 12.345.
Solutions:
1. Use the int data type to construct an integer instance from a string:
int("123")
2. Use the float data type to construct a float instance from an integer:
float(123)
3. Use the int data type to construct an integer instance from a float:
int(12.345) # > 12
Notice that the result is truncated to the integer part.
3.4.1.2 Operators for numbers
You can use most of the familiar operators with numbers, for example:
+ * ** / // %
<< >> & | ^ ~
< > <= >= == != <>
Look here for an explanation of these operators when applied to numbers: Numeric Types int, float, long, complex http://docs.python.org/lib/typesnumeric.html.
Some operators take precedence over others. The table in the Web page just referenced above also shows that order of priority.
Here is a bit of that table:
All numeric types (except complex) support the following operations, sorted by ascending priority (operations in the same box have the same
priority; all numeric operations have a higher priority than comparison
operations):
Operation Result
x + y sum of x and y
x y difference of x and y x * y product of x and y x / y quotient of x and y
x // y (floored) quotient of x and y x % y remainder of x / y
x x negated +x x unchanged
abs(x) absolute value or magnitude of x int(x) x converted to integer
long(x) x converted to long integer float(x) x converted to floating point
complex(re,im) a complex number with real part re, imaginary part im. im defaults to zero.
c.conjugate() conjugate of the complex number c
divmod(x, y) the pair (x // y, x % y) pow(x, y) x to the power y
x ** y x to the power y
Notice also that the same operator may perform a different function depending on the data type of the value to which it is applied.
Exercises:
1. Add the numbers 3, 4, and 5.
2. Add 2 to the result of multiplying 3 by 4.
3. Add 2 plus 3 and multiply the result by 4.
Solutions:
1. Arithmetic expressions are follow standard infix algebraic syntax:
3 + 4 + 5
2. Use another infix expression:
2 + 3 * 4
Or:
2 + (3 * 4)
But, in this case the parentheses are not necessary because the * operator binds more tightly than the + operator.
3. Use parentheses to control order of evaluation:
(2 + 3) * 4
Note that the * operator has precedence over (binds tighter than) the + operator, so the parentheses are needed.
Python does mixed arithemetic. When you apply an operation to an integer and a float, it promotes the result to the "higher" data type, a float.
If you need to perform an operation on several integers, but want use a floating point operation, first convert one of the integers to a float using float(x), which effectively creates an instance of class float.
Try the following at your Python interactive prompt:
1. 1.0 + 2
2. 2 / 3 Notice that the result is truncated.
3. float(2) / 3 Notice that the result is not truncated.
Exercises:
1. Given the following assignments:
x = 20 y = 50
Divide x by y giving a float result.
Solutions:
1. Promote one of the integers to float before performing the division:
z = float(x) / y
3.4.1.3 Methods on numbers
Most of the methods implemented by the data types (classes) int and float are special methods that are called through the use of operators. Special methods often have names that begin and end with a double underscore. To see a list of the special names and a bit of an indication of when each is called, do any of the following at the Python interactive prompt:
>>> help(int)
>>> help(32)
>>> help(float)
>>> help(1.23)
>>> dir(1)
>>> dir(1.2)