Skip to main content

Set in Python पायथन में सेट, Set Operations सेट ऑपरेशन , Set Functions and Methods सेट फंक्शन एवं मेथड

In Python, a set is an unordered collection of unique elements. Sets are useful when we need to eliminate duplicates and perform common set operations like unions, intersections, and differences. We can create a set by using curly braces {} or the set() function but we cannot create an empty set using {}, as it creates an empty dictionary. Instead, we can use set().

पायथन में, एक सेट अद्वितीय तत्वों का एक अव्यवस्थित संग्रह है। सेट तब उपयोगी होते हैं जब हमें डुप्लिकेट को खत्म करने और यूनियनों, इंटरसेक्शन और अंतर जैसे सामान्य सेट ऑपरेशन करने की आवश्यकता होती है। हम कर्ली ब्रेसिज़ {} या set() फ़ंक्शन का उपयोग करके एक सेट बना सकते हैं लेकिन हम {} का उपयोग करके एक खाली सेट नहीं बना सकते, क्योंकि यह एक खाली डिक्शनरी बनाता है। इसके स्थान पर हम set() का उपयोग कर सकते है।

Creating a Set सेट तैयार करना :-

my_set = {1, 2, 3}

# Using the set() function ( set() फ़ंक्शन का उपयोग करना )

my_set2 = set([1, 2, 3, 4, 5])

Set Operations/Methods सेट ऑपरेशन/ मेथड:- 

1. Union (| or union()):- Returns a set containing all unique elements from both sets.
दोनों सेटों के सभी अद्वितीय तत्वों वाला सेट लौटाता है।

set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1 | set2 # {1, 2, 3, 4, 5}
union_set = set1.union(set2) # {1, 2, 3, 4, 5}

2. Intersection (& or intersection()):- Returns a set containing only the elements found in both sets.
केवल दोनों सेटों में पाए जाने वाले तत्वों वाला सेट लौटाता है।

intersection_set = set1 & set2 # {3}
intersection_set = set1.intersection(set2) # {3}

3. Difference (- or difference()):- Returns a set containing elements in the first set but not in the second.
पहले सेट में तत्वों वाले लेकिन दूसरे सेट में नहीं वाले तत्वों वाले सेट को लौटाता है।

difference_set = set1 - set2 # {1, 2}
difference_set = set1.difference(set2) # {1, 2}

4. Symmetric Difference (^ or symmetric_difference()):- Returns a set containing elements in either set but not in both.
किसी एक सेट में तत्वों वाले लेकिन दोनों में नहीं, सेट लौटाता है।

sym_diff_set = set1 ^ set2 # {1, 2, 4, 5}
sym_diff_set = set1.symmetric_difference(set2) # {1, 2, 4, 5}

5. Subset:- Checks if all elements of the set are in another set.
जाँचता है कि क्या सेट के सभी तत्व किसी अन्य सेट में हैं।

set3 = {1, 2}
is_subset = set3.issubset(set1) # Output: True

6. Superset:- Checks if all elements of another set are contained in this set.
जाँचता है कि क्या किसी अन्य सेट के सभी अवयव इस सेट में समाहित हैं।

is_superset = set1.issuperset(set3) # Output: True

7. Disjoint Sets:- Checks if two sets have no elements in common.
जाँचता है कि क्या दो सेटों में कोई तत्व समान नहीं है।

is_disjoint = set1.isdisjoint(set2) # Output: False (because they share the element 3)

8. Set Comprehensions:- We can use comprehensions to create sets based on conditions or transformations.
हम परिस्थितियों या परिवर्तनों के आधार पर सेट बनाने के लिए कॉम्प्रिहेंशन का उपयोग कर सकते हैं।

squared_set = {x**2 for x in range(5)} # {0, 1, 4, 9, 16}

Set Functions and Methods सेट फंक्शन एवं मेथड:-

1. len(set):- Returns the number of elements in the set.
यह फंक्शन सेट में तत्वों की संख्या लौटाता है।
my_set = {1, 2, 3}
print(len(my_set)) # Output: 3

2. max(set) / min(set):- Returns the largest or smallest element in the set.
यह फंक्शन सेट में सबसे बड़ा या सबसे छोटा तत्व लौटाता है।

print(max(my_set)) # Output: 3
print(min(my_set)) # Output: 1

3. sorted(set):- Returns a sorted list of elements in the set.
यह फंक्शन सेट में तत्वों की क्रमबद्ध सूची लौटाता है।

print(sorted(my_set)) # Output: [1, 2, 3]

4. add(element):- Adds an element to the set. If the element already exists, it does nothing.
यह फंक्शन सेट में एक तत्व जोड़ता है। यदि तत्व पहले से मौजूद है, तो यह कुछ नहीं करता है।

