Sep 10
chap 2 & 3 ... continuing from Tues
first_name # underbar convention
FirstName # camelcase convention
3people # WRONG - can't start with number
site43_bldg2 # OK - can have embedded numbers
n="Jim Mahoney" # BAD - later will be hard to tell 'n' means.
- variables (those names) have values (numbers, strings, lists, ...).
- expressions let you manipulate the values
- example1: (2.0 + 0.1/20)**3
- example2: "Mr " + first_name + " " + last_name
- operators: + - / * ** ... and others
- types: int float (i.e. decimal) strings ... and others
$ python
>>> type(1)
<type 'int'>
>>> type(1.0)
<type 'float'>
- tuples
- fairly specific to the python language
$ python
>>> a = 1,2,3
(1, 2, 3)
- Try these to see what they do :
>>> a * 2
>>> 2 * a
>>> a + 2
>>> 2 + a
>>> type(a)
- output and output - printing to the terminal
a = input("What is a? ")
print "OK, a is ", a
(We'll look at various sorts of inputs and outputs in more detail later.)
Aside: the "print" statement in python, parens, and what it can be confusing.
interest_rate = 3.0 # percent
start_amount = amount = 100.00
periods = 10
for i in range(periods):
amount = amount * (1 + interest_rate/100.0)
print amount, " at ", \
interest_rate, "% ", \
periods, " times is ", amount
# (The "\" character at the end of a line
# continues it on the next line.)
We will look at the pieces of this more
carefully soon; for now, the point is to
have a first exposure and to get the general
idea. We'll continue to fill in the details
and go over specific pieces, like range().
chapter 3
Warning: this chapter will dump some math on you.
The good news is that we're not going to do much with that
math, and past this we'll mostly be working with other concepts.
So if things like sin() and exp() make no sense, just ignore them.
data types
- internal representations
- type() function in python
float numbers are approximate - finite number of bits for storage
>>> 0.3
0.29999999999999999
(That last result depends on what kind of computer you're using...)
but in python, integers grow as needed ... and become another type
>>> 2**10, 2**20, 2**30, 2**40
(1024, 1048576, 1073741824, 1099511627776L)
(Notice the "L" at the end of the last number: that's a new "long" type.)
Discussion: why is the switch near 2**30 ? why not always use "long" types for integers? why have different storage for floats and ints? Hmmm.
(Hint: it isn't 30 on all computers. Sometimes it's a bigger number...)
math library
First a bit about names, namespaces, and dir()
$ python
>>> dir() # what
['__builtins__', '__doc__', '__name__']
>>> dir(__builtins__)
... long list of built-in things ...
>>> from math import *
>>> dir()
['__builtins__', '__doc__', '__name__', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh']
The math functions like sin(), cos(), sqrt(), aren't available in python by default. Instead, you must "import" them from a "module" called "math".
The book does this instead :
>>> import math
>>> math.sqrt(3.0)
which leaves things from the math module with "math." before their names. If you do things that way, sin(pi/2) is math.sin(math.pi/2).
accumulating results in a loop
sum = 0
numbers = [1, 10, 20, 18, 17, 34, 22]
for number in numbers:
sum = sum + number
print "The sum is ", sum
Look at this carefully to understand what's going on.
In class: run this. Then put in more print statements
to see exactly what is going on during the loop.
(The loop is the "for" statement and the indented part
after it.)
The text doesn't go into the details of lists
until much later - mainly because they have
a lot of bells and whistles - but for now
let me just say that python lets you collect
up things into a sequence, and that these loops
happen over such a sequence.
>>> range(3)
[0, 1, 2]
>>> type([0, 1, 2])
<type 'list'>
>>> for i in [5, 17, 4]:
... print i
5
17
4
This "accumulator pattern" shows up a lot:
we start with a variable which is "empty",
and then in a loop "fill it up" with something,
accumulating the result.
Here are the types of python values we've seen :
- integer : 2, 5, -3
- float : 3.23
- long : 1180591620717411303424L
- string : "This is a string"
- list : [3, 5, 17]
- built-in function : sin, exp
- module : math
We'll learn more about these and see a few more as we go along.
practice
Write a program to calculate the mean of N numbers input by the user.
- (First ask for N, the ask for the numbers one at a time.)
- Also find the standard deviation.
- Now input the numbers as a python list (i.e. [2, 4, 6]).
tricks in the interactive python environment
$ python
>>> dir() # what names are defined?
>>> import math # math.sin, math.pi, ...
>>> dir(math) # here's what is "inside" that
or
$ python
>>> from math import *
>>> dir()
See the difference?
other random notes
Python "gotchas" (all languages have them)
answer = input("What is the number? ") # parens
print "You said ", answer # parens
Comments within the code: #
Assignment statements: What is happening with "x = 5 ; x = x + 1" ?
The range() function.
If there's time left, we'll look some other exercises from the text.
(Attached is a math-ish one, 3.17, calculating a square root. Gage: for a good time, derive the formula given in the text.)