You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importglobimportpandasaspd# list stating the differences in the csvs to be importedcsv_list= ['northern', 'southern', 'eastern', 'western']
# initalizing an empty list to store the df's createddf_list= []
# looping over each list_element to create 'file_name'forlist_elementincsv_list:
file_name=f'../Path/To/File/{list_element}US_popular_diners'# using glob to wildcard search the appropriate csv file; file type added at end of stringforfileinglob.glob(file_name+'*.csv'):
# saving each df generated to 'df_list'df_list.append(pd.read_csv(file, index_col=None))
# concatinating the stored dfs together, row-wise or union-stylecomplete_df=pd.concat(df_list, axis=0)
Iterators
Range
# increments are specified using STEPnum_range=range(0, 16, 2)
# converts RANGE OBJECT to a listnums_list=list(num_range)
print(nums_list)
Enumerate
# indexing starting at START value specifiedenumerate_values=enumerate(['cat', 'dog', 'monkey', 'lemur'], start=10)
# converts the ENUMERATE OBJECT to a listindexed_list=list(enumerate_values)
print(indexed_list)
Map
# MAP can be passed an aggregration to be applied to the second parameter, list of digitsrounded_values=map(round, [12.3, 14.4, 17.8])
# converts the MAP object to a listrounded_list=list(rounded_values)
print(f'Rounded Values: {rounded_list}\n')
# MAP can be passed a parsing or string modifier to be applied to the second parameter, list of stringstitled_values=map(str.title, ['denver', 'longbeach', 'hobbs'])
# converts the MAP object to a listtitled_list=list(titled_values)
print(f'Titled Values: {titled_list}\n')
# MAP can be used with lambda to pass anonymous functions without loopingsquared_values=map(lambdax: x**2, [6, 8, 9, 12, 14])
# converts the MAP object to a listsquares_list=list(squared_values)
print(f'Squared Values: {squares_list}\n')
Collections Module
Counter
fromcollectionsimportCounterpet_list= ['dog', 'dog', 'cat', 'parrot', 'monkey', 'dog', 'frog', 'cat', 'parrot', 'horse',
'cat', 'dog','hamster', 'horse','snake', 'cat', 'parrot', 'frog', 'hamster', 'snake']
# collects the COUNT of values in pet_list; stores as COUNTER object saved as counter_dictcounter_dict=Counter(pet_list)
print(f'COUNTER Object (dict)\n{counter_dict}\n')
# capturing top value counts (top 3) and saving LIST OF TUPLES as top_3top_3=counter_dict.most_common(3)
print(f'Top 3 Popular Pets\n{top_3}')
defaultdict
fromcollectionsimportdefaultdictpet_colors= {
'dog': ['golden', 'brindle'],
'cat': ['black', 'calico'],
'horse': ['chocolate', 'spotted']
}
# creating a new dict, with the default value of an empty listnew_dict=defaultdict(list)
# iterating through pet_colors dict, storing k:v pairs to the DEFAULTDICT named new_dict forkey, value_listinpet_colors.items():
new_dict[key].append(value_list)
# the DEFAULTDICT named new_dict does not have frog key, but by default returns an empty dictprint(f'Frog value does not exist yet: {new_dict["frog"]}\n')
# frog key now exists in new_dict, with an empty list as it's value by defaultprint(f'Frog value defaulted to empty list due to DEFAULTDICT\n{new_dict}')
namedtuple
fromcollectionsimportnamedtuple# list of tuples with bbq joint information, stored as bbq_tuplesbbq_tuples= [
('Jack Jordans', 'Odessa', 'smoked sausage', 12),
('Russels', 'Denver', 'brisket burnt ends', 25),
('Franklins', 'Austin', 'chopped sandwich', 15)
]
# creating the BARBEQUE nametuple and passing list of attribute namesBarbeque=namedtuple('Barbeque', ['name', 'location', 'special', 'special_price'])
# initializing a list to hold the list of namedtuplesbbq_joints= []
# looping over bbq_tuples, identifying each attributeforjoint, location, special, special_priceinbbq_tuples:
# creating NAMEDTUPLE object as details, appending to bbq_joints listdetails=Barbeque(joint, location, special, special_price)
bbq_joints.append(details)
# diplays the Barbeque nametuplesbbq_joints
# nametuples' attributes are now easily accessibleforjointinbbq_joints:
print(f'{joint.name} BBQ in {joint.location} has a great {joint.special} special!')
Jack Jordans BBQ in Odessa has a great smoked sausage special!
Russels BBQ in Denver has a great brisket burnt ends special!
Franklins BBQ in Austin has a great chopped sandwich special!
Itertools Module
Combinations
fromitertoolsimportcombinationscolors= ['red', 'blue', 'green']
# '2' indicates NUM OF COMBINATIONS, '*' unpacks the COMBO OBJECT to a listcolor_mixes= [ *combinations(colors, 2) ]
color_mixes
tx_favorites= ['steak', 'brisket', 'burritos', 'chili', 'bbq', 'hatch chilies', 'queso', 'mediterranean', 'wings']
co_favorites= ['steak', 'green chilies', 'chili', 'sushi', 'pizza', 'mediterranean', 'wings']
# converting lists into SET objects which only store unique values (no duplicates)tx_set=set(tx_favorites)
co_set=set(co_favorites)
intersection
# INTERSECTION collects values shared between the two setsboth_sets=tx_set.intersection(co_set)
print(both_sets)
{'wings', 'steak', 'mediterranean', 'chili'}
difference
#DIFFERENCE collects values only in the set specifiedtx_set_only=tx_set.difference(co_set)
co_set_only=co_set.difference(tx_set)
print(f'Only a Texas favorite\n{tx_set_only}\n')
print(f'Only a Colorado favorite\n{co_set_only}')
Only a Texas favorite
{'bbq', 'burritos', 'queso', 'hatch chilies', 'brisket'}
Only a Colorado favorite
{'green chilies', 'sushi', 'pizza'}
symmetric difference
# SYMMETRIC_DIFFERENCE collects values that exist in ONE of the sets, but NOT BOTHdifferences=tx_set.symmetric_difference(co_set)
print(f'Either Texas/Colorado Favorite, BUT NOT BOTH\n{differences}\n')
Either Texas/Colorado Favorite, BUT NOT BOTH
{'green chilies', 'bbq', 'pizza', 'burritos', 'queso', 'hatch chilies', 'brisket', 'sushi'}
union
# UNION collects all values from each set (no duplicates)union_set=tx_set.union(co_set)
print(f'All Favorites (both states), NO DUPLICATES\n{union_set}\n')
All Favorites (both states), NO DUPLICATES
{'wings', 'green chilies', 'bbq', 'burritos', 'queso', 'chili', 'hatch chilies', 'pizza', 'steak', 'brisket', 'mediterranean', 'sushi'}
set modifiers
# add a single value to the set only if the value is uniquetx_set.add('burger')
# merges a list of values into the set, only if the values are uniqueco_set.update(['buffalo burger', 'breakfast skillet', 'salads'])
# removes an element from the setco_set.discard('salads')
print(tx_set)
print(co_set)
# determing 15% discount for each item in price list w/list comprehensiondiscounts= [price* (0.85) forpriceinprice_list]
# formatting each price into $$.$$ format w/list comprehensiondiscounts= [round(discount, 2) fordiscountindiscounts]
print(f'Price List Discounts:\n{discounts}')
Price List Discounts:
[13.59, 6.76, 29.32, 17.17, 9.85]
# filters out elements not meeting conditional clausecheap= [priceforpriceindiscountsifprice>=10]
print(f'Cheapest Prices:\n{cheap}')
Cheapest Prices:
[13.59, 29.32, 17.17]
# if/else providing string values cooresponding if conditional is met price_levels= ['expensive'ifprice>10else'cheap'forpriceindiscounts]
# assume $10 gift card can be applied to only $15 and more...after_gc= [price-10ifprice>=15elsepriceforpriceindiscounts]
print(f'Prices after GiftCard applied:\n{after_gc}')
Prices after GiftCard applied:
[13.59, 6.76, 19.32, 7.170000000000002, 9.85]
Dictionaries
Command
Behavior
dict[new_key] = new_value
updates the dict with a new k:v pair denoted by passed key and value
dict.update(second_dict)
updates the dict with a passed dictionary, tuple(s), etc.
dict.pop(value)
safely removes the value from the dict if it exists
dict.get(value, 'NA')
safely retrieves the value from the dict if it exists, else it returns secondary value (default None)
key in dict
boolean statement for if key exists in specified dict; commonly used in conditional statements when parsing dicts
dictionary
# creates a dictionary of key:value pairsmeal_dict= {
'breakfast': 'scrambled eggs',
'lunch': 'sandwich',
'dinner': 'steak'
}
# dictionary is accessed by providing the key in bracketslunch=meal_dict['lunch']
print(f'Lunch Meal: {lunch}')
Lunch Meal: sandwich
List of dictionaries
# creates a list of dictionaries of key:value pairslist_of_dicts= [
{'breakfast': 'scrambled eggs'},
{'lunch': 'sandwich'},
{'dinner': 'steak'}
]
# dictionary is accessed by providing index of dict in list, then providing the key for that dictlunch=list_of_dicts[1]['lunch']
print(f'Lunch Meal: {lunch}')
Lunch Meal: sandwich
snack= {'snack': 'cheezits'}
# appends adds the snack dict the list_of_dictslist_of_dicts.append(snack)
print(list_of_dicts)
sport_dict= {
'basketball': ['lakers', 'spurs', 'nets'],
'football': ['cowboys', 'chiefs', 'seahawks'],
}
# conditional to determine if element is inside of football list in sport_dictif'cowboys'insport_dict['football']:
print('The team is inside the dictionary!')
The team is inside the dictionary!
# loops through sport_dict to display the sport keysprint('List of Sports: (keys representing each sport)')
forsportinsport_dict.keys():
print(sport)
List of Sports: (keys representing each sport)
basketball
football
# loops through sport_dict to display the list of teamsprint('\nList of Teams: (values for each sport key)')
forteamsinsport_dict.values():
print(teams)
List of Teams: (values for each sport key)
['lakers', 'spurs', 'nets']
['cowboys', 'chiefs', 'seahawks']
# loops through sport_dict to display the sport keys and corresponding team valuesforsport, teamsinsport_dict.items():
print(f'\n{sport} Teams: \n{teams}')
basketball Teams:
['lakers', 'spurs', 'nets']
football Teams:
['cowboys', 'chiefs', 'seahawks']
Functions
docstring example
deffunction(arg_1, arg_2=42):
"""Description of what the function does. Google Docstring Style Args: arg_1 (str): Description of arg_1 that can break onto the next line if needed. arg_2 (int, optional): Write optional when an argument has a default value. Returns: bool: Optional description of the return value Extra lines are not indented. Raises: ValueError: Include any error types that the function intentionally raises. Notes: See https://www.datacamp.com/community/tutorials/docstrings-python for more info. """
unpacking arguments
defmultiply(*args):
""" Using * lets the function know to unpack the arguments; allows for any number of arguments to be passed into the function. """print(args)
total=1forarginargs:
# multiplies the unpacked arguementstotal=total*argreturntotal# runs the multiply function with several sets of valuesprint(multiply(1,3,5), '\n')
print(multiply(-1), '\n')
print(multiply(12,3,44,-4))
(1, 3, 5)
15
(-1,)
-1
(12, 3, 44, -4)
-6336
destructuring arguments into parameters
defadd(x,y):
""" Using * when calling the add function breaks down the parameter into the x,y variables; requires the amount of desctructured parameters and function variables are the same. """returnx+y# using '*' allows the list passed to be deconstructednums= [3,5]
print(add(*nums), '\n')
# using '**' allows the dictionary passed to be deconstructednums_dict= {'x':10, 'y':10}
print(add(**nums_dict))
8
20
keyword arguments
defnamed(**kwargs):
""" Specifying **kwargs will process any number of passed keyword parameters inside dict """print(kwargs,'\n')
named(name='bob', age=25)
{'name': 'bob', 'age': 25}
defprint_nicely(**kwargs):
""" Passing the kwargs and iterating over any number of parameters """named(**kwargs)
forarg, valueinkwargs.items():
print(f'{arg}: {value}')
# dict containing kw arguments to be passed into print_nicely functiondetails= {'name':'bob', 'age':25}
print_nicely(**details)
{'name': 'bob', 'age': 25}
name: bob
age: 25
defboth(*args, **kwargs):
""" this setup is typically used so all arguments can be passed into another function """print(args, '\n')
print(kwargs)
both(1,2,3,12, name='bob', age=25)
(1, 2, 3, 12)
{'name': 'bob', 'age': 25}
Classes (Object Oriented Programming)
classStudent:
# init will run when class is calleddef__init__(self, name, grades):
self.name=nameself.grades=grades# function inside of class is called a 'method'defaverage(self):
''' average function uses the created student (aka 'self') '''returnsum(self.grades) /len(self.grades)
# calling the Student class, providing parameters for the classstudent=Student('Anakin', (80, 82, 85, 78, 92))
print(student.name)
print(student.grades)
Anakin
(80, 82, 85, 78, 92)
# calling Student class with new passed parameters, saved as student2student2=Student('Obi Wan', (90, 90, 93, 78, 90))
print(student2.name)
# average is a method in the class and requires ()print(student2.average())
Obi Wan
88.2
magic methods
classPerson:
def__init__(self, name, age):
self.name=nameself.age=age# magic method called automatically when turning the object into a stringdef__str__(self):
returnf'Person: {self.name}, {self.age} years old.\n'# magic method designed to return a string that can reconstruct the original objectdef__repr__(self):
returnf'<Person({self.name}, {self.age})>'vader=Person('Anakin', 35)
# displays a string representation of the object if __str__ not defined, else return defined __str__print(vader)
# prints the unambiguous representation of the objectprint(vader.__repr__())
Person: 'Anakin', 35 years old.
<Person(Anakin, 35)>
@classmethod and @staticmethod
classClassTest:
# instance methods are methods that use the class object selfdefinstance_method(self):
print(f'Called instance_method of {self}')
# classmethod are methods that use the cls object, or the Class object itself@classmethoddefclass_method(cls):
print(f'Called class_method of {cls}')
# static methods are methods that do not have passed parameters self/cls; @staticmethoddefstatic_method():
print('Called static_method.')
# saving instance of ClassTest and displaying instance methodtest=ClassTest()
test.instance_method()
# used for actions, uses data inside the object created, either manipulating or modifying
Called instance_method of <__main__.ClassTest object at 0x7fcb510245b0>
# python will know to pass ClassTest due to @classmethod decoratorClassTest.class_method()
# often used to create 'factories'
Called class_method of <class '__main__.ClassTest'>
# essentially a function in the class does not use any parametersClassTest.static_method()
# used to place a method inside a class, belongs with the class logically
Called static_method.
classBook:
# class variables that make sense to go with this classTYPES= ('hardcover', 'paperback')
def__init__(self, name, book_type, weight):
self.name=nameself.book_type=book_typeself.weight=weightdef__str__(self):
returnf'Book: {self.name}, {self.book_type}...weighing {self.weight}g.'def__repr__(self):
returnf'<Book({self.name}, {self.book_type}, {self.weight})>'@classmethoddefhardcover(cls, name, page_weight):
# creates new Book instance, using first type (hardcover), increasing page weight by 100returncls(name, cls.TYPES[0], page_weight+100)
@classmethoddefpaperback(cls, name, page_weight):
# creates new Book instance, using second type (paperback), keeping original page weightreturncls(name, cls.TYPES[1], page_weight)
print(Book.TYPES)
('hardcover', 'paperback')
# creating Book instance as bookbook=Book('Harry Potter', 'hardcover', 1500)
# displaying the defined __str__ method and __repr__ method stringsprint(book)
print(book.__repr__())
# when class instance is called by itself, the __repr__ method defined string will be displayedbook
# using the classmethod hardcover, creating hardcover instance of HP hc_book=Book.hardcover('Harry Potter', 1500)
print(hc_book)
Book: Harry Potter, hardcover...weighing 1600g.
# using the classmethod paperback, creating paperback instance of HPpb_book=Book.paperback('Harry Potter', 1500)
print(pb_book)
Book: Harry Potter, paperback...weighing 1500g.
Pandas
importpandasaspd# defines a dfdf=pd.DataFrame({'num_legs': [2, 4, 4, 6], 'num_wings': [2, 0, 0, 0]},
index=['falcon', 'dog', 'cat', 'ant'])
# stores the value counts of the values in num_legs columnseries_frequency=df['num_legs'].value_counts()
# looping over series_frequency, providing a way to display value counts in reports w/ addition infoforvalue, countinseries_frequency.items():
print(f'There are {count} animal(s) with {value} legs')
There are 2 animal(s) with 4 legs
There are 1 animal(s) with 6 legs
There are 1 animal(s) with 2 legs
Inheritance
classDevice:
def__init__(self, name, connected_by):
self.name=nameself.connected_by=connected_byself.connected=Truedef__str__(self):
# !r is a shorthand way to include quotes around valuereturnf'Device {self.name!r} ({self.connected_by})'defdisconnect(self):
self.connected=Falseprint('Disconnected.')
# inheritance is assigned by passing the class as a parameterclassPrinter(Device):
def__init__(self, name, connected_by, capacity):
# super() calls the init class of the parent class, Devicesuper().__init__(name, connected_by)
self.capacity=capacityself.remaining_pages=capacitydef__str__(self):
returnf'{super().__str__()} ({self.remaining_pages} pages remaining)'defprint(self, pages):
'''Prints out the # of pages passed, subtracts pages from remaining_pages value.'''ifnotself.connected:
print('Your printer is not connected!')
returnprint(f'Printing {pages} pages.\n')
self.remaining_pages-=pages
# creating instance of PRINTER class, displaying __str__ valueprinter=Printer("Printer", "USB", 500)
print(printer)
# running print function in Printer class, again displaying the modified __str__ value printer.print(20)
print(printer)
# calling the disconnect function from Device, testing if not self.connected clauseprinter.disconnect()
printer.print(30)
Device 'Printer' (USB) (500 pages remaining)
Printing 20 pages.
Device 'Printer' (USB) (480 pages remaining)
Disconnected.
Your printer is not connected!
Composition - More common than Inheritance
classBookShelf:
# *books expects to have any number of Book objects passeddef__init__(self, *books):
self.books=booksdef__str__(self):
returnf'BookShelf with {len(self.books)} books.'classBook:
def__init__(self, name):
self.name=namedef__str__(self):
returnf'Book {self.name}'
# creating two instances of Book Classbook=Book('Harry Potter')
book2=Book('The Dark Tower')
# with inheritance, the book objects were passed to define the BookShelfshelf=BookShelf(book, book2)
print(shelf)
importfunctoolsuser= {'username': 'chris', 'access_level': 'admin'}
defmake_secure(func):
# the functools.wraps tells secure_fuction it is a wrapper for get_admin_password, and prevents# get_admin_password function name/documentation from being replaced w/secure_function name/documentation@functools.wraps(func)defsecure_function():
ifuser['access_level'] =='admin':
returnfunc()
else:
returnf"No admin permissions for {user['username']}."returnsecure_function# this @make_secure decorator call will create the function and then pass it through make_secure@make_securedefget_admin_password():
return'1234'# w/out the @functools.wraps(func) call, this print would display 'secure_function', not 'get_admin_password'print(get_admin_password.__name__)
print(get_admin_password())
get_admin_password
1234
decorating functions w/parameters
importfunctoolsuser= {'username': 'chris', 'access_level': 'billing'}
defmake_secure(func):
@functools.wraps(func)# by providing (*args, **kwargs) the function can take any number of parameters from any function passed to decoratordefsecure_function(*args, **kwargs):
# if access_level == 'admin' or 'billing', then returns the func(get_password) with parameters if (user['access_level'] =='admin') | (user['access_level'] =='billing'):
returnfunc(*args, **kwargs)
else:
returnf"No admin permissions for {user['username']}."returnsecure_function@make_securedefget_password(panel):
# after running through make_secure, appropriate password is returnedifpanel=='admin':
return'1234'elifpanel=='billing':
return'super_secure_password'get_password('billing')
'super_secure_password'
decorators w/parameters
importfunctools# make_secure has now access_level parameter passed into functiondefmake_secure(access_level):
# decorator function does the access level testing; if an access level is passed that @make_secure decorator# has as a parameter, continue w/passed function, ELSE print string information.defdecorator(func):
@functools.wraps(func)defsecure_function(*args, **kwargs):
ifuser['access_level'] ==access_level:
returnfunc(*args, **kwargs)
else:
returnf"No {access_level} permissions for {user['username']}."# secure function is returned by decorator when runreturnsecure_function# decorator function is returned by make_secure when runreturndecorator# including 'admin' parameter in @make_secure decorator@make_secure('admin')defget_admin_password():
return'admin: 1234'# including 'user' parameter in @make_secure decorator@make_secure('user')defget_dashboard_password():
return'user: user_password'