def stack_create():
return []
s = stack_create()
type(s)
a = dict()
print(a)
b = dict([("foo", 42), ("bar", 37)])
print(b)
c = dict(b)
print(c)
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
p1 = Point(42,37)
p2 = Point(0,0)
p3 = Point(-3,4)
p1.x
p1.y
p2.x
p2.y
d1 = dict( [("foo", 42), ("bar", 37)] )
d2 = dict( [("foo", 100), ("BAZ", 45)] )
print(d1)
print(d2)
d1.get("foo", "NOPE")
d2.get("foo", "NOPE")
d1.get("bar", "NOPE")
d2.get("baz", "NOPE")
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def distance_to_origin(self):
return (self.x**2 + self.y**2)**0.5
p1 = Point(3, 4)
p1.distance_to_origin()
p2 = Point(0, 0)
p2.distance_to_origin()
p3 = Point(1, 1)
p3.distance_to_origin()
class Stack:
def __init__(self):
self.lst = []
class Stack:
def __init__(self):
self.lst = []
def push(self, value):
self.lst.append(value)
def pop(self):
value = self.lst.pop()
return value
def peek(self):
return self.lst[-1]
def is_empty(self):
return len(self.lst) == 0
s = Stack()
s.push(42)
s.push(37)
s.push(101)
s.push(53)
print(s.lst)
s.pop()
print(s.lst)
s.lst[1] = "FOOBAR"
print(s.lst)
class Stack:
def __init__(self):
self.__lst = []
def push(self, value):
self.__lst.append(value)
def pop(self):
value = self.__lst.pop()
return value
def peek(self):
return self.__lst[-1]
def is_empty(self):
return len(self.__lst) == 0
s = Stack()
s.push(42)
s.push(37)
s.push(101)
s.push(53)
s.push(76)
s.pop()
s.is_empty()
print(s.__lst)
print(s)
class Stack:
def __init__(self):
self.__lst = []
def push(self, value):
self.__lst.append(value)
def pop(self):
value = self.__lst.pop()
return value
def peek(self):
return self.__lst[-1]
def is_empty(self):
return len(self.__lst) == 0
def __repr__(self):
return "MY STACK"
s = Stack()
s.push(42)
s.push(37)
s.push(101)
s.push(53)
s.push(76)
print(s)
class Stack:
def __init__(self):
self.__lst = []
def push(self, value):
self.__lst.append(value)
def pop(self):
value = self.__lst.pop()
return value
def peek(self):
return self.__lst[-1]
def is_empty(self):
return len(self.__lst) == 0
def __repr__(self):
return "STACK: " + ", ".join(str(v) for v in self.__lst) + " (top)"
s = Stack()
s.push(42)
s.push(37)
s.push(101)
s.push(53)
s.push(76)
print(s)
s1 = Stack()
s1.push(42)
s1.push(37)
s2 = Stack()
s2.push(42)
s2.push(37)
s1 == s2
class Stack:
def __init__(self):
self.__lst = []
def push(self, value):
self.__lst.append(value)
def pop(self):
value = self.__lst.pop()
return value
def peek(self):
return self.__lst[-1]
def is_empty(self):
return len(self.__lst) == 0
def __repr__(self):
return "STACK: " + ", ".join(str(v) for v in self.__lst) + " (top)"
def __eq__(self, other):
return self.__lst == other.__lst
s1 = Stack()
s1.push(42)
s1.push(37)
s2 = Stack()
s2.push(42)
s2.push(37)
s3 = Stack()
s3.push(100)
s1 == s2
s1 == s3
d1 = {"name": "Sam Q. Student", "majors": ["Computer Science"], "year": 2}
d2 = {"name": "Cris T. Student", "majors": ["Economics"], "year": 1}
d3 = {"name": "Ari F. Student", "majors": ["Mathematics", "Computer Science"], "year": 4}
dict_students = [d1, d2, d3]
class Student:
def __init__(self, name, majors, year):
self.name = name
self.majors = majors
self.year = year
s1 = Student("Sam Q. Student", ["Computer Science"], 2)
s2 = Student("Cris T. Student", ["Economics"], 1)
s3 = Student("Ari F. Student", ["Mathematics", "Computer Science"], 4)
students = [s1, s2, s3]
class Student:
def __init__(self, name, majors, year):
self.name = name
self.majors = majors
self.year = year
def num_majors(self):
return len(self.majors)
def __repr__(self):
return "Student: {}".format(self.name)
# Without objects
d1 = {"name": "Sam Q. Student", "majors": ["Computer Science"], "year": 2}
d2 = {"name": "Cris T. Student", "majors": ["Economics"], "year": 1}
d3 = {"name": "Ari F. Student", "majors": ["Mathematics", "Computer Science"], "year": 4}
dict_students = [d1, d2, d3]
for s in dict_students:
print("Student: {}".format(s["name"]))
print("Number of majors:", len(s["majors"]))
print()
# With objects
s1 = Student("Sam Q. Student", ["Computer Science"], 2)
s2 = Student("Cris T. Student", ["Economics"], 1)
s3 = Student("Ari F. Student", ["Mathematics", "Computer Science"], 4)
students = [s1, s2, s3]
for s in students:
print(s)
print("Number of majors:", s.num_majors())
print()
d4 = {"name": "Alex D. Student", "majors": ["Computer Science"], "year": -3, "capacity": "4GB"}
class Student:
def __init__(self, name, majors, year):
assert isinstance(name, str), "name must be a string"
assert isinstance(majors, list) and all([isinstance(major, str) for major in majors]) ,\
"majors must be a list of strings"
assert 1 <= year <= 4, "year must be between 1 and 4"
self.name = name
self.majors = majors
self.year = year
def num_majors(self):
return len(self.majors)
def __repr__(self):
return "Student: {}".format(self.name)
Student("Alex D. Student", ["Computer Science"], -3)
Student("Alex D. Student", ["Computer Science"], 1, "4GB")
Student("Alex D. Student", ["Computer Science"], 1, capacity="4GB")
Student(57.0, ["Computer Science"], 1)
Student("Alex D. Student", [1,2,3], 1)
# Printing the number of majors with the original implementation of Student.
# Assertions ommitted for clarity
class Student:
def __init__(self, name, majors, year):
self.name = name
self.majors = majors
self.year = year
def num_majors(self):
return len(self.majors)
def __repr__(self):
return "Student: {}".format(self.name)
s1 = Student("Sam Q. Student", ["Computer Science"], 2)
s2 = Student("Cris T. Student", ["Economics"], 1)
s3 = Student("Ari F. Student", ["Mathematics", "Computer Science"], 4)
students = [s1, s2, s3]
for s in students:
print(s)
print("Number of majors:", s.num_majors())
print()
# We switch to an internal representation that separates the primary major
# from the rest of the majors. The code that prints out the majors continues
# working without any modifications.
class Student:
def __init__(self, name, majors, year):
self.name = name
self.primary_major = majors[0]
self.secondary_majors = majors[1:]
self.year = year
def num_majors(self):
return 1 + len(self.secondary_majors)
def __repr__(self):
return "Student: {}".format(self.name)
s1 = Student("Sam Q. Student", ["Computer Science"], 2)
s2 = Student("Cris T. Student", ["Economics"], 1)
s3 = Student("Ari F. Student", ["Mathematics", "Computer Science"], 4)
students = [s1, s2, s3]
for s in students:
print(s)
print("Number of majors:", s.num_majors())
print()
import csv
stations = []
with open("data/divvy_2013_stations.csv") as f:
reader = csv.DictReader(f)
for row in reader:
stations.append(row)
len(stations)
stations[0]
trips = []
with open("data/divvy_2013_trips_tiny.csv") as f:
reader = csv.DictReader(f)
for row in reader:
trips.append(row)
len(trips)
trips[0]
class DivvyStation(object):
def __init__(self, station_id, name, latitude, longitude,
dpcapacity, landmark, online_date):
self.station_id = station_id
self.name = name
self.latitude = latitude
self.longitude = longitude
self.dpcapacity = dpcapacity
self.landmark = landmark
self.online_date = online_date
def __repr__(self):
return "<DivvyStation object, station_id={}, name='{}'>".format(self.station_id, self.name)
s25 = DivvyStation(25, "Michigan Ave & Pearson St", 41.89766, -87.62351, 23, 34, "6/28/2013")
s44 = DivvyStation(44, "State St & Randolph St", 41.8847302, -87.62773357, 27, 2, "6/28/2013")
s52 = DivvyStation(52, "Michigan Ave & Lake St", 41.88605812, -87.62428934, 23, 43, "6/28/2013")
s25
[s25, s44, s52]
print("Destination:", s25)
class DivvyStation(object):
def __init__(self, station_id, name, latitude, longitude,
dpcapacity, landmark, online_date):
self.station_id = station_id
self.name = name
self.latitude = latitude
self.longitude = longitude
self.dpcapacity = dpcapacity
self.landmark = landmark
self.online_date = online_date
def __repr__(self):
return "<DivvyStation object, station_id={}, name='{}'>".format(self.station_id, self.name)
def __str__(self):
return "Divvy Station #{} ({})".format(self.station_id, self.name)
s25 = DivvyStation(25, "Michigan Ave & Pearson St", 41.89766, -87.62351, 23, 34, "6/28/2013")
s44 = DivvyStation(44, "State St & Randolph St", 41.8847302, -87.62773357, 27, 2, "6/28/2013")
s52 = DivvyStation(52, "Michigan Ave & Lake St", 41.88605812, -87.62428934, 23, 43, "6/28/2013")
stations = [s25, s44, s52]
s25
print("Destination:", s25)
class DivvyTrip(object):
def __init__(self, trip_id, starttime, stoptime, bikeid,
tripduration, from_station_id, from_station_name,
to_station_id, to_station_name,
usertype, gender, birthyear):
self.trip_id = trip_id
self.starttime = starttime
self.stoptime = stoptime
self.bikeid = bikeid
self.tripduration = tripduration
self.from_station_id = from_station_id
self.from_station_name = from_station_name
self.to_station_id = to_station_id
self.to_station_name = to_station_name
self.usertype = usertype
self.gender = gender
self.birthyear = birthyear
def __repr__(self):
return "<DivvyTrip object, trip_id={}, from_station={}, to_station={}>".format(
self.trip_id, self.from_station_id, self.to_station_id)
def __str__(self):
return "Divvy Trip #{} from {} to {}".format(self.trip_id, self.from_station_id, self.to_station_id)
class DivvyTrip(object):
def __init__(self, trip_id, starttime, stoptime, bikeid,
tripduration, from_station, to_station,
usertype, gender, birthyear):
self.trip_id = trip_id
self.starttime = starttime
self.stoptime = stoptime
self.bikeid = bikeid
self.tripduration = tripduration
self.from_station = from_station
self.to_station = to_station
self.usertype = usertype
self.gender = gender
self.birthyear = birthyear
def __repr__(self):
return "<DivvyTrip object, trip_id={}, from_station={}, to_station={}>".format(
self.trip_id, self.from_station.station_id, self.to_station.station_id)
def __str__(self):
return "Divvy Trip #{} from {} to {}".format(self.trip_id, self.from_station.name, self.to_station.name)
trip5433 = DivvyTrip(5433, "2013-06-28 10:43", "2013-06-28 11:03", 218, 1214,
s25, s44, "Customer", None, None)
trip4666 = DivvyTrip(4666, "2013-06-27 20:33", "2013-06-27 21:22", 242, 2936,
s44, s52, "Customer", None, None)
trip11236 = DivvyTrip(11236, "2013-06-30 15:41", "2013-06-30 15:58", 906, 1023,
s25, s44, "Customer", None, None)
trip4646 = DivvyTrip(4646, "2013-06-27 20:22", "2013-06-27 20:39", 477, 996,
s52, s52, "Customer", None, None)
trip13805 = DivvyTrip(13805, "2013-07-01 13:21", "2013-07-01 13:35", 469, 858,
s44, s25, "Customer", None, None)
trips = [trip5433, trip4666, trip11236, trip4646, trip13805]
trip5433.to_station
trip5433.from_station
trip5433.from_station.name
trip5433.from_station.longitude
trip5433.from_station.latitude
import math
class DivvyStation(object):
def __init__(self, station_id, name, latitude, longitude,
dpcapacity, landmark, online_date):
self.station_id = station_id
self.name = name
self.latitude = latitude
self.longitude = longitude
self.dpcapacity = dpcapacity
self.landmark = landmark
self.online_date = online_date
def __repr__(self):
return "<DivvyStation object, station_id={}, name='{}'>".format(self.station_id, self.name)
def __str__(self):
return "Divvy Station #{} ({})".format(self.station_id, self.name)
def distance_to(self, other):
diff_latitude = math.radians(other.latitude - self.latitude)
diff_longitude = math.radians(other.longitude - self.longitude)
a = math.sin(diff_latitude/2) * math.sin(diff_latitude/2) + \
math.cos(math.radians(self.latitude)) * \
math.cos(math.radians(other.latitude)) * \
math.sin(diff_longitude/2) * math.sin(diff_longitude/2)
d = 2 * math.asin(math.sqrt(a))
return 6371000.0 * d
class DivvyTrip(object):
def __init__(self, trip_id, starttime, stoptime, bikeid,
tripduration, from_station, to_station,
usertype, gender, birthyear):
self.trip_id = trip_id
self.starttime = starttime
self.stoptime = stoptime
self.bikeid = bikeid
self.tripduration = tripduration
self.from_station = from_station
self.to_station = to_station
self.usertype = usertype
self.gender = gender
self.birthyear = birthyear
def __repr__(self):
return "<DivvyTrip object, trip_id={}, from_station={}, to_station={}>".format(
self.trip_id, self.from_station.station_id, self.to_station.station_id)
def __str__(self):
return "Divvy Trip #{} from {} to {}".format(self.trip_id, self.from_station.name, self.to_station.name)
def get_distance(self):
return self.from_station.distance_to(self.to_station)
s25 = DivvyStation(25, "Michigan Ave & Pearson St", 41.89766, -87.62351, 23, 34, "6/28/2013")
s44 = DivvyStation(44, "State St & Randolph St", 41.8847302, -87.62773357, 27, 2, "6/28/2013")
s52 = DivvyStation(52, "Michigan Ave & Lake St", 41.88605812, -87.62428934, 23, 43, "6/28/2013")
stations = [s25, s44, s52]
trip5433 = DivvyTrip(5433, "2013-06-28 10:43", "2013-06-28 11:03", 218, 1214,
s25, s44, "Customer", None, None)
trip4666 = DivvyTrip(4666, "2013-06-27 20:33", "2013-06-27 21:22", 242, 2936,
s44, s52, "Customer", None, None)
trip11236 = DivvyTrip(11236, "2013-06-30 15:41", "2013-06-30 15:58", 906, 1023,
s25, s44, "Customer", None, None)
trip4646 = DivvyTrip(4646, "2013-06-27 20:22", "2013-06-27 20:39", 477, 996,
s52, s52, "Customer", None, None)
trip13805 = DivvyTrip(13805, "2013-07-01 13:21", "2013-07-01 13:35", 469, 858,
s44, s25, "Customer", None, None)
trips = [trip5433, trip4666, trip11236, trip4646, trip13805]
total_distance = 0
for trip in trips:
total_distance = trip.get_distance()
total_distance
total_duration = 0
for trip in trips:
total_duration = trip.tripduration
total_duration