220 likes | 241 Views
Learn about integer and float data types in Python, differences with other languages, workshops for practice, math operations, and more.
E N D
Using Numbers Chapter 3
Data Types • Traditionally, programming languages have assigned different types of data for different types of numbers. • In many languages, there may be several numeric data types, perhaps six. • These different numeric data types developed because different amounts of memory were needed to store different kinds of numbers. • Fortunately, Python has just two data types: int (for integers) and float (for decimal numbers)
2 data types • Integers are just whole numbers, and can be positive or negative • Floats are numbers including fractions, expressed with a floating point (decimal). • When we type a “literal” (when we hard code a value into our program), Python recognizes whether that number is an integer or a float number, and assigns the correct data type.
Example of numeric data types • # change.py • # A program to calculate the value of some change in dollars • def main(): • print "Change Counter" • print • print "Please enter the count of each coin type." • quarters = input("Quarters: ") • dimes = input("Dimes: ") • nickels = input("Nickels: ") • pennies = input("Pennies: ") • total = quarters * 0.25 + dimes * 0.10 + nickels * .05 + pennies * .01 • print • print "The total value of your change is", total • main()
Workshop • Alter the change.py program so that you also count half-dollars and dollars (Kennedy and Susan B. Anthony coins)
The type() function • Python has a built-in function, type(), which tells us which data type a variable is. Just put the variable name between the parentheses. • Example: if you type newVar = 58 into the Python interpreter, then type(newVar), the result displayed will be <type ‘int’>
Workshop • In the Python interpreter, use the type() function to test the following variables: • num_units = 10 • num_students=25 • subtotal = 95.60 • score1 = 87 • average = 81.5 • users_online = 112 • total = 234.73
Operators and Operations • Python performs the basic math operations on both numeric data types
What is exponentiation? • Exponentiation refers to the power, or exponent, of a number. For example, 24 refers to two to the power of four, or 2*2*2*2. • In Python, you can use the exponentiation operator, **, to find the value of exponentiation. • 24 = 2 ** 4 = 16 • Try it in the Python interpreter
What is remainder? • You all know what the remainder is… the value that is left after division, if there is any value (other than zero) left at all. • E.g., the remainder of 10/7 is 3. • In Python, we would express this as 10 % 7 • Workshop: Use the interpreter to find the remainders:200/23, 17/4, 25/3, 28/9, 100/12, 10/6
What is absolute value? • The absolute value of a number is its numerical value regardless of its sign, that is, whether it’s negative or positive. • The built-in Python function abs() will always return a number’s absolute value. • The absolute value of 107 is 107; the absolute value of -33 is 33. • Workshop: use the interpreter to verify these absolute values
The Math library • There is a suite of built-in functions in Python that handle mathematical functions. See Table 3.2 in our textbook, p. 57 • To have access to all these built-in functions in Python, we need first to invoke the library: import math, the very first line of the program
Special Math Library functions • We’ll limit our use of the Math library to one “property” (built-in data) and three functions. • Pi is an approximate value for pi, or 3.14159….. In Python, this gets expressed as math.pi • exp(x) will give the exponential of x • ceil(x) gives the smallest whole number greater than or equal to x • and floor(x) gives the largest whole number less than or equal to x
Workshop • In teams of two, write a program which asks the user for the radius of a circle and then calculates the area of that circle.
Workshop • In teams of two, write a program that asks the user for a float number and then rounds that number up and down.
Integer limits and the L data type • Sometimes integer calculations produce an integer so huge it exceeds a language and computer’s ability to display it. In other words, the number exceeds memory allocations. • When a number exceeds memory, we get an Overflow error • Very long/short floats in Python will be represented in scientific, or exponential, notation. e+n will be added to the end of the number, indicating that 10n should be added to the number, where n is a power of 10.
Long ints • There is a third data type, long ints, which handle huge numbers. The memory allocated for a long int, or L, is flexible, not fixed like floats or integers. • When declaring a literal number, you can create a long int by just adding L to the end.21349L • New versions of Python automatically convert humongous integers to long ints. • Except when huge numbers are expected, you should always use the int type, because it’s much faster to compute.
Type conversion • Often it is necessary to convert one data type to another, since arithmetic operations on a computer are different for integers and floats. • Automatic type conversion: When there is a mixed-type expression, Python will automatically convert an integer to a float to perform the operation:average_wgt = 507.5/7 • Seven will automatically be converted to a float, and average_wgt will be a float.
Explicit type conversion • Sometimes we have to force a type conversion. For example, if we’re trying to find an average, and both factors are integers, the result may well be a float. But unless we tell Python to produce a float, it will whack off the fraction and return an integer. • num_students = 12 • sum = 1005 • avg = sum/num_students • This will produce an integer; we need a float
The solution is to force the result into the float type:avg = float(sum/num_students) • Python provides similar type conversion functions for integers and long integers: • >>>myInt = int(3.3)>>>print myInt3 • >>>myLInt = long(6.7)>>>print myLInt6L
Rounding • Python also has a built-in function, round(), which rounds a float to the nearest whole float value • >>>round(7.1490)7.0 • We can then convert the rounded number into an integer, if we wanted:>>>int(round(7.1490))7
Workshop: in pairs • Write a program which gets a number between 1 and 10 from the user and which also gets a random number from the computer. Print both so the user can compare the values. • The first line of your program should be import random • To generate a random number, type random.random()