my_set.add(4)

5. update(iterable):- Adds multiple elements (from a list, tuple, etc.) to the set.
यह फंक्शन सेट में एकाधिक तत्वों (सूची, टपल, आदि ) को जोड़ता है।

my_set.update([5, 6, 7])

6. remove(element):- Removes the specified element from the set. Raises a KeyError if the element is not found.
यह फंक्शन सेट से निर्दिष्ट तत्व को हटाता है। यदि तत्व नहीं मिलता है तो KeyError उठाता है।

my_set.remove(2)

7. discard(element):- Removes the specified element, but does nothing if the element is not found (no error is raised).
यह फंक्शन निर्दिष्ट तत्व को हटाता है, लेकिन यदि तत्व नहीं मिलता है तो कुछ नहीं करता (कोई त्रुटि नहीं होती)।

my_set.discard(10)

8. pop():- Removes and returns an arbitrary element from the set. Raises a KeyError if the set is empty.
यह फंक्शन सेट से एक मनमाना तत्व निकालता है और लौटाता है। यदि सेट खाली है तो KeyError उठाता है।

element = my_set.pop()

9. clear():- Removes all elements from the set, leaving it empty.
यह फंक्शन सेट से सभी तत्वों को हटा देता है, तथा उसे रिक्त छोड़ देता है।

my_set.clear()

Comments

Popular posts from this blog

Filter function of python

filter function:-  Imagine we have a bunch of things, like a list of numbers, a collection of fruits, or any other group of items. Now, let's say we want to pick out only certain items from that group based on a specific condition. This is where the filter function comes in handy. Let's say we have a list of numbers, and we want to filter out only the even numbers. We define a condition-checking function that checks if a number is even. Then, we pass this function and the list of numbers to the filter function. It goes through each number, applies the condition-checking function, and keeps only the numbers that are even. Finally, it gives us a new list containing only the even numbers. def is_even(num):     return num % 2 == 0 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] filtered_numbers = list(filter(is_even, numbers)) print(filtered_numbers) Output [2, 4, 6, 8, 10] We can also utilize lambda expressions to define filtering conditions directly within the filter function. nu...

The Python Interpreter पायथन इंटरप्रेटर

The Python interpreter is a program that executes Python code. It reads and interprets Python scripts or interactive commands, converting them into machine-readable bytecode for the computer to execute.The interpreter is a crucial component for running Python code, facilitating both learning and development processes. Users can interact with the interpreter in two primary modes: पायथन इंटरप्रेटर एक प्रोग्राम है जो पायथन कोड को निष्पादित करता है। यह पायथन स्क्रिप्ट या इंटरैक्टिव कमांड को पढ़ता है और व्याख्या करता है, उन्हें कंप्यूटर द्वारा निष्पादित करने के लिए मशीन-पठनीय बाइटकोड में परिवर्तित करता है। दुभाषिया पायथन कोड चलाने के लिए एक महत्वपूर्ण घटक है, जो सीखने और विकास दोनों प्रक्रियाओं को सुविधाजनक बनाता है। उपयोगकर्ता इंटरप्रेटर के साथ दो प्राथमिक मोड में बातचीत कर सकते हैं:- 1. Interactive Mode:- In this mode we can launch the Python interpreter in our terminal or command prompt without specifying a script.This mode allows us to enter Python commands and see immediate results...

Flow of execution of a python program पाइथन प्रोग्राम के निष्पादन का प्रवाह

Flow of execution represents a general outline of Python program execution. The specific flow can vary based on the program's structure, control flow statements (if, else, for, while), and function calls. निष्पादन का प्रवाह पायथन प्रोग्राम निष्पादन की एक सामान्य रूपरेखा का प्रतिनिधित्व करता है। विशिष्ट प्रवाह प्रोग्राम की संरचना, नियंत्रण प्रवाह विवरण (यदि, अन्यथा, के लिए, जबकि), और फ़ंक्शन कॉल के आधार पर भिन्न हो सकता है। 1.) Start: The program begins execution. 1.) प्रारंभ: प्रोग्राम का निष्पादन प्रारंभ होता है। 2.) Import Modules: If necessary, the program imports external modules or libraries. 2.) मॉड्यूल आयात करें: यदि आवश्यक हो, तो प्रोग्राम बाहरी मॉड्यूल या लाइब्रेरी आयात करता है। 3.) Define Functions: Any functions used in the program are defined. 3.) फंक्शन्स को परिभाषित करें: प्रोग्राम में उपयोग किए गए किसी भी फंक्शन को परिभाषित किया जाता है। 4.) Main Program: The main body of the program starts. 4.) मुख्य प्रोग्राम: प्रोग्राम का मुख्य भाग प्रारंभ होता है। 5.) Initializat...