<jupyter_start><jupyter_text>Lab 3 - Iteration with For Loops Data 94, Spring 2021 Iteration is a very important tool in Python. So far we have learned about `while` loops, and now we are going to dive into `for` loops. `For` loops are a form of iteration that loop over a Python data structure, such as a list or a string. Below, you can see that `for` loops can work in all sorts of ways!<jupyter_code># We can iterate over numbers using the range() function!
for i in range(10):
    print("Charging... battery at:", str(i * 10), '%')
print("Battery charged at 100%!")<jupyter_output><empty_output><jupyter_text>Notice how the value of `i` changes on each loop because of the range() function! **Important:** We do not necessarily have to use `i` as the name of our `for` loop variable. We can use any name we want!<jupyter_code># We can iterate over the characters in a string!
string = "Data Science is Cool!"
for letter in string: # 'letter' instead of 'i'
    print(letter)
# We can even iterate over the words in a string!
sentence = string.split(" ") # Split up string, using spaces (" ") to separate words
for word in sentence: # 'word' instead of 'i'
    print(word)<jupyter_output><empty_output><jupyter_text>`While` loops in Python can do these things as well, but they are a bit more complicated:<jupyter_code>word_index = 0 # Start at first item
while word_index < len(sentence): # While we are iterating over the list
    print(sentence[word_index])
    word_index += 1 # Move onto the next item of the list<jupyter_output><empty_output><jupyter_text>As you can see, using the `while` loop is a lot more work and looks a bit more difficult to read. Both work, but often one will be better to use than the other.When choosing between which you should use, generally:- `While` loops work better when you don't necessarily know how many iterations your loop may take    - Think 'Persistency' from Question 5 on the Quiz- `For` loops work better when you have something to **iterate over** like a list or a string.    - See examples above Let's see an example of a `for` loop at work:In this function we will calculate the maximum value in a list of numbers without using the Python max() function. If the list is empty, we return the False boolean.<jupyter_code>def max_value(lst):
    if len(lst) == 0: # If we are given an empty list, there is no maximum value, so return False
        return False
    max_number = lst[0] # The first number is the biggest we've seen so far... because we haven't seen any other numbers
    for number in lst[1:]: # We check the rest of the items after the first; we don't need to check the first again
        if number > max_number: # If the number we are looking at is bigger than our biggest so far...
            max_number = number # ... then it becomes the new biggest!
    return max_number # After we finish the 'for' loop, we will have stored the maximum value in max_number
lst1 = [13, 96, -24, 53, -109]
lst2 = [1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1]
lst3 = ["data", "science", "is", "cool!"] # It even works on strings too because strings can be compared using '<' and '>' in Python!

print("The maximum value of", lst1, "is:", max_value(lst1))
print("The maximum value of", lst2, "is:", max_value(lst2))
print("The maximum value of", lst3, "is:", max_value(lst3))<jupyter_output><empty_output><jupyter_text>Let's go ahead and try writing a function with a `for` loop:We want to be able to pairwise multiply two lists together. This means that we want the result of multiplying the first items of each list, the second items of each list, etc.For example, pairwise multiplying `[2, 3, 4]` and `[10, 20, 30]` is `[20, 60, 120]` because 2 * 10 == `20`, 3 * 20 == `60` and 4 * 30 == `120`.We can use a `for` loop to move through each list, multiplying items as we go!<!--BEGIN QUESTIONname: q1points: 0--><jupyter_code>def pairwise_multiply(lst1, lst2):
    output = ...
    for i in range(len(lst1)):
        multiplication = ...
        ...
    return output
grader.check("q1")<jupyter_output><empty_output><jupyter_text>Now let's work on another example:We will implement a function that calculates the most revenue brought in during one shift at a store. Given the sales that were made, calculate the sales total for the best shift of the day.For example, with `sales = [3, 4, 1.25, 6, "Shift Change", 2, 4, 5.75, "Shift Change", 10, 2, .25, "Shift Change"]`, we will return `14.25` because 3 + 4 + 1.25 + 6 == `14.25`.In other words:`best_shift_sales`(`[3, 4, 1.25, 6, "Shift Change", 2, 4, 5.75, "Shift Change", 10, 2, .25, "Shift Change"]`) == `14.25`<!--BEGIN QUESTIONname: q2points: 0--><jupyter_code>def best_shift_sales(sales):
    current_shift_sales = ...
    best_shift_sales_total = ...
    for sale in sales:
        if sale == "Shift Change":
            if current_shift_sales > best_shift_sales_total:
                best_shift_sales_total = ...
            current_shift_sales = ...
        else:
            ...
    return best_shift_sales_total
