Python Programming Lecture 3, Fall 2009

http://grace.evergreen.edu/mon/asn-python.html

Numeric Data Types, Ch 3.1

  1. What is an Integer? {...,-1,0,1,2,...}
  2. What is an Int? {-maxint-1, -maxint, ..., -1,0,1,2, ..., maxint}
  3. What is a Real?
  4. What is a Float? Anything you can write in scientific notation.
  5. The computer usually uses base 2 or base 16 scientific notation.
  6. Python Long Int. { ..., -1, 0,1,2, ...} up to the available memory.
  7. Example: 2147483648L notice the trailing L
  8. Python does automatic conversion in mixed expressions. Eg: 9.0/5, 2**65
  9. Every number literal has a type. Every expression has a type. Every literal has a type. Sensible mixed expressions have a type, non-sensible mixed expressions give an error. Eg: "hi" + "there" makes sense as string concatenation. "hi"/3 makes no sense.
  10. Python type command is handy for checking types. Eg: type(3/4.0), type(3/4).
  11. Built in operators on numeric types: + - * / ** % abs(). Try each of these. Especially try 2**0.5.
  12. See p470 Python language built-in operator summary.

Data representation in the computer, Ch 3.4, 3.5

  1. Counting in Binary.
  2. Word size and maxint.
  3. Negative numbers in two-s complement.
  4. Storage of numbers in base 2 words of 32 or 64 bits.
  5. Overflow of Int, discussion on page 63-64.
  6. Long integers in Python

Using the Math Library, Ch 3.2

Many math functions are provided by pre-defined functions and constants in Python. Access these by importing the math library.
  1. At the top of your program - import math.
  2. Get the functions shown on p470, built-in math functions.
  3. Don't for get to say math.sqrt(2), ie, using library prefix.
  4. Try math.pi, math.log(x), math.log10(x), math.exp(x). Compare with the built-in operator **. Try math.exp(3) vs math.e ** 3
  5. Try math.log(math.exp(3)) and math.exp(math.log(3))

Type Conversions, Ch 3.6

  1. int(7.5)
  2. round(7.5)
  3. float(3)
  4. float(3/4)
  5. float(3)/4
  6. A way to average ... (3+4)/2
  7. float(3+4)/2
  8. round(-3.64)
  9. int(-3.64)
  10. long(3.9)
  11. long(round(3.9))

Accumulator Pattern, Ch 3.3

  1. factorial
      fact=1
      for n in [2,3,4,5,6]
        fact = fact * n
      
  2. factorial again
      fact=1
      for n in range(2,7)   # 2,3,4,5,6
        fact = fact * n
      
  3. More range(start, end, inc) examples

Sample Programs for Chapters 1,2,3

Sample programs

Discussion and Homework Problems for Chapter 3