
Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods 2.1. tell() Method 2.2....
Set, elements का unordered ओर unindexed collections होता है इसके elements unique होते हैं मतलब कोई भी elements repeat नहीं हो सकता और set unordered होता है इसलिए set के elements को हम directly indexing से access नहीं कर सकते, set को { } के define किया जाते हैं या फिर हम set banane के लिए set function का भी use कर सकते हैं।
my_set = {5, 7, 3, 1}
print(my_set)
# using set() method
a = [4, 2, 7]
my_set = set(a)
print(my_set)
# create empty set
empty_set = set()
loop का use करके हम set के elements को iterate कर सकते है, ध्यान दीजिए हम indexing से set के items को access नहीं कर सकते है।
fruits = {"banana", "apple", "guava"}
for i in fruits:
print(i)
# Output : banana
# apple
# guava
python मे set के लिए build-in methods होते है, जिनका use करके set के साथ अलग अलग operation perform किया जाता है।
Syntex:
method को किसी भी variable के साथ use करने के लिए dot ( . ) का use करते है।
Example:
add() : इस method का use हम set मे नए item को add करने के लिए करते है।
my_fruits = {"apple", "banana"}
my_fruits.add("cherry")
print(f"Set after adding 'cherry': {my_fruits}")
my_fruits.add("banana") # Adding a duplicate, set remains unchanged
print(f"Set after trying to add 'banana' again: {my_fruits}")
# Using remove()
my_colors = {"red", "green", "blue", "yellow"}
my_colors.remove("green")
print(f"Set after removing 'green': {my_colors}")
# Using discard()
my_numbers = {10, 20, 30, 40, 50}
my_numbers.discard(30)
print(f"Set after discarding 30: {my_numbers}")
# Using pop()
my_items = {"apple", "banana", "cherry"}
popped_item = my_items.pop()
print(f"Popped item: {popped_item}")
print(f"Set after pop(): {my_items}")
# Using clear()
my_data = {10, 20, 30}
my_data.clear()
print(f"Set after clear(): {my_data}")
# Using update()
my_set = {1, 2, 3}
other_set = {3, 4, 5}
my_set.update(other_set)
print(f"Set after update(): {my_set}")
# Using union()
my_set.union(other_set)
print(f"Set after union(): {my_set}")
intersection(): दो sets का intersection return करता है।
difference(): दो sets का difference return करता है।
symmetric_difference(): दो sets का symmetric difference return करता है।
issubset(): Check करता है की एक set दूसरे set का subset है या नहीं।
issuperset(): Check करता है की एक set दूसरे set का superset है या नहीं।
उम्मीद करते है कि आपको python set अच्छे समझ मे आ गया होगा । अपने learning को continue रखने के लिए next button पर click करे,
Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods 2.1. tell() Method 2.2....
Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...
Table of Contents All Chapters 1. Modules in Python 2. Pre-defined Modules 2.1. math 2.2. random...
Table of Contents All Chapters 1. Iterators 1.1. Iterable 1.2. Iterator 1.3. Iterator Protocol...
Table of Contents All Chapters 1. Polymorphism 2. Key Concepts of Polymorphism 3. Types of Polymorphism 3.1. Polymorphism...
Table of Contents All Chapters 1. Inheritance 2. Concepts of Inheritance 3. Types of Inheritance 3.1. Single Inheritance...
Table of Contents All Chapters 1. Classes and Objects 2. Class in Python 3. Class Structure 3.1. Attributes...
Table of Contents All Chapters 1. Python Function 2. Define Function 3. Function Arguments 4. Function Parameters 5. Local and...
Table of Contents All Chapters 1. Python Dictionary 2. Access Dictionary items 3. Change Dictionary items 4. Add items in...