Exercise 1 -- Getting information:

- Fire up netscape
   - find the python documentation
   - find one or more python tutorials
   - fire up the pydoc server, have a look at the pydoc pages

Exercise 2 -- Basic data structures, expressions and variables

- Fire up the Python interpreter (or IDLE)
- Experiment with exppressions and built-in types
- Try to answer the following:
  1. Which numeric types does python know?
  2. What happens?
     >>2.1
     2.1000000000000001
     >>1/2, 1.0/2
     (0, 0.5)
     >>2**1/2
     1
     >>abs(-1), abs(-(4+3j))
     1, 5.0
  3. Does this make sense?
     >>'abc'[5]
     Traceback (most recent call last):
       File "", line 1, in ?
     IndexError: string index out of range
     >>'abc'[1:5], 'abc'[5:6]
     'bc', ''
  4. why can't I do?
     >>a = (1, 2, 3, 4)
     >>a[2] = 'c'
  5. why does this not work?
     >>for i in [3, 1, 2].sort():
     ...     print i
  6. explain what happens
     >>a = 1
     >>b = a
     >>a += 1
     >>a, b
     (2, 1)    # a and b different
     >>a = [1]
     >>b = a
     >>a += [1]
     >>a, b
     ([1, 1], [1, 1]) # a and b the same
  7. What will happen ? Why?
     >>print [ord(i) for i in "Hello World!"]
     and here?
     >>map(chr, [ord(i) for i in 'Hello World!"]
     or here?
     >>map(ord, list("Hello World"))
  8. What goes on here (note order 'b' and 'c')?
     >>{'a':1, 'b':2, 'c':3}
     {'a': 1, 'c': 3, 'b': 2}
  9. Sort this list by name, by age:
     >>l = [{'name':'Piet', 'age':34},
              {'name':'Jan', 'age':21},
              {'name':'Marietje', 'age':27},
              {'name':'Joke', 'age':34}]
  10.What does the 'is' operator do? What's the difference between
     'is' and '=='? Explain the following (bonus)
     >>1+1 is 2
     1
     >>1000+1000 is 2000
     0

Exercise 3  -- Your first program

- write the "hello world" program and execute it
- Rewrite the previous program so that "hello world" is printed
  by calling a function print_hello()
- import the program in the interpreter. You should be able to do something
  like:
  >>import your_code
  >>your_code.print_hello()
  'Hello World'

  Note 1. Make sure that your_code.py is somewhere on your path
  Note 2. If you have to reload your module use:
  >>reload(your_code)

- rewrite your_code.py is such a way that:
  if you do
  python your_code.py
  hello world is printed, but if you do:
  python
  >>import your_code
  nothing happens.

  I.e: make sure that print_hello() is called if the code is run as a
  program, but that print_hello() is not called when the code is run on
  import (Hint: if __name__ == '__main__':).

Exercise 4 -- Writing functions, using modules:

- The os module contains a function popen(cmd), that allows you to start
  an external program and open one of its streams as a file. Use this to
  write a function that returns the day of the week:

- Write the same function using the module time

- Rewrite the function again using mx.DateTime

Exercise 5 -- Reading files

- Write a python function to read an ascii file that may contain
  1. blank lines
  2. comments, everything after '#' on a line is a comment
  3. comma-separated floating point values
  raise an approriate error if the file cannot be parsed.

- (if that was too easy) Use Python to read and parse the header of a
  fitsfile. For example: write a function that takes a filename and a
  keyname and returns the value of the header keyword (converted to the
  apropriate type, of course)
- (bonus) Use Python to read the data from a fitsfile, taking into account
  bitpix, and endianness

Exercise 6 -- Writing functions -- global and local scopes.

- Predict/explain what happens:
  1.>>x = 5
    >>def f():
    ...     print x
    >>f()
    ???

  2.>>x = 5
    >>def g()
    ...     x = 3
    ...     print x
    >>g()
    ???
    >>print x
    ???

  3.>>x = 5
    >>def g()
    ...     print x
    ...     x = 3
    >>g()
    ???

  4.>>x = 5
    >>def h(x):
    ...     print x
    ...     x = 3
    >>h(x)
    ???
    >>print x
    ???

  5.>>x = 5
    >>def g():
    ...     global x
    ...     x = 3
    >>g()
    >>print x
    ???

- Explain the difference between global and local scope
- Given
cat mm.py
x = 4
def p():
    print x

explain what happens:

>>from mm import p
>>x = 5
>>p()
???