Bird
Raised Fist0
Redisquery~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the set() method do in redis-py?
easy
A. Lists all keys stored in Redis
B. Retrieves a value by key from Redis
C. Deletes a key from Redis
D. Stores a value with a specified key in Redis

Solution

  1. Step 1: Understand the purpose of set()

    The set() method is used to save data in Redis by associating a value with a key.
  2. Step 2: Compare with other methods

    get() retrieves data, delete() removes keys, and keys() lists keys, so they do not store data.
  3. Final Answer:

    Stores a value with a specified key in Redis -> Option D
  4. Quick Check:

    set() means store data [OK]
Hint: Remember: set = save data, get = retrieve data [OK]
Common Mistakes:
  • Confusing set() with get()
  • Thinking set() deletes keys
  • Assuming set() lists keys
2. Which of the following is the correct syntax to get a value for key 'name' using redis-py?
easy
A. r.get('name')
B. r.get(name)
C. r.get['name']
D. r.get{name}

Solution

  1. Step 1: Identify correct method call syntax

    In Python, method calls use parentheses with arguments inside, so r.get('name') is correct.
  2. Step 2: Check argument format

    The key must be a string, so it needs quotes. Options A, B, and D have syntax errors or missing quotes.
  3. Final Answer:

    r.get('name') -> Option A
  4. Quick Check:

    Method call with parentheses and string key [OK]
Hint: Use parentheses and quotes for string keys in method calls [OK]
Common Mistakes:
  • Omitting quotes around string keys
  • Using square brackets instead of parentheses
  • Using curly braces instead of parentheses
3. What will be the output of this code snippet?
import redis
r = redis.Redis()
r.set('color', 'blue')
value = r.get('color')
print(value)
medium
A. blue
B. b'blue'
C. None
D. Error

Solution

  1. Step 1: Understand Redis returns bytes

    The get() method returns the value as bytes, so value is b'blue'.
  2. Step 2: Print output

    Printing value shows the bytes literal b'blue', not the decoded string.
  3. Final Answer:

    b'blue' -> Option B
  4. Quick Check:

    Redis returns bytes, so output shows b'...' [OK]
Hint: Remember: Redis get returns bytes, not decoded string [OK]
Common Mistakes:
  • Expecting a plain string without decoding
  • Assuming None if key exists
  • Thinking code raises error
4. Identify the error in this code snippet:
import redis
r = redis.Redis()
r.set('age', 30)
age = r.get('age')
print(age)
medium
A. get() returns bytes, need decoding
B. Cannot store integer directly with set()
C. Missing Redis server connection parameters
D. Syntax error in print statement

Solution

  1. Step 1: Check data storage

    Redis-py converts integers to strings automatically, so storing 30 is valid.
  2. Step 2: Check retrieval and printing

    get() returns bytes, so printing age shows bytes, not a readable string or integer. Decoding is needed.
  3. Final Answer:

    get() returns bytes, need decoding -> Option A
  4. Quick Check:

    Decode bytes after get() to read value [OK]
Hint: Always decode bytes from get() before printing [OK]
Common Mistakes:
  • Thinking set() rejects integers
  • Ignoring bytes return type from get()
  • Assuming print syntax error
5. You want to store user info as a dictionary in Redis using redis-py. Which approach correctly saves and retrieves the dictionary?
hard
A. Store dictionary as string with set() and retrieve with get() without decoding
B. Directly use set() with the dictionary and get() to retrieve
C. Use set() with json.dumps() to save and json.loads() after get()
D. Use hmset() to save and hgetall() to retrieve

Solution

  1. Step 1: Understand Redis stores strings/bytes

    Redis cannot store Python dicts directly; they must be converted to strings.
  2. Step 2: Use JSON to serialize and deserialize

    Using json.dumps() converts dict to string for set(), and json.loads() converts back after get().
  3. Step 3: Evaluate other options

    hmset() is deprecated; direct dict storage fails; storing string without decoding causes errors.
  4. Final Answer:

    Use set() with json.dumps() and json.loads() after get() -> Option C
  5. Quick Check:

    Serialize dict to string before storing, deserialize after retrieval [OK]
Hint: Serialize dict with JSON before set(), deserialize after get() [OK]
Common Mistakes:
  • Trying to store dict directly
  • Using deprecated hmset() method
  • Not decoding bytes before json.loads()
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard