TIL
Web Dev Bootcamp TIL Day-8(Python Basics/ Flask Review/ Git Crash Course)
frannyk
2022. 4. 27. 09:43
Python Basics
map(function, iterable)
- map() function returns a map object(which is an iterator) of the results after applying the given function to each item of a given iterable (list, tuple etc.)
lambda argument: expression
- anonymous function
- lambda arg : expression
- ex) square = lambda x : x **2 ----> stores function inside square()
filter(function, iterable)
- return values from the iterable that are True when evaluated by the function
#Program to filter out only the even items from a list
my_list = [1, 5, 4, 6, 8, 11, 3, 12]
new_list = list(filter(lambda x: (x%2 == 0) , my_list))
print(new_list)
function(*args)
- takes in all arguments
function(**kwargs)
- creates dictionary
----------------------------------------------------------------------------------------------------------------------
Flask Review
#import Flask class from flask module
from flask import Flask
#create an instance of the Flask class
#save the application object in a variable called app
app = Flask(__name__)
- When creating a Flask object, we need to pass in the name of the application. In this case, because we are working with a single module, we can use the special Python variable, __name__.
@app.route('/')
@app.route('/home')
#both http://localhost:5000/ and http://localhost:5000/home
def hello_world():
return <h1>'Hello World!'</h1>
#will return the h1 header Hello World
- route -----> return a response function
We can use triple quotes to contain multi-line code:
@app.route('/')
@app.route('/home')
def home():
return '''
<h1>Hello, World!</h1>
<p>My first paragraph.</p>
CODECADEMY
'''
We can use an anchor tag with href = ... route name to create a truncated hyperlink
@app.route('/reporter')
def reporter():
return '''
<h2>Reporter Bio</h2>
#anchor tag that creates a link back to homepage
<a href="/">Return to home page</a>
'''
Dynamic URLS
@app.route('/orders/<user_name>/<int:order_id>')
def orders(user_name, order_id):
return f'<p>Fetching order #{order_id} for {user_name}.</p>'
@app.route('/article/<article_name>')
def article(article_name):
return f'''
<h2>{article_name.replace('-', ' ').title()}</h2>
<a href='/'>Return back to home page</a>
'''