Lecture 23: Tuples Adapted from slides by Marty Stepp and Stuart Reges

Slides:



Advertisements
Similar presentations
DOT 1 January , 1990 DOT 2 July 23 - August 3, 1990.
Advertisements

Chubaka Producciones Presenta :.
2012 JANUARY Sun Mon Tue Wed Thu Fri Sat
P Pathophysiology Calendar. SundayMondayTuesdayWednesdayThursdayFridaySaturday January 2012.
Chicas, este calendario si es pa' nosotras !!!!!.
MONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSAT/SUN Note: You can print this template to use as a wall calendar. You can also copy the slide for any month to add.
You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
2007 Monthly Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
MONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSAT/SUN Note: You can print this template to use as a wall calendar. You can also copy the slide for any month to add.
January 2013 MondayTuesdayWednesdayThursdayFridaySaturdaySunday.
You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation. If you’d like to change.
WORD JUMBLE. Months of the year Word in jumbled form e r r f b u y a Word in jumbled form e r r f b u y a february Click for the answer Next Question.
Months of the Year Macarena song.
DATE POWER 2 INCOME JANUARY 100member X 25.00P2, FEBRUARY 200member X 25.00P5, MARCH 400member X 25.00P10, APRIL 800member.
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
July 2007 SundayMondayTuesdayWednesdayThursdayFridaySaturday
Deadline for Requisitions Payment Processing Date
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own.
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Spring 2017 Lecture 29: Sets and Dictionaries
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Lecture 24: print revisited, tuples cont.
CSc 110, Spring 2017 Lecture 28: Sets and Dictionaries
CSc 110, Autumn 2017 Lecture 31: Dictionaries
CSc 110, Autumn 2017 Lecture 30: Sets and Dictionaries
Dictation practice 2nd Form Ms. Micaela-Ms. Verónica.
TIMELINES PHOTOS This is an example text
TIMELINES PHOTOS This is an example text
McDonald’s Kalender 2009.
McDonald’s Kalender 2009.
CSc 110, Spring 2018 Lecture 33: Dictionaries
Adapted from slides by Marty Stepp and Stuart Reges
School Year Calendar You can print this template to use it as a wall calendar, or you can copy the page for any month to add it to your own presentation.
Thanks to Atif Memon from UMD for disaster examples
CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random
Lecture 23: Tuples Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
CSc 110, Autumn 2017 Lecture 27: Lists that change size and Tuples
1   1.テキストの入れ替え テキストを自由に入れ替えることができます。 フチなし全面印刷がおすすめです。 印刷のポイント.
2017/18 Payment Calendar Due Date Cut-off Day 1st of every month
McDonald’s Kalender 2009.
Adapted from slides by Marty Stepp and Stuart Reges
Problem Gambling Clicks to Opgr.org
2300 (11PM) September 21 Blue line is meridian..
References and objects
McDonald’s calendar 2007.
CSc 110, Spring 2018 Lecture 10: More Graphics
1 - January - Sun Mon The Wed Thu Fri Sat
Proud As A Peacock! We are very proud of__________________
Teacher name August phone: Enter text here.
CSc 110, Autumn 2017 Lecture 8: More Graphics
A programming problem Given a file of cities' (x, y) coordinates, which begins with the number of cities: Write.
Circle Chart Template Process Name.
Take a walk down the canal and see the changes that happen as each month comes and goes. SAMPLE SLIDE Random Slides From This PowerPoint Show
February 2007 Note: Source:.
January 2015 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
Adapted from slides by Marty Stepp and Stuart Reges
MONTHS OF THE YEAR January February April March June May July August
Lecture 24: Lists of Lists
McDonald’s calendar 2007.
Building Java Programs
Production Month Sun Hours K Monthly Kwh Tou Peak Value After Kwh
Habitat Changes and Fish Migration
2015 January February March April May June July August September
Habitat Changes and Fish Migration
Presentation transcript:

Lecture 23: Tuples Adapted from slides by Marty Stepp and Stuart Reges CSc 110, Spring 2017 Lecture 23: Tuples Adapted from slides by Marty Stepp and Stuart Reges

A programming problem Given a file of cities' names and (x, y) coordinates: Winslow 50 20 Tucson 90 60 Phoenix 10 72 Bisbee 74 98 Yuma 5 136 Page 150 91 Write a program to draw the cities on a DrawingPanel, then simulates an earthquake that turns all cities red that are within a given radius: Epicenter x? 100 Epicenter y? 100 Affected radius? 75

A poor solution lines = open("cities.txt").readlines() names = [0] * len(lines) x_coords = [0] * len(lines) y_coords = [0] * len(lines) for i in range(0, len(lines)): parts = lines[i].split() names[i] = parts[0] # city name x_coords[i] = parts[1] y_coords[i] = parts[2] ... What's bad about this solution?

A poor solution names[i] = parts[0] # city name x_coords[i] = parts[1] y_coords[i] = parts[2] ... Parallel lists: two or more lists with related data at the same indices. Parallel lists can easily lead to bugs: may get "out of sync" if you add an x-coordinate but not a y-coordinate Would have to pass all three lists as parameters to a function. Is there a better representation?

