0
0
FastAPIframework~10 mins

Why query parameters filter data in FastAPI - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a query parameter in FastAPI.

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.get("/items/")
async def read_items(q: [1] = None):
    return {"query": q}
Drag options to blanks, or click blank then click option'
Alist
Bint
Cstr
Dbool
Attempts:
3 left
💡 Hint
Common Mistakes
Using an integer type for a text query parameter.
2fill in blank
medium

Complete the code to filter items by a query parameter.

FastAPI
items = ["apple", "banana", "cherry"]

@app.get("/items/")
async def read_items(q: str = None):
    if q:
        return [item for item in items if [1] in item]
    return items
Drag options to blanks, or click blank then click option'
Aq
Bitems
Citem
Dread_items
Attempts:
3 left
💡 Hint
Common Mistakes
Using the list name instead of the query parameter for filtering.
3fill in blank
hard

Fix the error in the query parameter default value to make it optional.

FastAPI
@app.get("/users/")
async def read_users(name: [1]):
    return {"name": name}
Drag options to blanks, or click blank then click option'
Astr = None
Bstr = ''
Cint = None
Dbool = False
Attempts:
3 left
💡 Hint
Common Mistakes
Using empty string as default makes parameter required with empty value.
4fill in blank
hard

Fill both blanks to filter items by minimum and maximum price query parameters.

FastAPI
items = [{"name": "apple", "price": 5}, {"name": "banana", "price": 3}, {"name": "cherry", "price": 7}]

@app.get("/items/")
async def read_items(min_price: int = None, max_price: int = None):
    filtered = [item for item in items if (min_price is None or item[[1]] >= min_price) and (max_price is None or item[[2]] <= max_price)]
    return filtered
Drag options to blanks, or click blank then click option'
A"price"
B"name"
C"cost"
D"value"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong keys like "name" or "cost" which do not exist.
5fill in blank
hard

Fill all three blanks to create a filtered dictionary of items with names and prices above a threshold.

FastAPI
items = [{"name": "apple", "price": 5}, {"name": "banana", "price": 3}, {"name": "cherry", "price": 7}]

@app.get("/items/")
async def read_items(min_price: int = 4):
    result = {item[[1]]: item[[2]] for item in items if item[[3]] > min_price}
    return result
Drag options to blanks, or click blank then click option'
A"name"
B"price"
D"cost"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong keys like "cost" or mixing keys incorrectly.