📖 Scenario: You are helping a small business decide which Python web framework to use for their new website. They want a simple tool to compare Flask and Django based on their project needs.
🎯 Goal: Build a small Python program that stores key features of Flask and Django, sets a project requirement, and then selects the best framework based on that requirement.
📋 What You'll Learn
Create a dictionary with Flask and Django features
Add a variable for the project requirement
Write logic to choose the framework based on the requirement
Print the chosen framework
💡 Why This Matters
🌍 Real World
Choosing the right web framework is important for building websites efficiently and meeting project goals.
💼 Career
Understanding how to compare tools and make decisions based on project needs is a key skill for developers.
Progress0 / 4 steps
1
Create the frameworks features dictionary
Create a dictionary called frameworks with two keys: 'Flask' and 'Django'. Assign the value 'lightweight' to 'Flask' and 'full-featured' to 'Django'.
Flask
Hint
Use curly braces to create a dictionary with the exact keys and values.
2
Set the project requirement
Create a variable called project_need and set it to the string 'lightweight'.
Flask
Hint
Assign the exact string 'lightweight' to the variable project_need.
3
Choose the framework based on the requirement
Write a for loop using variables name and feature to iterate over frameworks.items(). Inside the loop, use an if statement to check if feature equals project_need. If yes, assign name to a variable called chosen_framework.
Flask
Hint
Use a for loop and an if condition to find the matching framework.
4
Print the chosen framework
Write a line to print the string 'Chosen framework:' followed by the variable chosen_framework.
Flask
Hint
Use the print function with the exact string and variable.
Practice
(1/5)
1. Which framework is best suited for a beginner who wants to learn web development step-by-step with a simple app?
easy
A. Angular
B. Django
C. React
D. Flask
Solution
Step 1: Understand beginner needs
A beginner usually needs a simple, easy-to-understand framework to learn web development basics.
Step 2: Match framework to needs
Flask is lightweight and simple, perfect for learning step-by-step, unlike Django which is more complex.
Final Answer:
Flask -> Option D
Quick Check:
Simple beginner app = Flask [OK]
Hint: Simple and small apps? Pick Flask [OK]
Common Mistakes:
Choosing Django for simple beginner apps
Confusing Flask with frontend frameworks
Thinking React or Angular are backend frameworks
2. Which of the following is the correct way to start a basic Flask app?
easy
A. from flask import Flask
app = Flask()
@app.route('/')
def home():
return 'Hello World!'
C. from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Hello World!'
D. from flask import Flask
app = Flask(__file__)
@app.route('/')
def home():
return 'Hello World!'
Solution
Step 1: Check Flask app initialization
Flask apps require passing __name__ to Flask() for correct setup.
Step 2: Verify route and function syntax
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Hello World!' correctly imports Flask, initializes app with __name__, and defines a route with a function returning a string.
Final Answer:
Correct Flask app start with __name__ -> Option C
Quick Check:
Flask(__name__) is correct app init [OK]
Hint: Flask app needs Flask(__name__) to start [OK]
Common Mistakes:
Using Flask() without __name__
Confusing Django import with Flask
Passing wrong argument like __file__
3. Given this Flask app code, what will be the output when accessing the root URL /?
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Welcome to Flask!'
medium
A. Welcome to Flask!
B. Empty page
C. 500 Internal Server Error
D. 404 Not Found error
Solution
Step 1: Analyze route decorator
The route '/' is defined and linked to the home() function.
Step 2: Check function return value
The home() function returns the string 'Welcome to Flask!'. Flask sends this as the response body.