Build Your Own AI Assistant Part 1 - Creating the Assistant
115007 Views
Is the new Raspberry Pi AI Kit better than Google Coral?
103855 Views
Control Arduino with Python using Firmata / PyFirmata
86426 Views
How to Map with LiDAR - using a Raspberry Pi Zero 2W, RPLidar and Rviz
55270 Views
Node-Red Automation, MQTT, NodeMCU & MicroPython
51306 Views
Creating a Supercomputer with a Raspberry Pi 5 Cluster and Docker Swarm!
50568 Views
Installing and Using DeepSeek-R1:1.5 on a Raspberry Pi with Docker
Gamepad & BurgerBot
Level Up your CAD Skills
Operation Pico
Raspberry Pi Home Hub
Hacky Temperature and Humidity Sensor
Using the Raspberry Pi Pico's Built-in Temperature Sensor
0h 24m
Getting Started with SQL
0h 32m
Introduction to the Linux Command Line on Raspberry Pi OS
0h 42m
How to install MicroPython
0h 8m
Wall Drawing Robot Tutorial
0h 22m
BrachioGraph Tutorial
0h 16m
Learn Linux from the basics to advanced topics.
Learn how to use a Raspberry Pi Pico
Learn MicroPython the best language for MicroControllers
Learn Docker, the leading containerization platform. Docker is used to build, ship, and run applications in a consistent and reliable manner, making it a popular choice for DevOps and cloud-native development.
Learn how to build SMARS robots, starting with the 3D Printing the model, Designing SMARS and Programming SMARS
Learn how to build robots, starting with the basics, then move on to learning Python and MicroPython for microcontrollers, finally learn how to make things with Fusion 360.
Learn Python, the most popular programming language in the world. Python is used in many different areas, including Web Development, Data Science, Machine Learning, Robotics and more.
Learn how to create robots in 3D, using Fusion 360 and FreeCAD. The models can be printed out using a 3d printer and then assembled into a physical robot.
Learn how to create Databases in Python, with SQLite3 and Redis.
KevsRobots Learning Platform
30% Percent Complete
By Kevin McAleer, 4 Minutes
In this lesson, we will cover Redis data types and how to use them in Python. Redis supports several data types, including strings, hashes, lists, sets, and sorted sets. We will cover how to create and manipulate each data type in Redis, and how to interact with them using the Redis Python library.
In this lesson, we will cover:
Redis supports several data types, each with its own set of commands and operations. Here are the main data types in Redis:
The string data type is the most basic data type in Redis. It is used to store text or binary data. Here are some examples of string operations in Redis:
import redis redis_client = redis.Redis(host='localhost', port=6379) # Set a string value redis_client.set('mykey', 'myvalue') # Get a string value value = redis_client.get('mykey') # Append a string to an existing value redis_client.append('mykey', 'appended value')
The hash data type is used to store key-value pairs, where the keys and values are both strings. Here are some examples of hash operations in Redis:
import redis redis_client = redis.Redis(host='localhost', port=6379) # Set a hash value redis_client.hset('myhash', 'key1', 'value1') # Get a hash value by key value = redis_client.hget('myhash', 'key1') # Get all hash values all_values = redis_client.hgetall('myhash')
The list data type is used to store a collection of ordered elements. Here are some examples of list operations in Redis:
import redis redis_client = redis.Redis(host='localhost', port=6379) # Add an element to a list redis_client.rpush('mylist', 'element1') # Get an element by index element = redis_client.lindex('mylist', 0) # Get all elements in a list all_elements = redis_client.lrange('mylist', 0, -1)
The set data type is used to store a collection of unordered elements with no duplicates. Here are some examples of set operations in Redis:
import redis redis_client = redis.Redis(host='localhost', port=6379) # Add an element to a set redis_client.sadd('myset', 'element1') # Check if an element is already in a set element_exists = redis_client.sismember('myset', 'element1')
all_elements = redis_client.smembers('myset')
The sorted set data type is used to store a collection of ordered elements with scores, where each element has a unique score. Here are some examples of sorted set operations in Redis:
import redis redis_client = redis.Redis(host='localhost', port=6379) # Add an element to a sorted set redis_client.zadd('mysortedset', {'element1': 1}) # Get an element by rank element = redis_client.zrange('mysortedset', 0, 0) # Get all elements in a sorted set with scores all_elements = redis_client.zrange('mysortedset', 0, -1, withscores=True)
To use Redis data types in Python, you can use the corresponding methods of the Redis Python library. Hereโs an example of how to use a Redis hash in Python:
import redis redis_client = redis.Redis(host='localhost', port=6379) # Set a hash value redis_client.hset('myhash', 'key1', 'value1') # Get a hash value by key value = redis_client.hget('myhash', 'key1')
After completing this lesson, you should be able to:
Describe the main Redis data types and their use cases Perform basic operations on Redis data types using the Redis Python library
In this lesson, we covered Redis data types and how to use them in Python. We described the main data types in Redis, including strings, hashes, lists, sets, and sorted sets, and demonstrated how to perform basic operations on each data type using the Redis Python library. In the next lesson, we will cover Redis commands and transactions.
< Previous Next >