# Python 1. [TL,DR](#tldr) 2. [Dictionaries](#dictionaries) 3. [F-strings](#f-strings) 4. [Web servers](#web-servers) 1. [Flask](#flask) 2. [WSGI server](#wsgi-server) 5. [Further readings](#further-readings) 6. [Sources](#sources) ## TL,DR ```py # Declare a dictionary. {'spam': 2, 'ham': 1, 'eggs': 3} dict(spam=2,ham=1,eggs=3) dict([('spam',2),('ham',1),('eggs',3)]) # String formatting with f-strings. f"Hello, {name}. You are {age}." F"{name.lower()} is funny." # Make elements in a list unique. # Keep the resulting list mutable. unique_list = list(set(redundant_list)) ``` ## Dictionaries ```py # Declare a dictionary. d = {'spam': 2, 'ham': 1, 'eggs': 3} d = dict(spam=2,ham=1,eggs=3) d = dict([('spam',2),('ham',1),('eggs',3)]) d = {x: x for x in range(5)} d = {c.lower(): c + '!' for c in ['SPAM','EGGS','HAM']} d = dict.fromkeys('abc',0) # Change an element. d['ham'] = ['grill', 'bake', 'fry'] # Add a new element. d['brunch'] = 'bacon' # Delete an element. del d['eggs'] d.pop('eggs') # List values and/or keys. d.values() d.keys() d.items() # Print the elements and their values. for k,v in d.items(): print(k,v) # Merge dictionaries. d1 = {'spam': 2, 'ham': 1, 'eggs': 3} d2 = {'toast': 4, 'muffin': 5, 'eggs': 7} d1.update(d2) # Copy dictionaries. d1 = {'spam': 2, 'ham': 1, 'eggs': 3} d2 = d1.copy() ``` ## F-strings ```py f"Hello, {name}. You are {age}." F"{name.lower()} is funny." ``` ## Web servers ### Flask - `request.args` gets query arguments - `request.form` gets POST arguments ```py from flask import request, jsonify @app.route('/get/questions/', methods=['GET', 'POST','DELETE', 'PATCH']) def question(): if request.method == 'GET': start = request.args.get('start', default=0, type=int) limit_url = request.args.get('limit', default=20, type=int) data = [doc for doc in questions] return jsonify( isError = False, message = "Success", statusCode = 200, data= data ), 200 if request.method == 'POST': question = request.form.get('question') topics = request.form.get('topics') return jsonify( isError = True, message = "Conflict", statusCode = 409, data = data ), 409 ``` ### WSGI server You can use `waitress`: ```py from flask import Flask app = Flask(__name__) @app.route("/") def index(): return "