#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Felix Muzny 10/4/2022 DS 2000 Lecture 8 - for loops (part 2), functions Logistics: - HW 4 & Quiz 4 are available now - Expect HW 3 grades *on Monday* -> we're re-designing the rubrics in Gradescope to make them easier for you all to interpret. We're still grading for the same things, just working to increase readability for you all. Thank you for your patience here! - 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 - lists --- What does the following print? """ """ A. 5 B. 1 C. 2 D. 3 E. Error Warm-up 2 - lists --- What is the output of the following code? """ """ A. 1, ALICE B. 7, Alice C. 1, ALICE D. 0, TIRED E. Error """ """ lists: syntax reminders --- # create a list ls = [3, 2] # append a value to the end of a list ls.append(4) # access a value at a certain index print(ls[1]) # change a value at a certain index ls[1] = 1000 # there are many useful list functions that you are welcome to use # https://docs.python.org/3/tutorial/datastructures.html#more-on-lists """ """ for loops: what we rememember --- """ """ Lists + indexing + loops --- For certain repetitive tasks over lists, we *need* index information. If: - you are editing the contents of a list - you are doing a task based on the position of the item in the list (- you are creating a list of a certain length) In this case, we'll use a for loop that looks different but that is actually the same of the for loops we've seen so far. """ """ for i in range(num) --- """ # example problem looping over a list w/ indexes """ strings & indexing --- """ # example problem looping over a string w/ indexes """ ~*~**~*~**~*~*~*~**~*~*~**~*~**~*~**~*~**~*~*~**~*~*~*~~~ """ """ Functions --- 1) memory 2) re-using code 3) branching 4) repetition """ """ Example function --- Write a function, hello, that takes no parameters and prints an intro to DS 2000 message. """ """ Functions with parameters --- parameters/arguments: """ """ Example function --- Write a function, hello_user, that takes the user's name as a parameter and prints a personalized introduction to DS 2000 to that user. """ """ Always the same task, but flexible about inputs --- A long time ago, we wrote the following code: celcius = input("what temp? (c) ") celcius = float(celcius) fahrenheit = ((9 / 5) * celcius) + 32 print("Fahrenheit", fahrenheit) Now, let's translate this into a function """ """ Functions with return values --- return values: """ """ Updating our celcius function --- """ """ Updating analyze_dists from last time --- We do the same task twice. Let's make a function! """ """ Structure of programs w/ multiple functions --- """ """ Next time: - functions, part 2 - writing a full program with functions - while we also read from file(s) - and deal with data weirdnesses - and where is our data coming from? """