Introduction
In Python programming, storing and organizing data efficiently is very important. Python provides powerful built-in data structures called Dictionaries and Sets.
Dictionaries store information using key-value pairs, while sets store unique values without duplicates.
These data structures are widely used in Artificial Intelligence, Data Science, Machine Learning, Web Development, and Automation projects.
In this tutorial, you will learn Dictionaries and Sets in detail, including syntax, methods, operations, real-world examples, and practical programs.
Learning Objectives
- Understand Dictionaries in Python.
- Understand Sets in Python.
- Create and use dictionaries.
- Create and use sets.
- Apply dictionary and set methods.
- Understand real-world applications.
What is a Dictionary in Python?
A dictionary is a collection data type used to store data in the form of key-value pairs.
Dictionaries are:
- Ordered
- Mutable (changeable)
- Do not allow duplicate keys
Dictionaries are created using curly braces {}.
Example:
student = {
"name":"John",
"age":21,
"course":"Artificial Intelligence"
}
Here:
- name → key
- John → value
Why Dictionaries are Important
Dictionaries allow programmers to store structured information efficiently.
Instead of storing separate variables:
name = "John"
age = 21
course = "AI"
We can organize data inside one dictionary:
student = {
"name":"John",
"age":21,
"course":"AI"
}
This makes programs cleaner and easier to manage.
Creating Dictionaries
Dictionaries can contain multiple data types.
Example:
employee = {
"name":"David",
"salary":50000,
"active":True
}
Accessing Dictionary Values
Dictionary values can be accessed using keys.
Example:
student = {
"name":"Alice",
"age":22
}
print(student["name"])
Output:
Alice
Using the get() Method
The get() method safely retrieves dictionary values.
Example:
student = {
"name":"Alice",
"age":22
}
print(student.get("age"))
Output:
22
Updating Dictionary Values
Dictionaries are mutable, so their values can be modified.
Example:
student = {
"name":"John",
"age":20
}
student["age"] = 25
print(student)
Output:
{'name':'John','age':25}
Adding New Dictionary Items
New key-value pairs can be added easily.
Example:
student = {
"name":"John"
}
student["city"] = "London"
print(student)
Output:
{'name':'John','city':'London'}
Removing Dictionary Items
pop() Method
student = {
"name":"Alice",
"age":22
}
student.pop("age")
print(student)
Output:
{'name':'Alice'}
del Keyword
student = {
"name":"David",
"city":"Paris"
}
del student["city"]
print(student)
Useful Dictionary Methods
| Method | Description |
|---|---|
| keys() | Returns keys |
| values() | Returns values |
| items() | Returns key-value pairs |
| get() | Returns value safely |
| pop() | Removes item |
| update() | Updates dictionary |
Dictionary Looping
We can loop through dictionary elements.
Example:
student = {
"name":"John",
"course":"Python"
}
for key,value in student.items():
print(key,value)
What is a Set in Python?
A set is a collection data type used to store multiple unique values.
Sets are:
- Unordered
- Mutable
- Do not allow duplicate values
Sets are created using curly braces {}.
Example:
numbers = {1,2,3,4}
Why Sets are Important
Sets are useful when duplicate values must be removed automatically.
Example:
data = {10,10,20,20,30}
print(data)
Output:
{10,20,30}
Duplicate values are removed automatically.
Creating Sets
Sets can store multiple values.
Example:
languages = {"Python","Java","C++"}
Adding Items to Sets
add() Method
courses = {"Python","AI"}
courses.add("Machine Learning")
print(courses)
Removing Set Items
remove() Method
colors = {"Red","Blue","Green"}
colors.remove("Blue")
print(colors)
discard() Method
colors.discard("Yellow")
discard() avoids errors if the value does not exist.
Set Operations
Union Operation
Combines two sets.
A = {1,2,3}
B = {3,4,5}
print(A | B)
Output:
{1,2,3,4,5}
Intersection Operation
Returns common elements.
A = {1,2,3}
B = {2,3,4}
print(A & B)
Output:
{2,3}
Difference Operation
A = {1,2,3}
B = {2,3,4}
print(A - B)
Output:
{1}
Difference Between Dictionaries and Sets
| Feature | Dictionary | Set |
|---|---|---|
| Storage Style | Key-Value Pair | Unique Values |
| Duplicate Keys | Not Allowed | Not Applicable |
| Access Method | Using Keys | No Indexing |
| Ordering | Ordered | Unordered |
Real-World Examples
Student Information System
student = {
"name":"David",
"course":"AI",
"marks":90
}
print(student)
Unique Visitor Tracking
visitors = {"User1","User2","User1","User3"}
print(visitors)
Sets automatically remove duplicate visitors.
Python Example
employee = {
"name":"Robert",
"department":"IT"
}
print(employee["name"])
skills = {"Python","AI","ML"}
skills.add("Data Science")
print(skills)
Interview Questions
1. What is a dictionary in Python?
A dictionary is a mutable collection that stores key-value pairs.
2. What is a set in Python?
A set is an unordered collection of unique values.
3. Which method safely retrieves dictionary values?
The get() method.
4. Do sets allow duplicate values?
No, sets automatically remove duplicates.
Assignment
- Create a dictionary storing student information.
- Update one dictionary value.
- Add a new key-value pair.
- Create a set containing five numbers.
- Perform union and intersection operations.
Quiz
Q1. Which data structure stores key-value pairs?
- A. Tuple
- B. List
- C. Dictionary
- D. String
Answer: C. Dictionary
Q2. Which data structure removes duplicate values automatically?
- A. List
- B. Set
- C. Tuple
- D. Integer
Answer: B. Set
Summary
In this tutorial, you learned Dictionaries and Sets in Python. Dictionaries store information using key-value pairs, while sets store unique values without duplicates.
You explored dictionary methods, set operations, real-world examples, and practical applications.
These concepts are extremely important for Python programming, Data Science, Machine Learning, and Artificial Intelligence development.
Next Tutorial
Tutorial 15: Conditional Statements
“`
