B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Python Set Exercises with Solutions | LiveCodeProgramming

Python Set Exercises

Practice with the following set-based exercises to strengthen your Python skills.

Python set से जुड़े नीचे दिए गए अभ्यासों के साथ अपने कौशल को मज़बूत करें।

Exercise 1: Create and Print a Set
fruits = {'apple', 'banana', 'cherry'}
print(fruits)
{'banana', 'cherry', 'apple'}

This program creates a set and prints it.

यह प्रोग्राम एक set बनाकर उसे print करता है।

Exercise 2: Add an Item to a Set
fruits = {'apple', 'banana'}
fruits.add('orange')
print(fruits)
{'apple', 'orange', 'banana'}

Adds an element to a set using add().

add() का उपयोग करके एक item set में जोड़ा गया है।

Exercise 3: Add Multiple Items
fruits = {'apple'}
fruits.update(['banana', 'grape'])
print(fruits)
{'banana', 'apple', 'grape'}

Adds multiple items using update().

update() का उपयोग करके कई items जोड़े गए हैं।

Exercise 4: Remove Item
fruits = {'apple', 'banana'}
fruits.remove('banana')
print(fruits)
{'apple'}

Removes item using remove().

remove() के द्वारा एक item हटाया गया है।

Exercise 5: Discard Item (No Error)
fruits = {'apple'}
fruits.discard('banana')
print(fruits)
{'apple'}

discard() does not raise error if item not found.

discard() item ना मिलने पर error नहीं देता।

Exercise 6: Union of Sets
a = {'apple', 'banana'}
b = {'banana', 'cherry'}
print(a.union(b))
{'banana', 'cherry', 'apple'}

Performs union operation.

यह union ऑपरेशन करता है।

Exercise 7: Intersection of Sets
a = {1, 2, 3}
b = {2, 3, 4}
print(a & b)
{2, 3}

Finds common elements.

common elements दिखाए गए हैं।

Exercise 8: Set Difference
x = {1, 2, 3, 4}
y = {3, 4, 5}
print(x - y)
{1, 2}

Removes items in y from x.

y के items को x से हटाया गया है।

Exercise 9: Check Membership
fruits = {'apple', 'banana'}
print('apple' in fruits)
True

Checks if an item exists in set.

जांच करता है कि item मौजूद है या नहीं।

Exercise 10: Loop Through Set
colors = {'red', 'green', 'blue'}
for c in colors:
    print(c)
red
green
blue

Loops through the set.

set के सभी items को loop किया गया है।