CS51A  Spring 2019  Class 4
Example code in this lecture
turtleexamples.py
Lecture notes
admin
 assignment 1: due Thursday at 11:59pm
2+2 example from class last time
 a bug in Wing?
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 many situations there are better ways of doing it (more on this later)
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 builtin 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
 We can import all of the functions, 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
turtleexamples.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 (computers are really 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 (where it can be referred to) is only within the for loop
 some_variable will take on the values from 0 to num_iterations1 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 n1
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 turtleexamples.py
 What does the simple_star function in turtleexamples.py do?
 draws a 36sided star (or asterisk)
 What if we wanted to have a star/asterisk with a different number of spokes?
 look at asterisk_star function
 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
turtleexamples.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 the walk function from
turtleexamples.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 pseudorandom 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 distributions
 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
turtleexamples.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