Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3.  Traditionally, programming languages have assigned different types of data for different types of numbers.  In many languages, there may.

Similar presentations


Presentation on theme: "Chapter 3.  Traditionally, programming languages have assigned different types of data for different types of numbers.  In many languages, there may."— Presentation transcript:

1 Chapter 3

2  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)

3  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.

4  # 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()

5  Alter the change.py program so that you also count half-dollars and dollars (Kennedy and Susan B. Anthony coins)

6  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

7  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

8  Python performs the basic math operations on both numeric data types Operatoroperation +Addition -Subtraction *Multiplication /Division **Exponentiation %Remainder abs()Absolute value

9  Exponentiation refers to the power, or exponent, of a number. For example, 2 4 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.  2 4 = 2 ** 4 = 16  Try it in the Python interpreter

10  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

11  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

12  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

13  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

14  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.

15  In teams of two, write a program that asks the user for a float number and then rounds that number up and down.

16  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 10 n should be added to the number, where n is a power of 10.

17  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.

18  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.

19  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

20  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 myInt 3  >>>myLInt = long(6.7) >>>print myLInt 6L

21  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

22  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()


Download ppt "Chapter 3.  Traditionally, programming languages have assigned different types of data for different types of numbers.  In many languages, there may."

Similar presentations


Ads by Google