Challenge - 5 Problems
Redis-py Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this Redis-py command?
Given a Redis instance with a key 'counter' set to '10', what will be the output of the following Python code?
import redis
r = redis.Redis()
result = r.incr('counter')
print(result)Redis
import redis
r = redis.Redis()
result = r.incr('counter')
print(result)Attempts:
2 left
💡 Hint
The INCR command increases the integer value stored at key by one and returns the new value.
✗ Incorrect
The INCR command increments the value stored at 'counter' by 1. Since it was 10, the new value is 11, which is returned and printed.
📝 Syntax
intermediate2:00remaining
Which option correctly sets a key with expiration using redis-py?
You want to set a key 'session' with value 'abc123' that expires after 60 seconds. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Check the redis-py documentation for the parameter name to set expiration in seconds.
✗ Incorrect
The correct parameter to set expiration time in seconds in redis-py set() method is 'ex'. Other options are invalid and cause errors.
❓ optimization
advanced2:00remaining
How to efficiently retrieve multiple keys in redis-py?
You need to get the values of keys 'key1', 'key2', and 'key3' from Redis. Which approach is the most efficient?
Attempts:
2 left
💡 Hint
Look for a redis-py command that fetches multiple keys in one call.
✗ Incorrect
The mget() method fetches multiple keys in a single call, reducing network overhead. Option C uses pipeline but is more complex and less direct.
🔧 Debug
advanced2:00remaining
Why does this redis-py code raise a TypeError?
Consider this code snippet:
Why does it raise a TypeError?
r.set('count', 5)
value = r.incr('count')
print(value)Why does it raise a TypeError?
Redis
r.set('count', 5) value = r.incr('count') print(value)
Attempts:
2 left
💡 Hint
Check the data type of the value passed to r.set and what redis-py expects.
✗ Incorrect
r.set expects the value to be bytes or string. Passing an integer directly causes a TypeError. You should convert the integer to string or bytes.
🧠 Conceptual
expert2:00remaining
What happens if you call r.get() on a non-existent key in redis-py?
If you run
r.get('missing_key') on a Redis instance where 'missing_key' does not exist, what is the result?Redis
r.get('missing_key')Attempts:
2 left
💡 Hint
Think about how redis-py handles keys that are not found.
✗ Incorrect
When a key does not exist, r.get returns None, indicating absence of value without error.