#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Felix Muzny 10/7/2022 DS 2000 Lecture 9 - functions Logistics: - HW 4 is due on *Tuesday* at 9pm -> no late submissions will be accepted -> example from last Friday (https://course.ccs.neu.edu/ds2000/felix_lectures/analyze_dists.py) is particularly relevant -> today's example will be more advanced than you need for HW 4 (you do not need to write functions) - No office hours from Sat - Mon (Monday is a holiday) - 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 - functions 1 --- What is a better name for this function? Warm-up 2 - functions 2 --- What is the *best* option to call this function? """ """ functions: syntax reminders --- (Deitel & Deitel 4.2) def function_name(parameters, separated, by, commas): # code return value # to call the function answer = function_name(values, matching, in, number) """ """ Function comments --- Your programs should have three kinds of comments: - - - """ # def function_name(parameters, separated, by, commas): # # code # return value """ Scope --- (Deitel & Deitel 4.13) scope: (We'll do more examples of this next week) """ # scope examples """ Structuring your programs --- Programs for this class will always be in the order: # header comment # imports # constants # non-main functions # main # call your main """ """ main() --- Your main function should read as the "table of contents" of your program. You don't want any one function to do everything. You always want program flow to return to main(). Remember: each function should do one thing and do it well! -> normally this means that your function is doing one of three things: 1) Reading in data 2) Doing a calculation or data manipulation 3) Displaying results (graphing or printing) """ """ Example program --- Write a program, earnings.py, that reads the data from the given earnings data file and calculates the average salary earned by city employees from 2011 to 2021. Here is the small data file. The large data file follows the same format. Columns are in the order: title,salary,overtime Police Officer,, Police Officer,, Police Offc Comm Serv Offc 3$8,69772.1,82300.87 Police Officer,100963.38,67849.66 Police Offc Comm Serv Offc 3$8,109858.02,75938.65 Police Sergeant (Det),127626.76,66433.83 Police Lieutenant (Det),142466.41,167509.61 Police Lieutenant,143566.78,109101.43 Police Detective,107352.54,76434.46 (The data comes from here, originally: https://data.boston.gov/dataset/employee-earnings-report) """ """ Next time: - data: where does it come from? - more on scope - lists + mutation - two dimensional lists - two dimensional lists + csv files """