<jupyter_start><jupyter_text>Get Familiar with Python  Functions<jupyter_code># function
def get_weather_info():
    return 'Weather is good today!'

def get_weather_info():
    a = 2 + 2
#     print(a)
    return
get_weather_info()
def update_model(model, *args, **kwargs):
    print(model, args, kwargs)
update_model('my model')
update_model('my model', 9, 'a', False, sep=',', header=[])
def update_model2(model, *a, **b):
    print(model, a, b)
update_model2('my model', 9, 'a', False, sep=',', header=[])
my_lambda_function = lambda x, y, z: round((x / y) ** z, 2)
my_lambda_function
update_model2
my_lambda_function(1,2,3)
(lambda: print('yey'))()<jupyter_output>yey<jupyter_text>Classes<jupyter_code>class Person:
    kind = 'Human' # class attributes
    
    def __init__(self, firstname, lastname, birth_year): # the word `self` means nothing. represents an instance of the class.
        self.birth_year = birth_year # instance attribute
        self.firstname = firstname
        self.lastname = lastname

    def printname(self):
        print(self.firstname, self.lastname)
        
    def get_age(self):
        return 2021 - self.birth_year
Person.kind
# will fail without instantiation
Person.birth_year
# instantiation will make instance attributes available
# to instance(copy) of this class
p = Person('Metin', 'Senturk', 1989)
p2 = Person('Kusum', 'Singh', 1995)
p.birth_year, p.firstname, p.lastname
p2.birth_year, p2.firstname, p2.lastname
p.printname(), p2.printname()
p.ssn = '123121234'
p.ssn
p.get_age(), p2.get_age()
def get_age2(year):
    return 2021 - year
p.get_age2 = get_age2
p.get_age2(p.birth_year)<jupyter_output><empty_output><jupyter_text>Generators<jupyter_code>%time sum(range(100000000))
my_long_list = list(range(100000000))
# 1. Create the definition of generator function
def get_index():
    a = 1
    yield a # once called, it stops its execution at this point
    print('after yield', a)
#     return 1
# 2. Create an instance from generator defintion
index_gen = get_index()
# calling with next will fail if generator has no more values
# next(index_gen)
# for loop will understand and not fail if gen has no values
for idx in index_gen:
    print(idx)
def get_index2():
    a = 1
    yield a 
    a += 1
    print('after yield', a)
index_gen = get_index2()
for idx in index_gen:
    print(idx)
def get_index3(stop):
    a = 1
    while True:
        if a == stop:
            return a
        yield a 
        a += 1
        
    print('after yield', a)
index_gen = get_index3(10)
for idx in index_gen:
    print(idx)<jupyter_output>1
2
3
4
5
6
7
8
9<jupyter_text>Modules and Packages- Can import a standard module (comes with python)- A third party package (you install with pip)- A local file on your computer<jupyter_code>import pathlib
pathlib
from module1 import greeting
greeting('Ramoji')
from module1 import PERSON1, FolderOperations
my_folder = FolderOperations('My Local Folder')
my_folder
if not my_folder.exists():
    my_folder.create()
my_folder.add_file()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11078334972140282, 'fraction_numerical': 0.029498525073746312, 'mean_word_length': 4.103678929765886, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '503444', 'license': 'MIT', 'n_tokens_mistral': 1107, 'n_tokens_neox': 1045, 'n_words': 314, 'path': 'contents/python-warmup/Python Intro.ipynb', 'repo_name': 'spu-dataviz-211/class-materials', 'size': '3051'}