#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Felix Muzny 9/30/2022 DS 2000 Lecture 7 - for loops, lists, constants Logistics: - Homework 3 is due tonight @ 9pm -> this homework is hard -> I am planning on ending lecture 10 minutes early to answer HW 3 questions. Please do your best to focus on today's material until then! -> (same deal if you have questions about your HW 2 grade) - Homework 4 is now available on the course website -> this homework is designed to be shorter than HW 3 -> it does incorporate concepts from this lecture though - Quiz 4 will be available on Monday - remote attendance (https://bit.ly/remote-ds2000-muzny) Three ways to participate (please do one of these!) 1) via the PollEverywhere website: https://pollev.com/muzny 2) via text: text "muzny" to the number 22333 to join the session 3) via Poll Everywhere app (available for iOS or Android) Warm-up 1 - while loops + accumulator --- What does the following code do? """ import random print("warm up 1") count = 0 while count < 10: # we essentially flipping a coin and only # adding one if it is 0 print(count) if random.randint(0, 1) == 0: count += 1 print() """ A. repeats 10 times B. repeats an unknown number of times <-- (at least 10 times) C. repeats infinitely D. Error nesting just talks about having one control structure inside of another the conditional is nested in a loop a nested loop is specifically a loop inside another loop Warm-up 2 - nested while loops --- What is the output of the following code? """ print("warm up 2") outer_count = 0 # repeat 3 times while outer_count < 3: count = 0 # first iteration: 0 + 1 # second iteration: 1 + 1 # third iteration: 2 + 1 inner_repetitions = outer_count + 1 while count < inner_repetitions: print(count) count += 1 print() outer_count += 1 print("all done") """ A. always print 0 B. print 0 1 2, 0 1 2, 0 1 2 C. print 0, 0 1, 0 1 2 <---- inner repetitions based on outer_count D. something else E. infinity/error Summary: in general, # of iterations of the inner loop is while outer < 10: # code while inner < 5: # innermost code innermost code repeats (runs) 50 times (10 * 5) while outer < 10: # code while inner < __expression w/ outer___: # innermost code innermost code repeats (runs): # of repetions for first outer iteration + # of repetions for second outer iteration + ... # of repetions for last (10th) outer iteration """ """ Lists: what we rememember --- - they can hold different values (strings, ints, ...) - convenient to store related values - flexible to hold any # of values """ print("list examples") # empty list ages = [] ages.append(31) ages.append(33) ages.append(66) ages.append(60) ages.append(0) # create a list w/ values fruits = ["banana", "apple", "raspberry"] # accessing specific elements in a list print(ages) print(fruits) # ask for the length of a list print(len(ages)) print(len(fruits)) # list values all have an index # "0-indexed" # 0 1 2 3 4 # [31, 33, 66, 60, 0] print(ages[0]) # first element print(ages[-1]) # last element print(ages[len(ages) - 1]) # last element print() # calculating an average w/ a list print("Average:", sum(ages) / len(ages)) # print(sum(fruits)) # ERROR! print() """ Getting a list from a string: str.split() ----- Say I have a string, for instance, the first words of Middlemarch by George Eliot: """ print("split examples") fancy_text = "Who that cares much to know the history of man" # If I want a list from this string, I use the str.split() function: words = fancy_text.split() print("split()", words) print() # I could also split on the letter "a" to demonstrate what python # is doing—it is using the "splitter" string as the thing to delete # and separate the original string into smaller substrings. by_as = fancy_text.split("a") print(by_as) print() # to do the reverse glue = "glue" print(glue.join(words)) print() """ A new kind of loop: for loops --- a control structure that repeats code Syntax: for variable_name in list_name: # code """ print("for loop 1") print(ages) for age in ages: # secret code # age = next value in the ages list print(age) print() print("for loop 2") for animal in ["cat", "bat", "dog"]: # secret code # animal = next value in the list print(animal) print() """ for loops + files --- This is generally how we read files with a while loop... line = file.readline() while line != "": # code line = file.readline() it's much easier with a for loop though! """ print("for loops and files") file = open("distances.txt", "r") for line in file: # line = file.readline() print(line) file.close() print() """ for loops + files + str.split() --- Often you have "structured data" where we can depend on certain formatting to show where different data values are. 29 3.01 28 4.02 24 5.14 """ print("for loop and structured data") file = open("muzny_distances.txt", "r") for line in file: # line = file.readline() pieces = line.split() print(pieces) file.close() print() """ Example problem -- see analyze_dists.py --- Write a program, analyze_dists.py, that reads the data from the file muzny_distances.txt and graphs it accordingly on a scatter plot. Use a constant to save the file name. """ """ More graph types: bar chart ----- Basically the same mechanically as a scatter plot, but write down: plt.bar(x_value, y_value) """ """ Example problem, updated x 1 --- Update the program to display the information on a bar chart instead. """ """ Plotting multiple points at the same time ----- We can also give the plotting function a list of x values and a list of y values instead of plotting each point individually # for scatter plots plt.plot(x_coords, y_coords, "o", label="name of series") # for bar charts plt.bar(x_coords, y_coords, label = "name of series") """ """ Example problem, updated x 2 --- Update the program to plot all points simultaneously. """ """ Example problem, updated x 3 --- Update the program to plot the strange_distances.txt in a separate series. Add a legend. """ # We'll start here on Tuesday! """ Lists + indexing + loops --- """ """ for i in range(num) --- """ # example problem looping over a list w/ indexes """ strings & indexing --- """ # example problem looping over a string w/ indexes """ Next time: - functions!!! """