This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Book(): | |
def __init__(self, title, author): | |
self.title = title | |
self.author = author | |
def __str__(self): | |
return '"{}" by {}'.format(self.title, self.author) | |
class PaperBook(Book): | |
def __init__(self, title, author, numPages): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1) Define a class called Bike that accepts a string and a float as input, and assigns those inputs respectively | |
# to two instance variables, color and price. Assign to the variable testOne an instance of Bike whose color is blue | |
# and whose price is 89.99. Assign to the variable testTwo an instance of Bike whose color is purple and | |
# whose price is 25.0. | |
# Solution: | |
class Bike: | |
def __init__(self, color, price): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point: | |
""" Point class for representing and manipulating x,y coordinates. """ | |
printed_rep = "*" | |
def __init__(self, initX, initY): | |
self.x = initX | |
self.y = initY |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
L = ["Cherry", "Apple", "Blueberry"] | |
print(sorted(L, key=len)) | |
#alternative form using lambda, if you find that easier to understand | |
print(sorted(L, key= lambda x: len(x))) | |
# Output: | |
# ['Apple', 'Cherry', 'Blueberry'] | |
# ['Apple', 'Cherry', 'Blueberry'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Suppose you have a point object and wish to find the midpoint halfway between it and some other target point. | |
# We would like to write a method, let’s call it halfway, which takes another Point as a parameter and returns | |
# the Point that is halfway between the point and the target point it accepts as input. | |
class Point: | |
def __init__(self, initX, initY): | |
self.x = initX | |
self.y = initY |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1) Write code to assign to the variable map_testing all the elements in lst_check while adding the string “Fruit: ” | |
# to the beginning of each element using mapping. | |
lst_check = ['plums', 'watermelon', 'kiwi', 'strawberries', 'blueberries', 'peaches', 'apples', 'mangos', 'papaya'] | |
map_testing = map(lambda str: "Fruit: " + str, lst_check) | |
print(map_testing) | |
# Ouput: ['Fruit: plums', 'Fruit: watermelon', 'Fruit: kiwi', 'Fruit: strawberries', 'Fruit: blueberries', 'Fruit: peaches', 'Fruit: apples', 'Fruit: mangos', 'Fruit: papaya'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The zip function takes multiple lists and turns them into a list of tuples | |
# (actually, an iterator, but they work like lists for most practical purposes), | |
# pairing up all the first items as one tuple, all the second items as a tuple, and so on. | |
# Then we can iterate through those tuples, and perform some operation on all the first items, | |
# all the second items, and so on. | |
L1 = [3, 4, 5] | |
L2 = [1, 2, 3] | |
L4 = list(zip(L1, L2)) | |
print(L4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python provides an alternative way to do map and filter operations, called a list comprehension. | |
# Many programmers find them easier to understand and write. List comprehensions are concise ways to | |
# create lists from other lists. The general syntax is: | |
# [<transformer_expression> for <loop_var> in <sequence> if <filtration_expression>] | |
things = [2, 5, 9] | |
yourlist = [value * 2 for value in things] # t[<trans.exp is "value * 2"> <item variable is "value"> <sequence is "things">] | |
print(yourlist) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Now consider another common pattern: going through a list and keeping only those items that meet certain criteria. This is called a filter. | |
# Again, this pattern of computation is so common that Python offers a more compact and general way to do it, | |
# the filter function. filter takes two arguments, a function and a sequence. The function takes one item | |
# and return True if the item should. It is automatically called for each item in the sequence. You don’t | |
# have to initialize an accumulator or iterate with a for loop. | |
def keep_evens(nums): | |
new_seq = filter(lambda num: num % 2 == 0, nums) | |
return list(new_seq) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1) Using map, create a list assigned to the variable greeting_doubled that doubles each element in the | |
# list lst. | |
lst = [["hi", "bye"], "hello", "goodbye", [9, 2], 4] | |
greeting_doubled = map((lambda a_list: a_list * 2), lst) | |
print(greeting_doubled) | |
# Output: | |
# [['hi', 'bye', 'hi', 'bye'], 'hellohello', 'goodbyegoodbye', [9, 2, 9, 2], 8] |
NewerOlder