#!/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 -> make sure your computer wasn't extra smart and renamed your .csv files for HW 4 -> if it did, rename it to end with .csv -> we've seen this happen mostly for mac users with your computer appending ".numbers" to the end of the file name - Expect HW 3 grades *on Tuesday* -> 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? """ print("warm-up 1") # 0 1 2 3 4 5 ls = [4, 5, 3, 2, 1, 5] # what does count do? # asking the list stored at ls how many times 5 occurs mystery = ls.count(5) # 2 # what does index do? # what is the position (index) of this value # gives the first occurrence mystery2 = ls.index(5) # 1 # add to the end of the list ls.append(mystery + mystery2) # 3 # get the last thing in the list print(ls[len(ls) - 1]) print() """ A. 5 B. 1 C. 2 D. 3 <----- E. Error Warm-up 2 - lists --- What is the output of the following code? """ print("warm up 2") words = "Alice was beginning to get very tired" # create a list of strings words = words.split() # this is unfamiliar! # resets the value at this index words[0] = words[0].upper() print(len(words)) # 7 print(words[0]) # ALICE print() """ A. 1, ALICE B. 7, Alice C. 7, ALICE <---- D. 0, TIRED E. Error """ """ lists: syntax reminders --- (Deitel & Deitel 5.2) # 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 ls[1] = ls[1] + -999 # these two are equivalent ls[1] = ls[1] + 1 ls[1] += 1 # 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 --- (Deitel & Deitel 3.8) for repeating code (just like while loops) for variable_name in list: # secret code advances the variable # code """ for num in [3, 6, 7]: # num = next value in the list print(num) animals = ["cat", "dog", "bat", "alligator"] for animal in animals: print(animal) print() # with while loops we started with counting count = 0 while count < 4: print(count) count += 1 for num in [0, 1, 2, 3]: # num = next value in the list print(num) """ Lists + indexing + loops --- For certain repetitive tasks over lists, we *need* index information. If: - you are editing the contents of a list ls[index] = ..... - 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. """ print() print("for i in range") # i stands for index # range is going to generate a list from 0 -> num # given - 1 (exclusive) for i in range(10): print(i) print() print(animals) for i in range(len(animals)): print(animals[i]) animals[i] = animals[i].upper() print(animals) # for vs. while loops # ---- # while loops can do anything (repeating code) # you don't need to know how many times the code will # repeat before it runs # for loops you must know how many times it # will repeat in advance import random for i in range(random.randint(1, 10)): print("yay!") """ for i in range(num) --- range(end) (MOST COMMON) -> 0 to end - 1 range(start, end) -> start to end - 1 range(start, end, step) -> start to end - 1 w/ increment of size step """ # example problem looping over a list w/ indexes """ strings & indexing --- string indexing works the same as list indexing name = "Felix" print(name[0]) # F print(len(name)) # 5 except: name[0] = "B" # ERROR """ # example problem looping over a string w/ indexes """ ~*~**~*~**~*~*~*~**~*~*~**~*~**~*~**~*~**~*~*~**~*~*~*~~~ """ """ Functions --- (Deitel & Deitel 4.1 - 4.3) 1) memory (variables) 2) re-using code (functions, built-in and custom) 3) branching (conditionals) 4) repetition (loops) Functions are going to: - make our programs "modular" - do one job and do that job well - typically, functions will either calculate OR display information """ """ Example function --- Write a function, hello, that takes no parameters and prints an intro to DS 2000 message. def function_name(): # code # to call the function function_name() """ def hello(): print("Hello!") print("We love the afternoon") print() hello() hello() hello() """ Functions with parameters --- parameters/arguments: the information being passed into the function (the inputs) def function_name(parameters, separated, by, commas): # code # to call the function function_name(values, matching, in, number) """ """ 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. """ def hello_user(name): # name = first value passed to the function print("Hello!", name) print("We love the afternoon") print() hello_user("Felix") hello_user("Kim") """ 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 """ def convert_c(celcius): fahrenheit = ((9 / 5) * celcius) + 32 print("Fahrenheit", fahrenheit) # the value 20 gets passed in as the parameter "celcius" convert_c(20) convert_c(60) """ Functions with return values --- return values: output, what the function calculates def function_name(parameters, separated, by, commas): # code return value # to call the function answer = function_name(values, matching, in, number) """ """ Updating our celcius function --- """ def convert_c_return(celcius): fahrenheit = ((9 / 5) * celcius) + 32 # returns the VALUE of the variable fahrenheit return fahrenheit # now this works really similarly to the built-in # functions like round() f_answer = convert_c_return(-46) print("Fancy formatting:", f_answer) """ 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? """