Easier way to produce random integer in Python: random.randint(a,b) returns a random integer between a and b, including a and b. So this code:
for i in range(0,20):
print random.randint(0,10)
produced this output:
10 4 4 10 10 4 9 6 4 0 6 2 4 9 7 0 6 5 8 9
MonoLakeExampleCode PopulationGrowthExampleCode
A very tricky thing about Python Try this:
x = [0,0,0,0,0,0] y = x y[2] = 5 print xWhat happened?? There was never a line in which you wrote x[2]=5, and yet x[2] became 5. It's one of those hard-core-programming things. The technical way to described what happened is that y=x says that y is assigned the address in the computer's memory where x is stored. So when you change y[2], you're making the change to whatever list is referred to by the address in y. That means that you change x. A possibly confusing analogy. Say there's a person, and there's the name Hillary Rodham. There's also the name Hillary Clinton, and it refers to the same person. If you donate money to the person referred to by the name Hillary Clinton, the person referred to by the name Hillary Rodham will still (also?) get the money. (Do you know that line in that song, "So if you've a date in Constantinople / She'll be waiting in Istanbul"?) Take-home message: if you want two lists to be initialized with the same values, but you want to be able to make changes to one without changing the other, you should create them separately. First make one. Then make a second list and one-by-one change each element of the second list to its corresponding element in the first one. You can't just make list x and then make list y and then write y=x. If you do that, every change you make to y will also be made to x.
