-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms
A set is a collection which is unordered, unindexed, and does not allow duplicate values.
Sets are written with curly brackets {}
.
fruits = {"apple", "banana", "cherry"}
print(fruits)
Sets automatically remove duplicates.
fruits = {"apple", "banana", "apple"}
print(fruits) # {'apple', 'banana'}
if "banana" in fruits:
print("Yes")
fruits.add("orange")
fruits.update(["grape", "melon"])
print(len(fruits))
fruits.remove("banana") # Raises error if not found
fruits.discard("apple") # No error if not found
for x in fruits:
print(x)
a = {"a", "b"}
b = {1, 2}
c = a.union(b)
x = {1, 2, 3}
y = {3, 4, 5}
print(x.intersection(y)) # {3}
print(x.difference(y)) # {1, 2}
print(x.symmetric_difference(y)) # {1, 2, 4, 5}
Practice adding, removing, checking membership, and performing operations like union and intersection.
Q1. Write a Python program to create a set of 3 programming languages and print it.
Q2. Write a Python program to add "Java"
to the set using add()
.
Q3. Write a Python program to add multiple items like "C"
and "C++"
to the set using update()
.
Q4. Write a Python program to remove "Python"
from the set using remove()
or discard()
.
Q5. Write a Python program to check if "JavaScript"
exists in the set using the in
keyword.
Q6. Write a Python program to create two sets and perform a union operation using union()
or |
.
Q7. Write a Python program to find common items between two sets using intersection()
.
Q8. Write a Python program to find the difference between two sets using difference()
.
Q9. Write a Python program to loop through a set and print each item using a for
loop.
Q10. Write a Python program to get the number of items in a set using the len()
function.
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms