Python Tutorial

Introduction

Edit Template

Python Tutorial

Introduction

Edit Template

Python Sets

A set in Python is a special type of collection that’s unordered, unchangeable, and doesn’t allow duplicates. It’s useful when you want to store multiple values but don’t care about their order or repetition.

Sets are written using curly braces {} and can hold values of different data types.

Example: Creating a Set
				
					profile = {"Zara", 25, True, 5.6, 25}
print(profile)


				
			

Output:

				
					{True, 25, 5.6, 'Zara'}

				
			

Here’s what’s happening:

  • Even though 25 was added twice, it only appears once.

  • The order is completely random — you might see the values in a different sequence every time you run the code.

  • Because sets don’t maintain order, you can’t access items using indexes like you would with lists or tuples.

Accessing Set Items

Since sets don’t have indexes, the only way to access their items is to loop through them.

				
					profile = {"Zara", 25, True, 5.6}

for detail in profile:
    print(detail)

				
			

Output:

				
					True  
Zara  
25  
5.6

				
			

Each time you run the code, the order of items printed may change — and that’s normal for sets.

Summary

  • Sets are unordered: no indexes, no guaranteed order.

  • They automatically remove duplicates.

  • You can only loop through a set to access its items.

  • Once created, you can’t modify individual items, but you can add or remove items entirely (we’ll see that in the next lesson).

Sets are perfect when you need a bag of unique values — like keeping track of unique names, IDs, or tags — and want fast membership checking.

Keep learning, keep building — only at Learn With Arshyan.

Scroll to Top