0
0
Redisquery~20 mins

Redis with Python (redis-py) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Redis-py Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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)
A11
B10
CTypeError
DNone
Attempts:
2 left
💡 Hint
The INCR command increases the integer value stored at key by one and returns the new value.
📝 Syntax
intermediate
2: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?
Ar.set('session', 'abc123', ex=60)
Br.set('session', 'abc123', expire=60)
Cr.set('session', 'abc123', timeout=60)
Dr.set('session', 'abc123', ttl=60)
Attempts:
2 left
💡 Hint
Check the redis-py documentation for the parameter name to set expiration in seconds.
optimization
advanced
2: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?
Afor k in ['key1', 'key2', 'key3']: r.get(k)
Br.get('key1'), r.get('key2'), r.get('key3')
Cr.mget(['key1', 'key2', 'key3'])
Dr.pipeline().get('key1').get('key2').get('key3').execute()
Attempts:
2 left
💡 Hint
Look for a redis-py command that fetches multiple keys in one call.
🔧 Debug
advanced
2:00remaining
Why does this redis-py code raise a TypeError?
Consider this code snippet:
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)
ABecause r.incr only works on keys that do not exist
BBecause r.incr cannot increment a key set with a string
CBecause r.set stores the integer as bytes, causing incr to fail
DBecause r.set expects a string value, not an integer
Attempts:
2 left
💡 Hint
Check the data type of the value passed to r.set and what redis-py expects.
🧠 Conceptual
expert
2: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')
ARaises a KeyError
BReturns None
CReturns an empty string ''
DReturns 0
Attempts:
2 left
💡 Hint
Think about how redis-py handles keys that are not found.