Is it generally frowned upon to alter the range rather than +1 the variable. Eg, we want to print [1,…,10]. We can either change the range to for i in range(1, num +1) or just print(i+1). Does the former have any specific disadvantages other than being less elegant?
Good question. No, it is not frowned upon. Pedagogically, we decided to present a simple version of `range() to begin to help students understand how to use it and to force us to think carefully about how a loop works. We will soon talk about a smarter way to solve the problem we had in class using the method you describe. There is a third feature range has, as well, that can be useful.
I’m still a bit unsure of how to use accumulator patterns.
There were many points similar to this. This is my fault, I ended at an odd location with just the initial ideas of an accumulator pattern. We will see examples of it Friday.
Does Range function have to start from zero if we want to start it from 7-999 is there a function that can do that?
No, it doesn’t. We will see examples of using range(start, end)
which gives a range from start to end-1. For your range of 7-999, we can use range(7,800)
When would be a time you would need to loop a list?
If I have a list of all students in the class and want to print their names and grades. Or, if I’m writing a graphics program with 3 balls bouncing around the screen, I may want to loop over the list of balls to update their location. In my artificial intelligence projects, I often loop or a list of examples to feed to my model to update it’s understanding of a problem. For example, I loop over a list of brain images to have my algorithm detect patterns that diff between Alzheimers and non-Alzheimers brains.
In the example where we asked a user for a number and squared it 3 times, I am a bit confused about how we used the loop. The statement that we printed had nothing to do with the values in our loop. Did the loop for i in range(3): just serve to make the function repeat 3 times, and the actual values of i (0,1,2) aren’t important?
You are exactly on point! I wanted to give an example of the many uses of a loop. Sometimes we need the value, sometimes we don’t. We’ll see other ways we can use the loop variable going forward.
for the third example in loop.py, why do we put range(3), in previous examples range(3) would give us 0,1, and 2
We only cared about how many times the loop ran in this example, so have a sequence of length 3 is all we need. range(3)
accomplishes this.
Can you increment i by something other than 1?
Sort of. i
takes on the next value in the sequence, no matter what. We could define a sequence that skipped by more than 1, though. We’ll see how to do this soon.
Why do for loops need a colon?
It is Python’s signal that you are defining a sub-procedure. Python then knows that the sequence is finished and that the body follows. You’ll noticed that the colon is used for def main():
to signal the definition of a procedure, and we’ll see it next week as well in a different context. Any time there is a set of lines of code that define a distinct procedure, we use a colon.
Does printing a large amount of values (i.e., printing values from 0 to 10^8) affect storage in the computer, or does it just print them and forget?
Interesting question. Print just puts something to the screen - if we run out of screen space we lose older print statements. If we chose to print to file (week 7) then we definitely have to worry about disk space.
Why is it that in a range(), it starts with 0 and not 1?
This is the more typical use in CS. The reason is because we often frame it as “how far from the beginning?” For example, if I’m in the sequence, I want to know what position the item is relative to the beginning. Well, if I’m in the first slot I am exactly 0 spots away from the beginning. When I look at the string “Ameet”, how far away is “A” from the beginning? 0! So I say that the “A” is in position 0.
Why did we not download the math library to python before doing some math calculations? Are there basic functions python can do on its own?
Python worries more about basic types (int, float, str, float) and operations (add, subtract, multiply, division, etc.). Advanced/niche functionality often requires importing. If everything Python could do was included, it’d get really bloated and run much more slowly.
How do you repeat the loops for infinite number of times?
AHHHHHH! Sorry, you just stated one of my recurring nightmares. We’d have to use an indefinite loop (coming soon).
Can the accumulator pattern be a counter as well? So it counts the number of primes, for example?
Yes! That is a great use!
Do you ever change i in the loop?
No. It is technically allowed, but it is a landmine of potential problems. It is safer practice to just create a different variable, or to use a while
loop (coming soon) if you need to update the loop variable in a more complicated manner.