├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md └── workflows │ └── main.yml ├── .gitignore ├── .vscode └── settings.json ├── CONTRIBUTING.md ├── Finished ├── Ch1 │ ├── basicdata.py │ ├── filter_challenge_soln.py │ ├── filteringdata.py │ ├── parsedata.py │ ├── sortingdata.py │ ├── transform_challenge_soln.py │ └── transformingdata.py ├── Ch2 │ ├── dateops.py │ ├── defaultdict.py │ ├── grouping.py │ ├── reduce-challenge-soln.py │ └── reducefunc.py └── Ch3 │ ├── random-challenge-soln.py │ ├── randomnumbers.py │ ├── randomoperations.py │ ├── statisticsfuncs.py │ └── stats_challenge_soln.py ├── LICENSE ├── NOTICE ├── README.md ├── Start ├── Ch1 │ ├── basicdata.py │ ├── filteringdata.py │ ├── parsedata.py │ ├── sortingdata.py │ └── transformingdata.py ├── Ch2 │ ├── dateops.py │ ├── defaultdict.py │ ├── grouping.py │ └── reducefunc.py └── Ch3 │ ├── randomnumbers.py │ ├── randomoperations.py │ └── statisticsfuncs.py └── sample-weather-history.json /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # Codeowners for these exercise files: 2 | # * (asterisk) denotes "all files and folders" 3 | # Example: * @producer @instructor 4 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 7 | 8 | ## Issue Overview 9 | 10 | 11 | ## Describe your environment 12 | 13 | 14 | ## Steps to Reproduce 15 | 16 | 1. 17 | 2. 18 | 3. 19 | 4. 20 | 21 | ## Expected Behavior 22 | 23 | 24 | ## Current Behavior 25 | 26 | 27 | ## Possible Solution 28 | 29 | 30 | ## Screenshots / Video 31 | 32 | 33 | ## Related Issues 34 | 35 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: Copy To Branches 2 | on: 3 | workflow_dispatch: 4 | jobs: 5 | copy-to-branches: 6 | runs-on: ubuntu-latest 7 | steps: 8 | - uses: actions/checkout@v2 9 | with: 10 | fetch-depth: 0 11 | - name: Copy To Branches Action 12 | uses: planetoftheweb/copy-to-branches@v1.2 13 | env: 14 | key: main 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | .tmp 4 | npm-debug.log 5 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "python.linting.enabled": false, 3 | "python.terminal.executeInFileDir": true 4 | } -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | 2 | Contribution Agreement 3 | ====================== 4 | 5 | This repository does not accept pull requests (PRs). All pull requests will be closed. 6 | 7 | However, if any contributions (through pull requests, issues, feedback or otherwise) are provided, as a contributor, you represent that the code you submit is your original work or that of your employer (in which case you represent you have the right to bind your employer). By submitting code (or otherwise providing feedback), you (and, if applicable, your employer) are licensing the submitted code (and/or feedback) to LinkedIn and the open source community subject to the BSD 2-Clause license. 8 | -------------------------------------------------------------------------------- /Finished/Ch1/basicdata.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Introspect the data to make some determinations 3 | 4 | import json 5 | import pprint 6 | 7 | # open the sample weather data file and use the json module to load and parse it 8 | with open("../../sample-weather-history.json", "r") as weatherfile: 9 | weatherdata = json.load(weatherfile) 10 | 11 | # What was the warmest day in the data set? 12 | warmday = max(weatherdata, key=lambda x: x['tmax']) 13 | print(f"The warmest day was {warmday['date']} at {warmday['tmax']} degrees") 14 | 15 | # What was the coldest day in the data set? 16 | coldday = min(weatherdata, key=lambda x: x['tmin']) 17 | print(f"The coldest day was {coldday['date']} at {coldday['tmin']} degrees") 18 | 19 | # How many days had snowfall? 20 | snowdays = [day for day in weatherdata if day['snow'] > 0] 21 | print(f"Snow fell on {len(snowdays)} days") 22 | pprint.pp(snowdays) 23 | -------------------------------------------------------------------------------- /Finished/Ch1/filter_challenge_soln.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Solution file for programming challenge 3 | 4 | # Challenge: using the weather data set, write code to use the filter() 5 | # function to create a list of all the "cold, rainy, windy" days as 6 | # define by precipitation (either rain or snow) > 0.7in, the average temp 7 | # for the day being below 45deg F, and average wind > 10.0 mph 8 | 9 | import json 10 | 11 | # open the sample weather data file and use the json module to load and parse it 12 | with open("../../sample-weather-history.json", "r") as weatherfile: 13 | weatherdata = json.load(weatherfile) 14 | 15 | def is_cold_windy_rainy_day(d): 16 | avg_temp = (d['tmax'] + d['tmin']) / 2 17 | total_prcp = d['snow'] + d['prcp'] 18 | if avg_temp < 45 and total_prcp > 0.7 and d['awnd'] >= 10.0: 19 | return True 20 | return False 21 | 22 | blustery_days = list(filter(is_cold_windy_rainy_day, weatherdata)) 23 | print(f"{len(blustery_days)} cold, windy, rainy days") 24 | print(blustery_days) 25 | -------------------------------------------------------------------------------- /Finished/Ch1/filteringdata.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Filter values out of a data set based on some criteria 3 | 4 | import json 5 | import pprint 6 | 7 | # open the sample weather data file and use the json module to load and parse it 8 | with open("../../sample-weather-history.json", "r") as weatherfile: 9 | weatherdata = json.load(weatherfile) 10 | 11 | # the filter() function gives us a way to remove unwanted data points 12 | # create a subset of the data for days that had snowfall 13 | snowdays = list(filter(lambda d: d['snow'] > 0.0, weatherdata)) 14 | print(len(weatherdata)) 15 | print(len(snowdays)) 16 | 17 | # pretty-print the resulting data set 18 | pprint.pp(snowdays) 19 | 20 | # filter can also be used on non-numerical data, like strings 21 | # create a subset that contains summer days with heavy rain (more than 1 in, about 2.5cm) 22 | def is_summer_rain_day(d): 23 | summer_months = ["-07-", "-08-"] 24 | if any([m in d['date'] for m in summer_months]) and (d['prcp'] >= 1.0): 25 | return True 26 | return False 27 | 28 | 29 | summerraindays = list(filter(is_summer_rain_day, weatherdata)) 30 | print(len(summerraindays)) 31 | pprint.pp(summerraindays) 32 | -------------------------------------------------------------------------------- /Finished/Ch1/parsedata.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Load and parse a JSON data file and determine some information about it 3 | 4 | import json 5 | import pprint 6 | 7 | # open the sample weather data file and use the json module to load and parse it 8 | with open("../../sample-weather-history.json", "r") as weatherfile: 9 | weatherdata = json.load(weatherfile) 10 | 11 | # make sure the data loaded correctly by printing the length of the dataset 12 | print(len(weatherdata)) 13 | # let's also take a look at the first item in the data 14 | pprint.pp(weatherdata[0]) 15 | 16 | # How many days of data do we have for each year? 17 | years = {} 18 | for d in weatherdata: 19 | key = d['date'][0:4] 20 | if key in years: 21 | years[key] += 1 22 | else: 23 | years[key] = 1 24 | 25 | pprint.pp(years, width=5) 26 | -------------------------------------------------------------------------------- /Finished/Ch1/sortingdata.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Sort the data in a sequence 3 | 4 | import json 5 | import pprint 6 | 7 | # open the sample weather data file and use the json module to load and parse it 8 | with open("../../sample-weather-history.json", "r") as weatherfile: 9 | weatherdata = json.load(weatherfile) 10 | 11 | # create a subset of the data for days that had snowfall 12 | dataset = list(filter(lambda d: d['snow'] > 0.0, weatherdata)) 13 | 14 | # sort the entire data set by how much snowfall there was 15 | # method 1: use the sorted() function to create a new list 16 | sorted_dataset = sorted(dataset, key=lambda d: d['snow'], reverse=True) 17 | pprint.pp(sorted_dataset) 18 | 19 | # # method 2: use the sort() function that every list has to sort in-place 20 | dataset.sort(key=lambda d: d['snow'], reverse=False) 21 | pprint.pp(dataset) 22 | 23 | # Sort on multiple fields: first by snowfall, then by average wind speed 24 | sorted_dataset = sorted(dataset, key=lambda d: (d['snow'], d['awnd'])) 25 | pprint.pp(sorted_dataset) 26 | -------------------------------------------------------------------------------- /Finished/Ch1/transform_challenge_soln.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Solution file for programming challenge 3 | 4 | # Challenge: using the weather data set, write code to transform each 5 | # weather data point from a dictionary into a tuple value that contains 6 | # the date string and a string that is one of "cold", "warm", "hot" based 7 | # upon the average temperature for that day. Return the newly created 8 | # list of tuples as the result. 9 | # "cold" day: average temp <= 60 10 | # "warm" day: average temp between 60 and 80 11 | # "hot" day: average temp >= 80 12 | # 13 | # Example: 14 | # This weather record: Becomes this tuple: 15 | # { 16 | # "date": "2017-01-01", ("2017-01-01", "cold") 17 | # "tmin": 41, 18 | # "tmax": 50, 19 | # "prcp": 0.54, 20 | # "snow": 0.0, 21 | # "snwd": 0.0, 22 | # "awnd": 6.49 23 | # } 24 | 25 | import json 26 | 27 | # open the sample weather data file and use the json module to load and parse it 28 | with open("../../sample-weather-history.json", "r") as weatherfile: 29 | weatherdata = json.load(weatherfile) 30 | 31 | # create a new list that transforms each element in the weather data 32 | def average_temp_to_desc(day_data): 33 | avg_temp = (day_data['tmin'] + day_data['tmax']) /2 34 | desc = "" 35 | if avg_temp <= 60: 36 | desc = "cold" 37 | elif avg_temp > 60 and avg_temp < 80: 38 | desc = "warm" 39 | else: 40 | desc = "hot" 41 | return (day_data['date'], desc) 42 | 43 | newdata = list(map(average_temp_to_desc, weatherdata)) 44 | print(len(weatherdata), len(newdata)) 45 | print(newdata) 46 | -------------------------------------------------------------------------------- /Finished/Ch1/transformingdata.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Transform data from one format to another 3 | 4 | import json 5 | import copy 6 | import pprint 7 | 8 | # open the sample weather data file and use the json module to load and parse it 9 | with open("../../sample-weather-history.json", "r") as weatherfile: 10 | weatherdata = json.load(weatherfile) 11 | 12 | # the map() function is used to transform data from one form to another 13 | # Let's convert the weather data from imperial to metric units 14 | def ToC(f): 15 | f = 0 if f is None else f 16 | return (f - 32) * 5/9 17 | 18 | def ToMM(i): 19 | i = 0 if i is None else i 20 | return i * 25.4 21 | 22 | def ToKPH(s): 23 | s = 0 if s is None else s 24 | return s * 1.60934 25 | 26 | def ToMetric(wd): 27 | new_wd = copy.copy(wd) 28 | new_wd['tmin'] = ToC(wd['tmin']) 29 | new_wd['tmax'] = ToC(wd['tmax']) 30 | new_wd['prcp'] = ToMM(wd['prcp']) 31 | new_wd['snow'] = ToMM(wd['snow']) 32 | new_wd['snwd'] = ToMM(wd['snwd']) 33 | new_wd['awnd'] = ToKPH(wd['awnd']) 34 | return new_wd 35 | 36 | metric_weather = list(map(ToMetric, weatherdata)) 37 | pprint.pp(weatherdata[0]) 38 | pprint.pp(metric_weather[0]) 39 | 40 | # use the map() function to convert objects to tuples 41 | # in this case, create tuples with a date and the average of tmin and tmax 42 | Avg_Temp = lambda t1, t2: (t1 + t2) / 2.0 43 | tuple_data = list(map(lambda d: (d['date'], Avg_Temp(d['tmax'],d['tmin'])),weatherdata)) 44 | pprint.pp(tuple_data[0:5]) 45 | -------------------------------------------------------------------------------- /Finished/Ch2/dateops.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Working with date values 3 | 4 | import json 5 | from datetime import date, timedelta 6 | 7 | # open the sample weather data file and use the json module to load and parse it 8 | with open("../../sample-weather-history.json", "r") as weatherfile: 9 | weatherdata = json.load(weatherfile) 10 | 11 | 12 | # The datetime module converts strings into dates fairly easily 13 | dateobj = date.fromisoformat(weatherdata[0]['date']) 14 | print(dateobj) 15 | # Date objects give us information such as day of week (0=Monday, 6=Sunday) 16 | print(dateobj.weekday()) 17 | 18 | 19 | # what was the warmest weekend day in the dataset? 20 | def is_weekend_day(d): 21 | day = date.fromisoformat(d['date']) 22 | return (day.weekday() == 5 or day.weekday() == 6) 23 | 24 | weekend_days = list(filter(is_weekend_day, weatherdata)) 25 | warmest_day = max(weekend_days, key=lambda d: d['tmax']) 26 | print(date.fromisoformat(warmest_day['date']).strftime('%a, %d %b %Y')) 27 | 28 | 29 | # The timedelta object provides an easy way of performing date math 30 | # find the coldest day of the year and get the average temp for the following week 31 | coldest_day = min(weatherdata, key=lambda d: d['tmin']) 32 | # convert the date to a Python date 33 | coldest_date = date.fromisoformat(coldest_day['date']) 34 | print(f"The coldest day of the year was {str(coldest_date)} ({coldest_day['tmin']})") 35 | 36 | # Look up the next 7 days 37 | avg_temp = 0.0 38 | next_date = coldest_date 39 | for _ in range(7): 40 | next_date += timedelta(days=1) 41 | # find the weather object for that date in the list 42 | wd = next((day for day in weatherdata if day['date'] == str(next_date)), None) 43 | avg_temp += (wd['tmin'] + wd['tmax']) / 2 44 | 45 | avg_temp = avg_temp / 7 46 | print(f"The average temp for the next 7 days was {avg_temp}") 47 | -------------------------------------------------------------------------------- /Finished/Ch2/defaultdict.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Count items using a default dictionary 3 | 4 | import json 5 | import pprint 6 | from collections import defaultdict 7 | 8 | # open the sample weather data file and use the json module to load and parse it 9 | with open("../../sample-weather-history.json", "r") as weatherfile: 10 | weatherdata = json.load(weatherfile) 11 | 12 | # The defaultdict collection provides a cleaner way of initializing key values 13 | # Count the number of data points for each year we have data 14 | years = defaultdict(int) 15 | for d in weatherdata: 16 | key = d['date'][0:4] 17 | years[key] += 1 18 | pprint.pp(years) 19 | 20 | # defaultdict can use more complex objects, like lists 21 | years_months = defaultdict(list) 22 | # create a dictionary with year-month keys and lists for each day in the month 23 | for d in weatherdata: 24 | key = d['date'][0:7] 25 | years_months[key].append(d) 26 | print(len(years_months)) 27 | 28 | # What were the coldest and warmest days of each month? 29 | def warmest_day(month): 30 | wd = max(month, key=lambda d: d['tmax']) 31 | return (wd['date'], wd['tmax']) 32 | 33 | def coldest_day(month): 34 | cd = min(month, key=lambda d: d['tmin']) 35 | return (cd['date'], cd['tmin']) 36 | 37 | # loop over the keys of the dictionary and find each warmest and coldest day 38 | for year_month, daylist in years_months.items(): 39 | print(f"Warmest day in {year_month}: {warmest_day(daylist)}") 40 | print(f"Coldest day in {year_month}: {coldest_day(daylist)}") 41 | -------------------------------------------------------------------------------- /Finished/Ch2/grouping.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Arrange data into groups 3 | 4 | import json 5 | from collections import defaultdict 6 | import pprint 7 | from itertools import groupby 8 | 9 | # open the sample weather data file and use the json module to load and parse it 10 | with open("../../sample-weather-history.json", "r") as weatherfile: 11 | weatherdata = json.load(weatherfile) 12 | 13 | # get all the measurements for a particular year 14 | year = [day for day in weatherdata if "2022" in day['date']] 15 | 16 | # create manual grouping of days that had a certain level of precipitation 17 | year.sort(key=lambda d:d['prcp']) 18 | datagroup = defaultdict(list) 19 | for d in year: 20 | datagroup[d['prcp']].append(d['date']) 21 | print(f"{len(datagroup)} total precipitation groups") 22 | pprint.pp(datagroup) 23 | 24 | # Use groupby to get the days of a given year by how much precipitation happened 25 | grouped = {k: list(v) for k, v in groupby(year, key=lambda d: d['prcp'])} 26 | 27 | # How many groups do we have? Now we can use len() on the dictionary 28 | print(f"{len(grouped)} total precipitation groups") 29 | 30 | # we can iterate over the dictionary to list each group 31 | for key, data in grouped.items(): 32 | print(f"Precip: {key}, # days: {len(data)}, Days: {list(map(lambda d: d['date'], data))}") 33 | -------------------------------------------------------------------------------- /Finished/Ch2/reduce-challenge-soln.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Solution file for programming challenge 3 | 4 | import json 5 | from functools import reduce 6 | 7 | # Challenge: using the reduce() function, return the most "miserable" day 8 | # in the data set as calculated by a combination of heat, rain, and wind. 9 | # 10 | # To calculate a "misery score", use the following formula: 11 | # score = (average wind speed + (precipitation * 10) + (max temp * 0.8)) / 3 12 | 13 | 14 | # open the sample weather data file and use the json module to load and parse it 15 | with open("../../sample-weather-history.json", "r") as weatherfile: 16 | weatherdata = json.load(weatherfile) 17 | 18 | def misery_score(day): 19 | wind = 0 if day['awnd'] is None else day['awnd'] 20 | temp = day['tmax'] * 0.8 21 | rain = day['prcp'] * 10 22 | 23 | score = (temp + rain + wind) / 3 24 | return score 25 | 26 | def day_rank(acc, elem): 27 | return acc if misery_score(acc) >= misery_score(elem) else elem 28 | 29 | result = reduce(day_rank, weatherdata) 30 | print(f"{result['date']} with data: {result['tmax']}, {result['prcp']}, {result['awnd']}") 31 | -------------------------------------------------------------------------------- /Finished/Ch2/reducefunc.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Using the reduce function 3 | 4 | import json 5 | from functools import reduce 6 | 7 | # open the sample weather data file and use the json module to load and parse it 8 | with open("../../sample-weather-history.json", "r") as weatherfile: 9 | weatherdata = json.load(weatherfile) 10 | 11 | # how much snowfall is in the entire dataset? 12 | total_snowfall = reduce(lambda acc, elem: acc + elem['snow'], weatherdata, 0) 13 | print(total_snowfall) 14 | 15 | # how much total precipitation is in the entire dataset? 16 | total_precip = reduce(lambda acc, elem: acc + (elem['snow'] + elem['prcp']), weatherdata, 0) 17 | print(total_precip) 18 | 19 | # What was the warmest day in which it snowed? Need to find highest 'tmax' for all 20 | # days where 'snow' > 0 21 | def warm_snow_day(acc, elem): 22 | # return the current element if the snow amount > 0 and its tmax value is 23 | # larger than the tmax value that is in the acc argument 24 | return elem if elem['snow'] > 0 and elem['tmax'] > acc['tmax'] else acc 25 | 26 | # define a "zero" value start date for the reduce function to start with 27 | start_val = { 28 | "date": "1900-01-01", 29 | "tmin": 0, 30 | "tmax": 0, 31 | "prcp": 0.0, 32 | "snow": 0.0, 33 | "snwd": 0.0, 34 | "awnd": 0.0 35 | } 36 | 37 | # reduce the data set to the warmest snow day 38 | result = reduce(warm_snow_day, weatherdata, start_val) 39 | print(f"{result['date']} with temp: {result['tmax']} and snowfall: {result['snow']}") 40 | -------------------------------------------------------------------------------- /Finished/Ch3/random-challenge-soln.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Using the random package 3 | 4 | import json 5 | import random 6 | 7 | # open the sample weather data file and use the json module to load and parse it 8 | with open("../../sample-weather-history.json", "r") as weatherfile: 9 | weatherdata = json.load(weatherfile) 10 | 11 | def select_days(year, month): 12 | # concatenate the year and month to one string 13 | yearmonth = year + "-" + month 14 | 15 | def yearmonthfilter(day): 16 | if yearmonth in day['date']: 17 | return True 18 | return False 19 | 20 | # use that string to filter the weather data 21 | yearmonthdata = list(filter(yearmonthfilter, weatherdata)) 22 | # use the random.sample method to select 5 days 23 | datalist = random.sample(yearmonthdata, k=5) 24 | 25 | # return the list 26 | return datalist 27 | 28 | result = select_days("2020", "12") 29 | print(result) 30 | -------------------------------------------------------------------------------- /Finished/Ch3/randomnumbers.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | 3 | import json 4 | import pprint 5 | import random 6 | 7 | # open the sample weather data file and use the json module to load and parse it 8 | with open("../../sample-weather-history.json", "r") as weatherfile: 9 | weatherdata = json.load(weatherfile) 10 | 11 | # the random module can be used to generate random values 12 | print(random.random()) 13 | 14 | # choose a random number in a range including both points 15 | print(random.randint(10,20)) 16 | # choose a random number in a range excluding end point 17 | print(random.randrange(10,20)) 18 | 19 | # build a list of the summer days in 2019 20 | def is_summer_day(d): 21 | summer_months = ["2019-07-", "2019-08-"] 22 | if any([m in d['date'] for m in summer_months]): 23 | return True 24 | return False 25 | summer_2019 = list(filter(is_summer_day, weatherdata)) 26 | 27 | # choose 5 random days from that summer 28 | random_days = [] 29 | for _ in range(5): 30 | random_days.append(summer_2019[random.randrange(len(summer_2019))]) 31 | pprint.pp(random_days) 32 | 33 | # what was the windiest of those 5 days? 34 | print(max(random_days, key=lambda d:d['awnd'])) 35 | -------------------------------------------------------------------------------- /Finished/Ch3/randomoperations.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | 3 | import json 4 | import random 5 | import pprint 6 | 7 | # open the sample weather data file and use the json module to load and parse it 8 | with open("../../sample-weather-history.json", "r") as weatherfile: 9 | weatherdata = json.load(weatherfile) 10 | 11 | # get the first 30 days in the dataset 12 | month_data = weatherdata[0:30] 13 | 14 | # the shuffle() function will randomly shuffle a list in-place 15 | pprint.pp(month_data[0:3]) 16 | print("---------------------------") 17 | random.shuffle(month_data) 18 | pprint.pp(month_data[0:3]) 19 | 20 | # use choice() and choices() to get random items, but beware that 21 | # these functions can produce duplicate results 22 | rnd_day = random.choice(month_data) 23 | pprint.pp(rnd_day) 24 | print("---------------------------") 25 | 26 | rnd_days = random.choices(month_data, k=3) 27 | pprint.pp(rnd_days) 28 | 29 | # the sample() function will choose random items with no duplicates 30 | rnd_days = random.sample(month_data, k=3) 31 | pprint.pp(rnd_days) 32 | -------------------------------------------------------------------------------- /Finished/Ch3/statisticsfuncs.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Using the statistics package 3 | 4 | import json 5 | import statistics 6 | 7 | # open the sample weather data file and use the json module to load and parse it 8 | with open("../../sample-weather-history.json", "r") as weatherfile: 9 | weatherdata = json.load(weatherfile) 10 | 11 | # select the days from the summer months from all the years 12 | summers = ["-06-","-07-","-08-"] 13 | summer_months = [d for d in weatherdata if any([month in d['date'] for month in summers])] 14 | print(f"Data for {len(summer_months)} summer days") 15 | 16 | # calculate the mean for both min and max temperatures 17 | max_temps = [d['tmax'] for d in summer_months] 18 | min_temps = [d['tmin'] for d in summer_months] 19 | print(max_mean := statistics.mean(max_temps)) 20 | print(min_mean := statistics.mean(min_temps)) 21 | 22 | # calculate the median values for min and max temperatures 23 | print(statistics.median(max_temps)) 24 | print(statistics.median(min_temps)) 25 | 26 | # use the standard deviation function to find outlier temperatures 27 | upper_outlier = max_mean + statistics.stdev(max_temps) * 2 28 | lower_outlier = min_mean - statistics.stdev(min_temps) * 2 29 | print(upper_outlier) 30 | print(lower_outlier) 31 | 32 | max_outliers = [t for t in max_temps if t >= upper_outlier] 33 | min_outliers = [t for t in min_temps if t <= lower_outlier] 34 | print("Upper outliers", max_outliers) 35 | print("Lower outliers", min_outliers) 36 | -------------------------------------------------------------------------------- /Finished/Ch3/stats_challenge_soln.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Using the statistics package 3 | 4 | import json 5 | import statistics 6 | 7 | # open the sample weather data file and use the json module to load and parse it 8 | with open("../../sample-weather-history.json", "r") as weatherfile: 9 | weatherdata = json.load(weatherfile) 10 | 11 | def average_temp(day): 12 | return (day['tmin'] + day['tmax']) / 2 13 | 14 | # select the days from the summer months from all the years 15 | winters = ["-12-","-01-","-02-"] 16 | winter_months = [d for d in weatherdata if any([month in d['date'] for month in winters])] 17 | 18 | avg_temps = [average_temp(day) for day in winter_months] 19 | avg_mean = statistics.mean(avg_temps) 20 | 21 | outlier_temp = avg_mean + statistics.stdev(avg_temps) * 2 22 | outliers = [day for day in winter_months if average_temp(day) >= outlier_temp] 23 | print(avg_mean) 24 | print(len(outliers)) 25 | print(outliers) 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | LinkedIn Learning Exercise Files License Agreement 2 | ================================================== 3 | 4 | This License Agreement (the "Agreement") is a binding legal agreement 5 | between you (as an individual or entity, as applicable) and LinkedIn 6 | Corporation (“LinkedIn”). By downloading or using the LinkedIn Learning 7 | exercise files in this repository (“Licensed Materials”), you agree to 8 | be bound by the terms of this Agreement. If you do not agree to these 9 | terms, do not download or use the Licensed Materials. 10 | 11 | 1. License. 12 | - a. Subject to the terms of this Agreement, LinkedIn hereby grants LinkedIn 13 | members during their LinkedIn Learning subscription a non-exclusive, 14 | non-transferable copyright license, for internal use only, to 1) make a 15 | reasonable number of copies of the Licensed Materials, and 2) make 16 | derivative works of the Licensed Materials for the sole purpose of 17 | practicing skills taught in LinkedIn Learning courses. 18 | - b. Distribution. Unless otherwise noted in the Licensed Materials, subject 19 | to the terms of this Agreement, LinkedIn hereby grants LinkedIn members 20 | with a LinkedIn Learning subscription a non-exclusive, non-transferable 21 | copyright license to distribute the Licensed Materials, except the 22 | Licensed Materials may not be included in any product or service (or 23 | otherwise used) to instruct or educate others. 24 | 25 | 2. Restrictions and Intellectual Property. 26 | - a. You may not to use, modify, copy, make derivative works of, publish, 27 | distribute, rent, lease, sell, sublicense, assign or otherwise transfer the 28 | Licensed Materials, except as expressly set forth above in Section 1. 29 | - b. Linkedin (and its licensors) retains its intellectual property rights 30 | in the Licensed Materials. Except as expressly set forth in Section 1, 31 | LinkedIn grants no licenses. 32 | - c. You indemnify LinkedIn and its licensors and affiliates for i) any 33 | alleged infringement or misappropriation of any intellectual property rights 34 | of any third party based on modifications you make to the Licensed Materials, 35 | ii) any claims arising from your use or distribution of all or part of the 36 | Licensed Materials and iii) a breach of this Agreement. You will defend, hold 37 | harmless, and indemnify LinkedIn and its affiliates (and our and their 38 | respective employees, shareholders, and directors) from any claim or action 39 | brought by a third party, including all damages, liabilities, costs and 40 | expenses, including reasonable attorneys’ fees, to the extent resulting from, 41 | alleged to have resulted from, or in connection with: (a) your breach of your 42 | obligations herein; or (b) your use or distribution of any Licensed Materials. 43 | 44 | 3. Open source. This code may include open source software, which may be 45 | subject to other license terms as provided in the files. 46 | 47 | 4. Warranty Disclaimer. LINKEDIN PROVIDES THE LICENSED MATERIALS ON AN “AS IS” 48 | AND “AS AVAILABLE” BASIS. LINKEDIN MAKES NO REPRESENTATION OR WARRANTY, 49 | WHETHER EXPRESS OR IMPLIED, ABOUT THE LICENSED MATERIALS, INCLUDING ANY 50 | REPRESENTATION THAT THE LICENSED MATERIALS WILL BE FREE OF ERRORS, BUGS OR 51 | INTERRUPTIONS, OR THAT THE LICENSED MATERIALS ARE ACCURATE, COMPLETE OR 52 | OTHERWISE VALID. TO THE FULLEST EXTENT PERMITTED BY LAW, LINKEDIN AND ITS 53 | AFFILIATES DISCLAIM ANY IMPLIED OR STATUTORY WARRANTY OR CONDITION, INCLUDING 54 | ANY IMPLIED WARRANTY OR CONDITION OF MERCHANTABILITY OR FITNESS FOR A 55 | PARTICULAR PURPOSE, AVAILABILITY, SECURITY, TITLE AND/OR NON-INFRINGEMENT. 56 | YOUR USE OF THE LICENSED MATERIALS IS AT YOUR OWN DISCRETION AND RISK, AND 57 | YOU WILL BE SOLELY RESPONSIBLE FOR ANY DAMAGE THAT RESULTS FROM USE OF THE 58 | LICENSED MATERIALS TO YOUR COMPUTER SYSTEM OR LOSS OF DATA. NO ADVICE OR 59 | INFORMATION, WHETHER ORAL OR WRITTEN, OBTAINED BY YOU FROM US OR THROUGH OR 60 | FROM THE LICENSED MATERIALS WILL CREATE ANY WARRANTY OR CONDITION NOT 61 | EXPRESSLY STATED IN THESE TERMS. 62 | 63 | 5. Limitation of Liability. LINKEDIN SHALL NOT BE LIABLE FOR ANY INDIRECT, 64 | INCIDENTAL, SPECIAL, PUNITIVE, CONSEQUENTIAL OR EXEMPLARY DAMAGES, INCLUDING 65 | BUT NOT LIMITED TO, DAMAGES FOR LOSS OF PROFITS, GOODWILL, USE, DATA OR OTHER 66 | INTANGIBLE LOSSES . IN NO EVENT WILL LINKEDIN'S AGGREGATE LIABILITY TO YOU 67 | EXCEED $100. THIS LIMITATION OF LIABILITY SHALL: 68 | - i. APPLY REGARDLESS OF WHETHER (A) YOU BASE YOUR CLAIM ON CONTRACT, TORT, 69 | STATUTE, OR ANY OTHER LEGAL THEORY, (B) WE KNEW OR SHOULD HAVE KNOWN ABOUT 70 | THE POSSIBILITY OF SUCH DAMAGES, OR (C) THE LIMITED REMEDIES PROVIDED IN THIS 71 | SECTION FAIL OF THEIR ESSENTIAL PURPOSE; AND 72 | - ii. NOT APPLY TO ANY DAMAGE THAT LINKEDIN MAY CAUSE YOU INTENTIONALLY OR 73 | KNOWINGLY IN VIOLATION OF THESE TERMS OR APPLICABLE LAW, OR AS OTHERWISE 74 | MANDATED BY APPLICABLE LAW THAT CANNOT BE DISCLAIMED IN THESE TERMS. 75 | 76 | 6. Termination. This Agreement automatically terminates upon your breach of 77 | this Agreement or termination of your LinkedIn Learning subscription. On 78 | termination, all licenses granted under this Agreement will terminate 79 | immediately and you will delete the Licensed Materials. Sections 2-7 of this 80 | Agreement survive any termination of this Agreement. LinkedIn may discontinue 81 | the availability of some or all of the Licensed Materials at any time for any 82 | reason. 83 | 84 | 7. Miscellaneous. This Agreement will be governed by and construed in 85 | accordance with the laws of the State of California without regard to conflict 86 | of laws principles. The exclusive forum for any disputes arising out of or 87 | relating to this Agreement shall be an appropriate federal or state court 88 | sitting in the County of Santa Clara, State of California. If LinkedIn does 89 | not act to enforce a breach of this Agreement, that does not mean that 90 | LinkedIn has waived its right to enforce this Agreement. The Agreement does 91 | not create a partnership, agency relationship, or joint venture between the 92 | parties. Neither party has the power or authority to bind the other or to 93 | create any obligation or responsibility on behalf of the other. You may not, 94 | without LinkedIn’s prior written consent, assign or delegate any rights or 95 | obligations under these terms, including in connection with a change of 96 | control. Any purported assignment and delegation shall be ineffective. The 97 | Agreement shall bind and inure to the benefit of the parties, their respective 98 | successors and permitted assigns. If any provision of the Agreement is 99 | unenforceable, that provision will be modified to render it enforceable to the 100 | extent possible to give effect to the parties’ intentions and the remaining 101 | provisions will not be affected. This Agreement is the only agreement between 102 | you and LinkedIn regarding the Licensed Materials, and supersedes all prior 103 | agreements relating to the Licensed Materials. 104 | 105 | Last Updated: March 2019 106 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Copyright 2023 LinkedIn Corporation 2 | All Rights Reserved. 3 | 4 | Licensed under the LinkedIn Learning Exercise File License (the "License"). 5 | See LICENSE in the project root for license information. 6 | 7 | Please note, this project may automatically load third party code from external 8 | repositories (for example, NPM modules, Composer packages, or other dependencies). 9 | If so, such third party code may be subject to other license terms than as set 10 | forth above. In addition, such third party code may also depend on and load 11 | multiple tiers of dependencies. Please review the applicable licenses of the 12 | additional dependencies. 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hands-On Advanced Python: Data Exploration and Manipulation 2 | This is the repository for the LinkedIn Learning course Hands-On Advanced Python: Data Exploration and Manipulation. The full course is available from [LinkedIn Learning][lil-course-url]. 3 | 4 | ![Hands-On Advanced Python: Data Exploration and Manipulation][lil-thumbnail-url] 5 | 6 | Looking to get up to speed with advanced coding skills in Python? This course was made for you. Join instructor Joe Marini in this hands-on, interactive, skills-first coding course designed for advanced-level Python developers. Explore critical Python coding skills to boost your technical know-how or prepare for an interview to land a new role. This course includes Code Challenges powered by CoderPad: interactive coding exercises with real-time feedback, so you can get hands-on coding practice to advance your coding skills. Joe helps you develop your skills as a Python programmer with five specific, data-focused coding challenges. Practice parsing and exploring data, working with collections, math and statistics, and more. 7 | 8 | ## Installing 9 | 1. To use these exercise files, you must have the following installed: 10 | - Python 3.10 11 | - A text editor like Visual Studio Code 12 | 2. You can also work online directly using a Github Codespace. 13 | 3. To work locally, clone this repository into your local machine using the terminal (Mac), CMD (Windows), or a GUI tool like SourceTree. 14 | 15 | ## Instructor 16 | Joe Marini has been building software professionally for some of the biggest and best known companies in Silicon Valley for more than 30 years. 17 | 18 | Check out my other courses on [LinkedIn Learning](https://www.linkedin.com/learning/instructors/joe-marini). 19 | 20 | [lil-course-url]: https://www.linkedin.com/learning/hands-on-advanced-python-data-exploration-and-manipulation?dApp=59033956&leis=LAA 21 | [lil-thumbnail-url]: https://media.licdn.com/dms/image/D560DAQEeGQr9A1OHHQ/learning-public-crop_675_1200/0/1692640821642?e=2147483647&v=beta&t=w3OoiLzfdzhsR_HOCYB2RBQFGyEqmY4Jx_LfMhfw4KQ 22 | 23 | -------------------------------------------------------------------------------- /Start/Ch1/basicdata.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Introspect the data to make some determinations 3 | 4 | import json 5 | import pprint 6 | 7 | # open the sample weather data file and use the json module to load and parse it 8 | with open("../../sample-weather-history.json", "r") as weatherfile: 9 | weatherdata = json.load(weatherfile) 10 | 11 | # TODO: What was the warmest day in the data set? 12 | 13 | 14 | # TODO: What was the coldest day in the data set? 15 | 16 | 17 | # TODO: How many days had snowfall? 18 | 19 | -------------------------------------------------------------------------------- /Start/Ch1/filteringdata.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Filter values out of a data set based on some criteria 3 | 4 | import json 5 | import pprint 6 | 7 | # open the sample weather data file and use the json module to load and parse it 8 | with open("../../sample-weather-history.json", "r") as weatherfile: 9 | weatherdata = json.load(weatherfile) 10 | 11 | # the filter() function gives us a way to remove unwanted data points 12 | # TODO: create a subset of the data for days that had snowfall 13 | 14 | 15 | # TODO: pretty-print the resulting data set 16 | 17 | 18 | # filter can also be used on non-numerical data, like strings 19 | # TODO: create a subset that contains summer days with heavy rain (more than 1 in, about 2.5cm) 20 | def is_summer_rain_day(d): 21 | return False 22 | -------------------------------------------------------------------------------- /Start/Ch1/parsedata.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Load and parse a JSON data file and determine some information about it 3 | 4 | 5 | # TODO: open the sample weather data file and use the json module to load and parse it 6 | 7 | 8 | # TODO: make sure the data loaded correctly by printing the length of the dataset 9 | 10 | # TODO: let's also take a look at the first item in the data 11 | 12 | 13 | # TODO: How many days of data do we have for each year? 14 | 15 | -------------------------------------------------------------------------------- /Start/Ch1/sortingdata.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Sort the data in a sequence 3 | 4 | import json 5 | import pprint 6 | 7 | # open the sample weather data file and use the json module to load and parse it 8 | with open("../../sample-weather-history.json", "r") as weatherfile: 9 | weatherdata = json.load(weatherfile) 10 | 11 | # create a subset of the data for days that had snowfall 12 | dataset = list(filter(lambda d: d['snow'] > 0.0, weatherdata)) 13 | 14 | # sort the entire data set by how much snowfall there was 15 | # TODO: method 1: use the sorted() function to create a new list 16 | 17 | 18 | # TODO: method 2: use the sort() function that every list has to sort in-place 19 | 20 | 21 | # TODO: Sort on multiple fields: first by snowfall, then by average wind speed 22 | -------------------------------------------------------------------------------- /Start/Ch1/transformingdata.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Transform data from one format to another 3 | 4 | import json 5 | import copy 6 | import pprint 7 | 8 | # open the sample weather data file and use the json module to load and parse it 9 | with open("../../sample-weather-history.json", "r") as weatherfile: 10 | weatherdata = json.load(weatherfile) 11 | 12 | # the map() function is used to transform data from one form to another 13 | # TODO: Let's convert the weather data from imperial to metric units 14 | def ToC(f): 15 | return (f - 32) * 5/9 16 | 17 | 18 | def ToMM(i): 19 | return i * 25.4 20 | 21 | 22 | def ToKPH(s): 23 | return s * 1.60934 24 | 25 | 26 | def ToMetric(wd): 27 | pass 28 | 29 | # TODO: Use map() to call ToMetric and convert weatherdata to metric 30 | 31 | 32 | # TODO: use the map() function to convert objects to tuples 33 | # in this case, create tuples with a date and the average of tmin and tmax 34 | Avg_Temp = lambda t1, t2: (t1 + t2) / 2.0 35 | -------------------------------------------------------------------------------- /Start/Ch2/dateops.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Working with date values 3 | 4 | import json 5 | 6 | 7 | # open the sample weather data file and use the json module to load and parse it 8 | with open("../../sample-weather-history.json", "r") as weatherfile: 9 | weatherdata = json.load(weatherfile) 10 | 11 | 12 | # TODO: The datetime module converts strings into dates fairly easily 13 | 14 | 15 | # TODO: Date objects give us information such as day of week (0=Monday, 6=Sunday) 16 | 17 | 18 | # TODO: what was the warmest weekend day in the dataset? 19 | 20 | 21 | # The timedelta object provides an easy way of performing date math 22 | # find the coldest day of the year and get the average temp for the following week 23 | # coldest_day = min(weatherdata, key=lambda d: d['tmin']) 24 | # convert the date to a Python date 25 | # coldest_date = date.fromisoformat(coldest_day['date']) 26 | # print(f"The coldest day of the year was {str(coldest_date)} ({coldest_day['tmin']})") 27 | 28 | # TODO: Look up the next 7 days 29 | -------------------------------------------------------------------------------- /Start/Ch2/defaultdict.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Count items using a default dictionary 3 | 4 | import json 5 | import pprint 6 | 7 | 8 | # open the sample weather data file and use the json module to load and parse it 9 | with open("../../sample-weather-history.json", "r") as weatherfile: 10 | weatherdata = json.load(weatherfile) 11 | 12 | # The defaultdict collection provides a cleaner way of initializing key values 13 | # TODO: Count the number of data points for each year we have data 14 | 15 | 16 | # TODO: defaultdict can use more complex objects, like lists 17 | 18 | # TODO: create a dictionary with year-month keys and lists for each day in the month 19 | 20 | 21 | # What were the coldest and warmest days of each month? 22 | def warmest_day(month): 23 | wd = max(month, key=lambda d: d['tmax']) 24 | return (wd['date'], wd['tmax']) 25 | 26 | def coldest_day(month): 27 | cd = min(month, key=lambda d: d['tmin']) 28 | return (cd['date'], cd['tmin']) 29 | 30 | # TODO: loop over the keys of the dictionary and find each warmest and coldest day 31 | -------------------------------------------------------------------------------- /Start/Ch2/grouping.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Arrange data into groups 3 | 4 | import json 5 | from collections import defaultdict 6 | import pprint 7 | 8 | # open the sample weather data file and use the json module to load and parse it 9 | with open("../../sample-weather-history.json", "r") as weatherfile: 10 | weatherdata = json.load(weatherfile) 11 | 12 | # get all the measurements for a particular year 13 | year = [day for day in weatherdata if "2022" in day['date']] 14 | 15 | # create manual grouping of days that had a certain level of precipitation 16 | year.sort(key=lambda d:d['prcp']) 17 | datagroup = defaultdict(list) 18 | for d in year: 19 | datagroup[d['prcp']].append(d['date']) 20 | print(f"{len(datagroup)} total precipitation groups") 21 | pprint.pp(datagroup) 22 | 23 | # TODO: Use groupby to get the days of a given year by how much precipitation happened 24 | 25 | 26 | # TODO: How many groups do we have? Now we can use len() on the dictionary 27 | 28 | 29 | # TODO: we can iterate over the dictionary to list each group 30 | -------------------------------------------------------------------------------- /Start/Ch2/reducefunc.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Using the reduce function 3 | 4 | import json 5 | 6 | 7 | # open the sample weather data file and use the json module to load and parse it 8 | with open("../../sample-weather-history.json", "r") as weatherfile: 9 | weatherdata = json.load(weatherfile) 10 | 11 | # TODO: how much snowfall is in the entire dataset? 12 | 13 | 14 | # TODO: how much total precipitation is in the entire dataset? 15 | 16 | 17 | # TODO: What was the warmest day in which it snowed? Need to find highest 'tmax' for all 18 | # days where 'snow' > 0 19 | def warm_snow_day(acc, elem): 20 | # return the elem value if the snow amount > 0 and its tmax value is 21 | # larger than the tmax value that is in the acc argument 22 | pass 23 | 24 | # define a "zero" value start date for the reduce function to start with 25 | start_val = { 26 | "date": "1900-01-01", 27 | "tmin": 0, 28 | "tmax": 0, 29 | "prcp": 0.0, 30 | "snow": 0.0, 31 | "snwd": 0.0, 32 | "awnd": 0.0 33 | } 34 | 35 | # TODO: reduce the data set to the warmest snow day 36 | -------------------------------------------------------------------------------- /Start/Ch3/randomnumbers.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | 3 | import json 4 | import pprint 5 | import random 6 | 7 | # open the sample weather data file and use the json module to load and parse it 8 | with open("../../sample-weather-history.json", "r") as weatherfile: 9 | weatherdata = json.load(weatherfile) 10 | 11 | # TODO: the random module can be used to generate random values 12 | 13 | 14 | # TODO: choose a random number in a range including both points 15 | 16 | # TODO: choose a random number in a range excluding end point 17 | 18 | 19 | # build a list of the summer days in 2019 20 | def is_summer_day(d): 21 | summer_months = ["2019-07-", "2019-08-"] 22 | if any([m in d['date'] for m in summer_months]): 23 | return True 24 | return False 25 | summer_2019 = list(filter(is_summer_day, weatherdata)) 26 | 27 | # TODO: choose 5 random days from that summer 28 | 29 | 30 | # TODO: what was the windiest of those 5 days? 31 | -------------------------------------------------------------------------------- /Start/Ch3/randomoperations.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | 3 | import json 4 | import random 5 | import pprint 6 | 7 | # open the sample weather data file and use the json module to load and parse it 8 | with open("../../sample-weather-history.json", "r") as weatherfile: 9 | weatherdata = json.load(weatherfile) 10 | 11 | # get the first 30 days in the dataset 12 | month_data = weatherdata[0:30] 13 | 14 | # TODO: the shuffle() function will randomly shuffle a list in-place 15 | 16 | 17 | # TODO: use choice() and choices() to get random items, but beware that 18 | # these functions can produce duplicate results 19 | 20 | 21 | # TODO: the sample() function will choose random items with no duplicates 22 | -------------------------------------------------------------------------------- /Start/Ch3/statisticsfuncs.py: -------------------------------------------------------------------------------- 1 | # Example file for Advanced Python: Hands On by Joe Marini 2 | # Using the statistics package 3 | 4 | import json 5 | import statistics 6 | 7 | # open the sample weather data file and use the json module to load and parse it 8 | with open("../../sample-weather-history.json", "r") as weatherfile: 9 | weatherdata = json.load(weatherfile) 10 | 11 | # select the days from the summer months from all the years 12 | summers = ["-06-","-07-","-08-"] 13 | summer_months = [d for d in weatherdata if any([month in d['date'] for month in summers])] 14 | print(f"Data for {len(summer_months)} summer days") 15 | 16 | # TODO: calculate the mean for both min and max temperatures 17 | 18 | 19 | # TODO: calculate the median values for min and max temperatures 20 | 21 | 22 | # TODO: use the standard deviation function to find outlier temperatures 23 | --------------------------------------------------------------------------------