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

Programming with Python Language Tutorial available in hindi and english पाइथन प्रोग्रामिंग टुटोरिअल हिंदी एवं अंग्रेजी भाषा में

 Unit -01 1. Introduction to python (पाइथन भाषा का परिचय) 2. History of Python Programming Language पाइथन प्रोग्रामिंग लैंग्वेज का इतिहास 3. Python Programming Language’s Features and Advantages पायथन प्रोग्रामिंग लैंग्वेज की प्रमुख विशेषताएं 4. Python’s Programming Language's Popularity पाइथन प्रोग्रामिंग लैंग्वेज की लोकप्रियता 5.The Future of Python Programming Language पायथन प्रोग्रामिंग लैंग्वेज का भविष्य 6. The Python Interpreter पायथन इंटरप्रेटर 7. The Python IDLE पाइथन आईडीएलई 8. Installation process of Python IDLE पायथन आईडीएलई की इंस्टालेशन प्रक्रिया 9. Dynamically typed and strongly typed features of python गतिशील रूप से टाइप और दृढ़ता से टाइप की गई भाषा पायथन 10. Basic Data Types of Python पायथन के बेसिक डेटा टाइप 11. Variables in python पायथन में वेरिएबल 12. Expressions in Python पायथन में अभिव्यक्तियां 13. Statements in Python पायथन में कथन 14. Operator's in Python पायथन में ऑपरेटर्स 15. Flow of execution of a python program पाइथन प्रोग्राम के निष्पादन का प्रवाह 16...

Inbuilt Functions in Python पाइथन में अंतर्निहित फ़ंक्शन्स

Python is a powerful programming language that provides many inbuilt functions to make coding simple and efficient. These functions help programmers perform common tasks like finding length, converting characters, or checking memory addresses without writing extra code. पायथन एक शक्तिशाली प्रोग्रामिंग भाषा है जो कई अंतर्निहित (inbuilt) फ़ंक्शन्स प्रदान करती है, जिससे कोडिंग आसान और प्रभावी हो जाती है। ये फ़ंक्शन प्रोग्रामर को सामान्य कार्य करने में मदद करते हैं जैसे लंबाई निकालना, अक्षरों का रूपांतरण करना या मेमोरी एड्रेस जांचना, और इसके लिए अतिरिक्त कोड लिखने की आवश्यकता नहीं होती। (a)  id()  – Unique Identifier of an Object  किसी वस्तु का अद्वितीय पहचानकर्ता The  id()  function returns the unique memory address of an object. Every variable or object in Python is stored in memory, and  id()  helps us know where exactly it is stored. id()  फ़ंक्शन किसी ऑब्जेक्ट का अद्वितीय मेमोरी एड्रेस लौटाता है। पायथन में हर वेरिएबल या ऑब्जेक्ट मेमोरी में संग्रह...

Conditional statements in Python if, if-else, if-elif-else पायथन में सशर्त कथन

  In Python, conditional statements help you make decisions in the code. There are three main types of conditionals:- पायथन में, सशर्त कथन आपको कोड में निर्णय लेने में मदद करते हैं। सशर्त के तीन मुख्य प्रकार हैं:-  1.) Conditional `if` statement:- An `if` statement executes the code block only if the condition evaluates to `True`. 1.) सशर्त `if` कथन:- एक `if` स्टेटमेंट कोड ब्लॉक को केवल तभी निष्पादित करता है जब स्थिति सही पर मूल्यांकन करती है। Syntax:- if condition:    Statement(s) Example:- x = 10 if x > 5:     print("x is greater than 5") 2.) Alternative `if-else` statement:  An `if-else` statement checks a condition and executes a block of code('if' block), if the condition is true otherwise provides an alternative block of code ('else' block), if the condition is false. 2.) वैकल्पिक `if- else` कथन:- एक `if-else` स्टेटमेंट एक शर्त की जाँच करता है और कोड के एक ब्लॉक ('if' ब्लॉक) को निष्पादित करता है, यदि स्थिति सही है अन्यथा कोड का एक वैकल्पिक ब्ल...