There has to be an easier way than this!!
Loops are the way! A loop is a definite iteration.
For loops: in each iteration, we perform an action on each element of the collection.
The general structure of a for loop consists of the following:
for <temporary variable> in <collection>:
<action>
Where:
"for" means the start of a for loop
<temporary variable> represents the value of the element in the collection the loop is currently on
"in" separates the temporary variable from the collection used for iteration
<collection> is what the loop applies to
<action> is doing something on each iteration of the loop
An example of a for loop:
colors = ["red", "blue", "orange"]
#skip this line because pretty code
for color in colors:
print(color)
Additional notes about temporary variables: the temporary variable's name is arbitrary and doesn't have to be defined beforehand. You can use anything for temporary variable name.
Best practices say to name the temporary variable something descriptive and representative of what we are working on.
In Python you can conserve lines and put the entire for loop on one line:
for color in colors: print(color)
My first for loop on line 8!
Using a range inside loops:
Then I got the idea to add this and see what happens:
And then this happened. I seems to run through everything in the for loop before starting again:
while <conditional statement>:
<action>
While loop walkthrough with printout, and my own loop!
This is what happens when the loop can't figure out if it is at the end:
And the teeny-tiny thing I did to fix it. Do you see it?
(I removed the = sign from the while loop)
**Note to self: avoid writing infinite loops because the computer will run the program forever and bad things happen! If this happens, hit ctrl + c to end the program.**
Stopping a loop with break: break goes on a line by itself.Continue statement: ignore what you found and move to the next item in the list. Here is an example:
It really does end up more "elegant" when we use fewer lines in the code. Everything stays together closely and it takes less time to read.
In this exercise I had to get the first character of each name using an index number, and I surprised myself by getting it on the first try.
In the above code, I copied the solution from the program itself. Initially I had made a very complicated code that was 5 lines long and tried to figure out how to do a for loop inside another for loop. It was too messy. The solution suggested by the program is very simple and elegant indeed. This line:
product = [e1 * e2 for (e1, e2) in nested_lists]
names the elements inside the brackets e1 (left) and e2 (right) and then multiplies them together. I misunderstood the directions, and I thought it meant multiply it across like a matrix, and then I panicked, because I didn't know how to do it.
I did this one on my own without hints or help, and extended thinking about the example directly above.
Well, I thought I was on a roll here, but this happened when trying to get the first element only to be listed:
(Here is where I finally remembered that I can comment and explain what I'm doing.)
My code was correct and did the correct output, but I kept getting an error saying zip needs to support something something:


























No comments:
Post a Comment
Note: Only a member of this blog may post a comment.