Understanding Python Set Literals
Many Python programs need to store collections of unique values, such as visited cities, supported file formats, product IDs, or user permissions. When these values are known in advance, they can be written directly in the code.
In Python, a collection of unique values written directly in code is called a set literal. It provides a simple way to define a set without creating it during program execution.
In this guide, you’ll learn what Python set literals are, their syntax, how to write them correctly, and the different forms they can take through beginner-friendly examples.
🚀 Getting Started: See how set literals compare with other literal types in our complete guide to Python literals.
💡 Tip: To understand how Python creates and manages sets internally, you can explore our guide on Python set() Function.
Introduction: What is a Set Literal?
Definition: A set literal is a collection of unique values written directly in Python code using curly braces ({}). Unlike dictionaries, it stores only values, not key-value pairs, and automatically removes duplicates.
Example: In colors = {"red", "green", "blue"}, {"red", "green", "blue"} is a Python set literal.
Now let’s look at its syntax before exploring the different forms of Python set literals.
How to Write Python Set Literals
Python allows set literals to be written directly in the source code without using any special function or constructor. A set literal is created by enclosing one or more unique values within curly braces, separating individual values with commas.
The syntax below shows the general structure of a Python set literal.
Syntax
set_name = {value1, value2, value3, ...}
Explanation: Here, set_name is the variable that stores the set. Each value is separated by a comma, while the curly braces tell Python to create a set. If the same value appears multiple times, Python automatically keeps only one copy.
| Component | Description |
|---|---|
set_name |
The variable used to store the set. |
= |
The assignment operator used to assign the set literal to a variable. |
{ } |
Curly braces that define the set literal. |
value |
An individual element stored in the set. |
, |
Separates one value from another. |
Let’s explore the different forms of Python set literals with simple examples.
Forms of Python Set Literals
Set literals can store different kinds of values. Depending on the data you’re working with, a set may contain strings, numbers, or a combination of multiple immutable data types.
Let’s explore each form of Python set literals with explanations and examples.
1. String Values
Set literals commonly store unique string values.
Example: String Values
colors = {"red", "green", "blue"}
print(colors)
Explanation: Each color becomes a separate element in the set. Since every value is unique, Python stores all of them exactly as written.
2. Numeric Values
A set literal can also store integers, floating-point numbers, or both.
Example: Numeric Values
numbers = {10, 20, 30, 40}
print(numbers)
Explanation: This set contains only numeric values. Because there are no duplicate numbers, every value becomes part of the final set.
3. Mixed Data Types
Python sets can contain values of different immutable data types.
Example: Mixed Data Types
sample = {"Python", 3.12, True, (10, 20)}
print(sample)
Explanation: This example shows that a set can contain different immutable data types at the same time. Here, Python stores a string, a floating-point number, a Boolean value, and a tuple together in one set.
Characteristics of Python Set Literals
Like every Python collection, set literals have a few characteristics that make them different from lists, tuples, and dictionaries.
- Each value in a set is unique.
- If duplicate values are provided, Python automatically removes them.
- Set literals are written inside curly braces (
{}). - Sets do not preserve insertion order as a defining characteristic of the data structure.
- Every element stored in a set must be immutable (hashable).
Key Examples at a Glance: Python Set Literals
Before moving on, here’s a quick recap of what you’ve learned about Python set literals.
| Type | Format | Example | Meaning |
|---|---|---|---|
| String Values | {value1, value2} |
{"red", "green"} |
Stores unique string values. |
| Numeric Values | {10, 20, 30} |
{10, 20, 30} |
Stores unique numeric values. |
| Mixed Data Types | {"A", 10, True} |
{"Python", 3.12, True} |
Stores different immutable data types together. |
Key Takeaways: Python Set Literals
Here are the key points to remember about Python set literals:
- A Python Set Literal is written using curly braces (
{}). - Set literals store only unique values.
- Duplicate values are automatically removed.
- Set elements are unordered.
- A set can contain different immutable data types.
- Curly braces containing key-value pairs create dictionaries, while curly braces containing only values create sets.