diff --git a/day-54-intro-to-flask/hello.py b/day-54-intro-to-flask/hello.py new file mode 100644 index 0000000..89d0576 --- /dev/null +++ b/day-54-intro-to-flask/hello.py @@ -0,0 +1,13 @@ +from flask import Flask + +app = Flask(__name__) + + +@app.route('/') +def hello_world(): + return 'Hello, World!' + + +if __name__ == "__main__": + app.run() + diff --git a/day-54-intro-to-flask/main.py b/day-54-intro-to-flask/main.py new file mode 100644 index 0000000..c8a0f2a --- /dev/null +++ b/day-54-intro-to-flask/main.py @@ -0,0 +1,74 @@ +# ----------- Python Functions as First Class Objects: Passing & Nesting Functions -------------- # + +# Functions can have inputs/functionality/output +def add(n1, n2): + return n1 + n2 + +def subtract(n1, n2): + return n1 - n2 + +def multiply(n1, n2): + return n1 * n2 + +def divide(n1, n2): + return n1 / n2 + +# Functions are first-class objects, can be passed around as arguments e.g. int/string/float etc. + +def calculate(calc_function, n1, n2): + return calc_function(n1, n2) + +result = calculate(add, 2, 3) +print(result) + +# Functions can be nested in other functions + +def outer_function(): + print("I'm outer") + + def nested_function(): + print("I'm inner") + + nested_function() + +outer_function() + +# Functions can be returned from other functions +def outer_function(): + print("I'm outer") + + def nested_function(): + print("I'm inner") + + return nested_function + +inner_function = outer_function() +inner_function + + +# Simple Python Decorator Functions +import time + +def delay_decorator(function): + def wrapper_function(): + time.sleep(2) + #Do something before + function() + function() + #Do something after + return wrapper_function + +@delay_decorator +def say_hello(): + print("Hello") + +# With the @ syntactic sugar +@delay_decorator +def say_bye(): + print("Bye") + +# Without the @ syntactic sugar +def say_greeting(): + print("How are you?") +decorated_function = delay_decorator(say_greeting) +decorated_function()