Observations Each item in the data set is a name, an x-coordinate and y-coordinate for a given city Winslow 50 20 It would be better to associate these values

Tuples Good for associating a fixed number of items Syntax for creating a tuple: (value0, value1, … ,valueN) Example: ("Tucson", 90, 60) Tuples can be subscripted just like lists and strings: >>> t = ("Tucson", 90, 60) >>> t ('Tucson', 90, 60) >>> t[0] 'Tucson'

Tuples vs. lists Tuples Lists tuples hold a fixed number of items the items in a tuple cannot be assigned to >>> t = ("Tucson", 90, 60) >>> t ('Tucson', 90, 60) >>> t[0] = "OldPueblo" … TypeError: 'tuple' object does not support item assignment Lists lists may grow or shrink the items in a list can be assigned to typically a list holds values of the same type (e.g., all integers or all strings)

Using tuples As mentioned, tuples are subscripted just like lists and strings t = ("Tucson", 90, 60) x_coord = t[1] You can loop through tuples the same as lists and strings operation call result len() len((1, 2, 3)) 3 + (1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) * ('Hi', 1) * 2 ('Hi', 1, 'Hi', 1) in 3 in (1, 2, 3) True for for x in (1,2,3): print(x) 1 2 min() min((1, 3)) max() max((1, 3))

Using tuples >>> book = ("Pride and Prejudice", "Austin", 1813, "Fiction") >>> book ('Pride and Prejudice', 'Austin', 1813, 'Fiction') >>> len(book) 4 >>> "Fiction" in book True >>> for item in book: print(item) Pride and Prejudice Austin 1813 Fiction >>>

Zipval Write a function called zipval(lst, value) that take a list and value as parameters and returns a list of tuples consisting of each element of the list and the value call return zipval([10,20,30], "a") [(10,'a'), (20,'a'), (30, 'a'])

Zip Write a function called zip(a, b) that takes two lists as parameters and returns a list of tuples. Each tuple consists of the paired consecutive values of the parameter lists. call return zip([1,2,3],[4,5,6]) [(1,4), (2,5), (3, 6)]

A programming problem Given a file of cities' names and (x, y) coordinates: Winslow 50 20 Tucson 90 60 Phoenix 10 72 Bisbee 74 98 Yuma 5 136 Page 150 91 Write a program to draw the cities on a DrawingPanel, then simulates an earthquake that turns all cities red that are within a given radius: Epicenter x? 100 Epicenter y? 100 Affected radius? 75

Earthquake plot # Draws all of the cities affected by earthquakes # given the radius input by the user. from drawingpanel import * def main(): cities = get_cities() # gets information from the user epi_x = int(input("Epicenter x? ")) epi_y = int(input("Epicenter y? ")) radius = int(input("Radius? ")) draw(cities, epi_x, epi_y, radius) ...

Earthquake plot – cont. Let's write get_cities(). First step is the pseudocode. Open the file cities.txt Read all the lines from the file For each line of the file create a tuple of the city and the x and y coordinates add that tuple to a list return the list

Earthquake plot – cont. def get_cities(): file = open("cities.txt") # Returns a list of tuples. Each tuple contains a city name, x and y # coordinates from one line of cities.txt def get_cities(): file = open("cities.txt") lines = file.readlines() cities = [] for line in lines: # format: 'Tucson, 60, 90' parts = line.split() city = (parts[0], parts[1], parts[2]) cities.append(city) return cities

Earthquake plot – cont. def draw(cities, epi_x, epi_y, radius): # Draws all of the cities as dots on a DrawingPanel. If in # the affected radius, colors them red, otherwise black. # Draws a circle around the affected region. def draw(cities, epi_x, epi_y, radius): p = DrawingPanel(400, 400) p.canvas.create_oval(epi_x - radius, epi_y - radius, epi_x + radius, epi_y + radius) for city in cities: # the variable city is a tuple x = int(city[1]) # get x-coordinate y = int(city[2]) # get y-coordinate color = "black" if(x >= epi_x - radius and x <= epi_x + radius and y >= epi_y - radius and y <= epi_y + radius): color = "red" p.canvas.create_oval(x, y, x + 4, y + 4, outline=color) …

Days till Write a function called days_till that accepts a start month and day and a stop month and day and returns the number of days between them call return days_till("januAry", 1, "January", 10) 9 days_till("novembeR", 15, "december", 10) 25 days_till("OCTober", 6, "december", 17) 72 days_till("october", 6, "ocTober", 1) 360

Days till solution def days_till(start_month, start_day, stop_month, stop_day): months = [('january', 31),('february', 28),('march', 31),('april', 30), ('may', 31),('june', 30), ('july', 31), ('august', 31),('september', 30), ('october', 31), ('november', 30), ('december', 31)] if start_month.lower() == stop_month.lower() and stop_day >= start_day: return stop_day - start_day days = 0 for i in range(0, len(months)): month = months[i] if month[0] == start_month.lower(): days = month[1] - start_day i += 1 while months[i % 12][0] != stop_month.lower(): days += months[i % 12][1] days += stop_day return days