0
0
Redisquery~30 mins

Redis with Python (redis-py) - Mini Project: Build & Apply

Choose your learning style9 modes available
Simple Redis Key-Value Store with Python
📖 Scenario: You are building a simple key-value store using Redis and Python. This store will hold user information such as usernames and their favorite colors. Redis is a fast, in-memory database often used for caching and quick data retrieval.
🎯 Goal: Build a Python script that connects to Redis, stores user data as key-value pairs, retrieves the data, and finally deletes the keys to clean up.
📋 What You'll Learn
Create a Redis connection using redis-py
Store multiple user favorite colors as key-value pairs
Retrieve and print the stored values
Delete the keys from Redis
💡 Why This Matters
🌍 Real World
Redis is widely used for caching user preferences and session data in web applications for fast access.
💼 Career
Understanding how to use Redis with Python is valuable for backend developers working on scalable, high-performance applications.
Progress0 / 4 steps
1
Connect to Redis and set up initial data
Import the redis module and create a Redis client called r that connects to localhost on the default port. Then, create a dictionary called user_colors with these exact entries: 'alice': 'blue', 'bob': 'green', 'carol': 'red'.
Redis
Need a hint?

Use redis.Redis() to connect. The dictionary keys and values must match exactly.

2
Store user colors in Redis
Use a for loop with variables user and color to iterate over user_colors.items(). Inside the loop, use r.set(user, color) to store each user's favorite color in Redis.
Redis
Need a hint?

Use for user, color in user_colors.items(): and inside the loop call r.set(user, color).

3
Retrieve and decode user colors from Redis
Create an empty dictionary called retrieved_colors. Use a for loop with variable user to iterate over user_colors.keys(). Inside the loop, get the color from Redis using r.get(user), decode it to a string with .decode('utf-8'), and store it in retrieved_colors[user].
Redis
Need a hint?

Remember Redis returns bytes, so decode with .decode('utf-8').

4
Delete the user keys from Redis
Use a for loop with variable user to iterate over user_colors.keys(). Inside the loop, delete each key from Redis using r.delete(user).
Redis
Need a hint?

Use r.delete(user) inside the loop to remove keys.