CS150 - Fall 2013 - Class 4

  • Quiz: 3.5 from the book

  • admin
       - Lab prep 2 out today and due at the beginning of class on Friday

  • scope
       - The "scope" of a variable is the portion of the code where we can use (reference) that variable and have it be valid
       - When we declare a variable in the interpreter, what is its scope?
          >>> x = 10

          - the scope is any shell statements/expressions typed after it
       - When we declare a variable (outside a function) in a file, what is its scope?
          - anywhere below it in the file
             - remember, running a program is very similar to typing those commands into the shell
       - When we declare a variable inside a function, what is its scope?
          - anywhere below it in the FUNCTION
          - notice, for example, that if we run the bbq-functions.py file and then type todd the variable is undefined
             >>> todd
             Traceback (most recent call last):
              File "<string>", line 1, in <fragment>
             NameError: name 'todd' is not defined

             - this is called a "local" variable, since it's scope is local to the function
          - it doesn't matter if we run the function or not
             >>> hotdogs(1, 4)
             25
             >>> todd
             Traceback (most recent call last):
              File "<string>", line 1, in <fragment>
             NameError: name 'todd' is not defined

             - the variable's scope is only local to the function
       - What is the scope of the parameters?
          - anywhere in the FUNCTION
          - it doesn't really make sense outside of the function since we wouldn't have a value for it

       - Why do programming languages limit a variable's scope?

  • Modules
       - When I say the word module what does that make you think of?
       - A module in python is a collection of functions and variables
       - modules allow us to use code that other people have written
       - For example, there is a module called "math" that has many of the math functions you might want
          - We can look at the documentation for this module online by searching for "math python" or by going to docs.python.org and browsing searching there
          - http://docs.python.org/library/math.html
             - logs
             - sqrt
             - trigonemtric functions
             - constants
       - If we want to use a module, we need to tell the program to include it with our program. To do this, we need to "import" it
          - there are many ways of importing modules (some better than others)
          - for now, we're going to import the functions and variables into our program as if they were local (i.e. just as if we'd written them in our program)
             - this is convenient for now, but in general it is bad practice
             - I'll soon show you a better way to do it
             
             from math import *

          - this statement has multiple components:
             - "from" is a keyword
             - "math" is the name of the module
             - "import" meaning, load them into our program
             - "*" means everything, i.e. everything in the math module

       - once we've imported a module, we can then use the functions in that module
          >>> log(10)
          2.302585092994046
          >>> sqrt(16)
          4.0
          >>> cos(10)
          -0.8390715290764524
       
       - notice that we just use them just like any other functions we've seen before
       - in a program, the imports are generally put at the top of the program
       - Python has LOTS of built-in modules and many more that you can download that other people have written
          - http://docs.python.org/library/   

  • turtle module
       - One of the modules that Python makes available is the "turtle" module
       - The turtle module implements a set of commands similar to the Logo programming language
          - some of you may have used Logo at some point in your schooling
          - http://en.wikipedia.org/wiki/Logo_(programming_language)

       - the basic idea is that you control the movements of a turtle (in our case, it will be an arrow) through basic commands such as
          - forward
          - backward
          - right
          - left
          - and many others

       - As the turtle moves, it draws a line behind it, so by giving it different commands, we can draw things on the screen
       - you can see the documentation for the turtle class online
          - http://docs.python.org/library/turtle.html
          - you'll be getting more comfortable with this documentation as part of your lab prep

       - We can import all of the function, etc. from the turtle module just like we did for the math module

          from turtle import *

       - Once you've done this, then we can start playing with it

          >>> forward(100)
          >>> right(45)
          >>> forward(100)

       - the right and left commands are in degrees (though you can change the mode to radians if you prefer that)
       - how would we draw a square?
          forward(some_length)
          right(90)
          forward(some_length)
          right(90)
          forward(some_length)
          right(90)
          forward(some_length)

       - We can easily turn this into a function that takes the length as a parameter. For example, look at the square function in the turtle-examples.py code

       - This seems like a lot of repetitive typing. Let's say we can tell the turtle to repeat some statements, would there be a better way of creating a square (state it in English)?
          - go forward some length and then turn right, repeat this 4 times
       
  • for loops in Python
       - Python has a number of different "loop" structures that allow us to do repetition (remember we said that computers are good at doing repetitive tasks)
       - The "for" loop is one way of doing this
       - There are a number of ways we can use the for loop, but for now the basic structure we'll use is:

          for some_variable in range(num_iterations):
             statement1
             statement2
             ...

          - "for" is a keyword
          - "in" is a keyword
          - range is a function that we'll use to tell Python how many repetitions we want
          - num_iterations is the number of iterations that we want the loop to do
          - some_variable is a local variable whose scope is the within the for loop
             - some_variable will take on the values from 0 to num_iterations-1 as each iteration of the loop occurs
             - for example, in the first iteration, it will be 0, 1 the second time, etc.
             - we're computer scientists so we start counting at zero :)
          - don't forget the ':' at the end
          - Like with defining functions, Python uses indenting to tell which statements belong in the for loop

          - Here are a few simple examples. What will each of these do?

             # prints out the numbers from 0 to 9
             for i in range(10):
                print i

             # sum the numbers from 0 to n-1
             def sum(n):
                total = 0
                
                for val in range(n):
                   total = total + val

                return total

  • for loops in turtle
       - Back to turtle, how can we use a for loop to draw a square?
          - look at the iterative_square function in turtle-examples.py

       - how could we draw draw a star (i.e. like an asterisk type star?)
          - look at asterisk_star functions
             - figure out how we have to space the spokes
             - do a for loop over the number of spokes
                - each iteration in the loop draw a spoke
                - need to go backwards into the middle for the next spoke
                - rotate right based on the angle we calculated

       - what will simple_spiral, spiral and rotating_circles in turtle-examples.py code do?
          - simple_spiral
             - i
                - i is just the name of a variable
                - for small loops (i.e. just a couple of statements), it's common to just us the variable name i (short for index)
             - each time, the length of the edge drawn will be longer by a factor of 5
                - 0, 5, 10, 15, 20, ...
             - and will be at a 50 degree angle
                - if it is < 90 it will spiral out
                - > 90, spiral in

          - spiral
             - similar to simple_spiral, however now we've parameterized the length of the sides and the angle
       
          - rotating_circles
             - draws num circles
             - each one rotated "angle" degrees from the previous

  • run walk function from turtle-examples.py code
       - how is this being accomplished?
          - turns a random angle between -90 and 90
          - steps forward some step size

  • random module
       - there is another python module called "random"
       - http://docs.python.org/library/random.html
          - it generates pseudo-random numbers
             - what does this mean?
             - the numbers are not technically random, they're generated based on an algorithm
             - for most purposes this is pretty good
             - if you want truly random numbers, check out:
                - http://www.random.org/
                - also there are some programs that try and measure things like time between keystrokes, etc. to get some randomness (but these also aren't quite perfect)
       - many useful methods inside random
          - random -- random number between 0 and 1.0
          - uniform(a, b) -- random floats between a and b
          - randint(a, b) -- random integers between a and b
          - samples from many other distribution
             - beta
             - exponential
             - gamma
             - normal
       
       - for now, we're just going to utilize the randint function
       - if you only want to import a single function, you can do that like:

          from random import randint

          - before the '*' indicated all function and variables. Here, we just said the "randint" function
       
       - You can print out a bunch of random numbers if you're curious:
       
          for i in range(100):
             print randint(0,10)
       
  • look at walk function in turtle-examples.py code
       - went through the loop num_steps times/iterations
       - uses the uniform function to get an angle between -90 and 90

  • what will the pretty_picture function do?
       - draws a line
          - at an angle between -90 and 90
          - of length between 10 and 60
       - then draws a star
          - of length between 10 and 60
          - with int(spokes) spokes
             - remember, if we want an integer, for example to be used in range, you need to turn it into an int