grader.check("q2")<jupyter_output><empty_output><jupyter_text>Done! 😇That's it! There's nowhere for you to submit this, as labs are not assignments. However, please ask any questions you have with this notebook in lab or on Ed.If you want some extra practice, you may proceed onto the next section, which contains a practice problem for this week.  Extra Practice ProblemsThese problems are here for extra practice. They are not mandatory, and they will not be turned in for any points, but we highly suggest you do them as practice for both homework questions and quiz questions.  Extra Question 1We want to modify our `best_shift_sales` function from earlier in the lab so that it calculates the **name of the employee** who was working during the best shift for the store. You are given the shift list in order and the sales that were made. There are guaranteed to be an equal number of shifts and employees, so each employee only does one shift.For example, with `employees = ["Alice", "Bob", "Cam"]` and `sales = [3, 4, 1.25, 6, "Shift Change", 2, 4, 5.75, "Shift Change", 10, 2, .25]`, we will return `"Alice"`.In other words:`best_salesperson`(`["Alice", "Bob", "Cam"]`, `[3, 4, 1.25, 6, "Shift Change", 2, 4, 5.75, "Shift Change", 10, 2, .25]`) == `"Alice"`*Hint: You should only need to **modify** your implementation from `best_shift_sales`. We are still finding the best sales, but instead of returning how much money was the most, we want the name of the employee. How can we keep track of not only what the best shift by sales was, but also who was working that shift?*<!--BEGIN QUESTIONname: eq1points: 0--><jupyter_code>def best_salesperson(employees, sales):
    current_employee_number = 0 # Start with the first employee
    current_employee_sales = ...
    best_sales_total = ...
    employee_number_of_best_salesperson_so_far = ...
    ...
    best_salesperson_name = ...
    return best_salesperson_name
grader.check("eq1")<jupyter_output><empty_output><jupyter_text>Solution (for after you have tried yourself)    def best_salesperson(employees, sales):    current_employee_number = 0  Start with the first employee    current_employee_sales = 0  We are not yet checking a shift total    best_sales_total = 0  We have not yet seen a best shift total    employee_number_of_best_salesperson_so_far = 0  There is no best employee sales yet    for sale in sales:        if sale == "Shift Change":            if current_employee_sales > best_sales_total:                best_sales_total = current_employee_sales                employee_number_of_best_salesperson_so_far = current_employee_number            current_employee_number += 1            current_employee_sales = 0        else:            current_employee_sales += sale    best_salesperson_name = employees[employee_number_of_best_salesperson_so_far]    return best_salesperson_name  Extra Question 2aLet's write a boolean function that tells us if there are any duplicate values in a list. If we find a duplicate, we should return `True`, but if we search everywhere and we cannot find a duplicate, we should return `False`.We want to implement this function so that it checks all the items from itself to the end of the list for duplicates. There is no need to check anything behind it because past loops already did those duplicate checks. In the list `[1, 2, 3]`, we check if 1 == 2, then if 1 == 3, then if 2 == 3. There is no need to check if 2 == 1 by the time we get to 2, because we had already done that check when we were on 1.Examples:- The list `[1, 2, 3, 4, 5, 1]` has a duplicate, so we should return `True`.- The list `[1, 2, 3, 4, 5, 6]` has no duplicates, so we should return `False`.<!--BEGIN QUESTIONname: eq2apoints: 0--><jupyter_code>def duplicate_values1(values):
    ...
grader.check("eq2a")<jupyter_output><empty_output><jupyter_text>Solution (for after you have tried yourself)    def duplicate_values1(values):    for i in range(len(values)):        if values[i] in values[i + 1:]:            return True    return False   Extra Question 2bThere is actually another way to implement this function using the `count()` method of lists! We can simply loop through the list once and ask for the count of each item. If at any point we encounter a count greater than 1, we have found a duplicate and we can immediately return` True`! If we never see any counts over 1, we return `False` as there are no duplicates.<!--BEGIN QUESTIONname: eq2bpoints: 0--><jupyter_code>def duplicate_values2(values):
    ...
grader.check("eq2b")<jupyter_output><empty_output><jupyter_text>Solution (for after you have tried yourself)    def duplicate_values2(values):    for value in values:        if values.count(value) > 1:            return True    return False  Meme GalleryHere are some memes about the topics we covered today, feel free to like, comment, and subscribe 😆    ---To double-check your work, the cell below will rerun all of the autograder tests.<jupyter_code>grader.check_all()<jupyter_output><empty_output><jupyter_text>SubmissionMake sure you have run all cells in your notebook in order before running the cell below, so that all images/graphs appear in the output. The cell below will generate a zip file for you to submit. **Please save before exporting!**<jupyter_code># Save your notebook first, then run this cell to export your submission.
grader.export(pdf=False)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup