Monday, October 18, 2021

Loops (update labels for this post)

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:

print("I will finish the python loops module! This is iteration " + str(prom + 1))

And then this happened. I seems to run through everything in the for loop before starting again:



While loops: an indefinite iteration. It performs iterations as long as the given condition exists as true. Here is the basic structure.

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.


We want the loop to break before we print the statement that we have found our dog breed.

I tried something else here to avoid printing the dog breed every time it checks until it finds the one I want. It can check the list and confirm it found the dog breed:


Continue statement: ignore what you found and move to the next item in the list. Here is an example:



sales_data contains data for 3 locations:



Writing more "elegant" code: (i.e. fewer lines of code)

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.


It's not hard with some more practice. Can you say the statement 3 times fast? 

Well, I guess this is what it's supposed to look like, as I finished this assignment:

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.


Reversing booleans:


This one threw me for a loop but I think I get it now:

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:


Well, this is infuriating: code was correct but the brackets!!! I still don't understand when to use which bracket! 


(Here is where I finally remembered that I can comment and explain what I'm doing.)

Again with the brackets! But I did get it on multiple iterations by myself without help in any form!


My code was correct and did the correct output, but I kept getting an error saying zip needs to support something something:


Again:


And now with accidentally calling a defined variable vs a temporary variable:


Thought I had it completely done right the first time! 


Final project for loops lesson:



No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Strings

A string consists of characters inside 'single quotes' or "double quotes". It can be any length and can contain letters, ...