Python Numbers CMSC 201 Fall 2012 Instructor: John Park Lecture Section 01 Discussion Sections 02-08, 16, 17 1 (adapted from Meeden, Evans & Mayberry) 2 Learning Outcomes To become familiar with the basic numeric data types in Python To understand how numbers are represented and stored in memory To be aware of the Python math library To understand the Accumulator pattern To be able to read and write programs that process numerical data 3 1
Number Basics Computers were originally designed, as the name suggests, to crunch numbers Why are numbers important? Code can be thought of as a mathematical formula Number manipulation lies at the heart of most programs Computers store all information in binary, base 2. Each bit of memory is either on, 1, or off, 0. What does the binary number system look like? 4 The Decimal Number System The decimal number system is a positional number system. Example: 5 6 2 1 1 X 10 0 = 1 10 3 10 2 10 1 10 0 2 X 10 1 = 20 6 X 10 2 = 600 5 X 10 3 = 5000 5 The Decimal Number System The decimal number system is also known as base 10. The values of the positions are calculated by taking 10 to some power. Why is the base 10 for decimal numbers? Because we use 10 digits, the digits 0 through 9. The decimal number system, and other number systems, are symbolic representations of concrete quantities 6 2
The Binary Number System The binary number system is also known as base 2. The values of the positions are calculated by taking 2 to some power. Why is the base 2 for binary numbers? Because we use 2 digits, the digits 0 and 1. 7 The Binary Number System The binary number system is also a positional numbering system. Instead of using ten digits, 0-9, the binary system uses only two digits, 0 and 1. Example of a binary number and the values of the positions: 1 0 0 1 1 0 1 2 6 2 5 2 4 2 3 2 2 2 1 2 0 8 Converting from Binary to Decimal 1 0 0 1 1 0 1 1 X 2 0 = 1 2 6 2 5 2 4 2 3 2 2 2 1 2 0 0 X 2 1 = 0 1 X 2 2 = 4 2 0 = 1 2 4 = 16 1 X 2 3 = 8 2 1 = 2 2 5 = 32 0 X 2 4 = 0 2 2 = 4 2 6 = 64 0 X 2 5 = 0 2 3 = 8 2 7 = 128 1 X 2 6 = 64 77 10 9 3
Converting from Binary to Decimal Practice conversions: Binary Decimal 11101 1010101 100111 10 Geek Joke #1 Seen on a random T-shirt: There are 10 kinds of people in the world: Those who understand binary and those who don t 11 Converting from Decimal to Binary Make a list of the binary place values up to the number being converted. (In the example below, 2 5 is the largest possible leftmost position) Perform successive divisions by 2, placing the remainder of 0 or 1 in each of the positions from right to left. Continue until the quotient is zero. Example: 42 10 64 32 16 8 4 2 1 _ 1 0 1 0 1 0 2 6 2 5 2 4 2 3 2 2 2 1 2 0 12 4
Converting from Decimal to Binary Practice conversions: Decimal 59 82 175 Binary 13 Working with Large Numbers 0 1 0 1 0 0 0 0 1 0 1 0 0 1 1 1 =? Humans can t work well with binary numbers; there are too many digits to deal with. Memory addresses and other data can be quite large. Therefore, we sometimes use the hexadecimal and octal number systems. 14 The Hexadecimal Number System The hexadecimal number system is also known as base 16. The values of the positions are calculated by taking 16 to some power. Why is it base 16 for hexadecimal numbers? Because we use 16 symbols, the digits 0 through 9 and the letters A through F. 15 5
The Hexadecimal Number System Binary Decimal Hexadecimal Binary Decimal Hexadecimal 0 0 0 1010 10 A 1 1 1 1011 11 B 10 2 2 1100 12 C 11 3 3 1101 13 D 100 4 4 1110 14 E 101 5 5 1111 15 F 110 6 6 111 7 7 1000 8 8 1001 9 9 16 The Hexadecimal Number System Example of a hexadecimal number and the values of the positions: 3 C 8 B 0 5 1 16 6 16 5 16 4 16 3 16 2 16 1 16 0 17 The Octal Number System Example of an octal number and the values of the positions: 1 3 0 0 2 4 8 5 8 4 8 3 8 2 8 1 8 0 Binary equivalent: 1 011 000 000 010 100 = 1011000000010100 18 6
Example of Equivalent Numbers Binary: 1 1 0 1 0 0 0 0 1 0 1 0 0 1 1 1 2 Octal: 150247 8 Decimal: 53415 10 Hexadecimal: D0A7 16 19 Notice how the number of digits gets smaller as the base increases. But Why Use Hex or Octal? Simple: can divide binary numbers into equalsized sets of bits, then convert directly This is not true of decimal-to- {binary,hex,octal} 20 Python Operators Operation Python Operator Addition + Subtraction - Multiplication * Division / Exponentiation ** Modulus % 21 7
In-Place Operators All of the arithmetic operators have an inplace version for convenience: x += 1 # Adds 1 to x, like x = x + 1 x -= 2 # equiv. to x = x - 2 x *= 6 x /= 5 x **= 3 x %= 2 22 Integers vs. Floats Integers Truncates floating point results Always has exact values Used for things that are countable Faster arithmetic operations Stored in 32 bits (4 bytes) one bit holds the sign and 31 bits store the number Largest possible int is 2147483647 Floats Allows floating point results Sometimes has approximations of values Used for continuous data Slower arithmetic operations Stored in 64 bits (8 bytes) one bit holds the sign, 11 bits hold the exponent and 52 bits hold the mantissa Much larger, since numbers are stored in exponential form 23 Integers vs. Floats Many languages are difficult to use when a program requires working with integers that are larger than 2147483647. Since a different data type has to be used to hold the values, the code has to be rewritten. Python handles this problem for you by seamlessly continuing to run the program by switching the data type to long ints automatically. 24 8
Long Integers The third numeric type in Python is the long int. A long int doesn't have a fixed size. It expands to the length the number needs, limited only by the amount of memory your computer has. 25 Working with Integers >>> 7 / 3 2 Both the dividend and the divisor are integers, causing the integer arithmetic unit to do the calculation. The integer arithmetic unit can take only integers as input and can produce only integer results. This is called integer division and it can be quite useful, as in the following example. 26 Working with Integers >>> length = input("enter a length in inches : ") Enter a length in inches : 27 >>> feet = length / 12 >>> inches = length % 12 >>> print length, "inches is ", feet, "feet and ", inches, "inches 27 inches is 2 feet and 3 inches 27 9
Working with Floating-Point Numbers >>> 7.0 / 3.0 2.3333333333333335 Here the floating-point arithmetic unit had to be used for the calculation. Floats are not always exact answers. We all know the last digit here should be a 3, not a 5. If the number is a repeating decimal, or if it has more decimal places than there is space, the value may be slightly off or truncated. 28 Types How can you tell what type of number you are using? >>> type(1) <type 'int'> >>> type(1.0) <type 'float'> >>> type(1.0 + 2) <type 'float'> >>> mynum = 4 >>> type(mynum) <type 'int'> >>> mynum = mynum + 2.0 >>> type(mynum) <type 'float'> 29 Types Notice that numbers are automatically converted to floats if an operation contains both integers and floats. Preserves accuracy of calculation Explicit conversion can be done with float() >>> type(float(1)) <type 'float'> >>> float(7)/3 2.3333333333333335 30 10
Variable Types Unlike many other languages, Python does not require you to preselect a type for your variables No variable declarations needed Variables are created the first time they are written into Their type can change to fit the current need when rewritten 31 Math Library The math library contains many useful math functions. You must have 'import math' in your program to use the functions in the math library. More information can be found on the official Python documentation page. 32 Math Library Functions Function cos(x),sin(x),tan(x) log(x,base) floor(x) ceil(x) sqrt(x) pi e Purpose Trigonometric functions Logarithm of x with given base Floor function (closest integer less than or equal to x) Ceiling function (closest integer greater than or equal to x) Square root of x Constant that represents pi Constant that represents e 33 11