Skip to content

Instantly share code, notes, and snippets.

View IvanFrecia's full-sized avatar

Ivan Frecia IvanFrecia

View GitHub Profile
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):
@IvanFrecia
IvanFrecia / course_4_assessment_1.py
Created May 11, 2021 22:31
Python Classes and Inheritance Week 1 Assessment: Week One course_4_assessment_1
# 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):
@IvanFrecia
IvanFrecia / Class_Variables_and_Instance_Variables.py
Created May 11, 2021 16:44
Class Variables and Instance Variables
class Point:
""" Point class for representing and manipulating x,y coordinates. """
printed_rep = "*"
def __init__(self, initX, initY):
self.x = initX
self.y = initY
@IvanFrecia
IvanFrecia / Sorting_Lists_of_Instances.py
Created May 11, 2021 16:25
Sorting Lists of Instances
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']
@IvanFrecia
IvanFrecia / Instances_as_Return_Values.py
Created May 11, 2021 16:04
Instances as Return Values
# 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
@IvanFrecia
IvanFrecia / course_3_assessment_2.py
Created May 4, 2021 20:59
Course 3 - Data Collection and Processing with Python - Week 2 - course_3_assesment_2
# 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']
@IvanFrecia
IvanFrecia / 23_5_Zip.py
Created May 4, 2021 15:28
Course 3 - Data Collection and Processing with Python - Week 2 - 23.5 Zip
# 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)
@IvanFrecia
IvanFrecia / 23_4_List-Comprehensions.py
Created May 3, 2021 21:11
Course 3 - Data Collection and Processing with Python - Week 2 - 23.4. List Comprehensions
# 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)
@IvanFrecia
IvanFrecia / 23_3_Filter.py
Created May 3, 2021 18:30
Data Collection and Processing with Python - Week 2 - 23.3 - Filter
# 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)
@IvanFrecia
IvanFrecia / 23_2_Map.py
Created May 3, 2021 16:01
Data Collection and Processing with Python - Week 2 - 23.2. Map
# 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]