CHAPTER 09
Tuples और Sets
Immutable sequences और unique collections — Python data structures
Advertisement
Tuple
Tuple List जैसा है लेकिन इसे बाद में बदला नहीं जा सकता। () brackets use होते हैं।
python
colors = ("लाल", "हरा", "नीला") months = ("Jan", "Feb", "Mar", "Apr") print(colors[0]) # लाल print(len(colors)) # 3 print("हरा" in colors) # True # Tuple unpacking x, y, z = (10, 20, 30) print(x, y, z) # 10 20 30 # colors[0] = "पीला" ← ERROR! Tuple नहीं बदलता
OUTPUT
लाल
3
True
10 20 30Advertisement
Set
Set में duplicate values नहीं होतीं। {} brackets use होते हैं।
python
nums = {1, 2, 3, 2, 1, 4}
print(nums) # {1, 2, 3, 4} duplicates हट गए!
# Set operations (Mathematics)
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
print(A & B) # {3, 4} - common
print(A | B) # {1,2,3,4,5,6} - सब
print(A - B) # {1, 2} - difference
# List के duplicates हटाना
data = [1,2,2,3,3,4]
unique = list(set(data))
print(unique) # [1, 2, 3, 4]
OUTPUT
{1, 2, 3, 4}
{3, 4}🎯 QUICK QUIZ — Chapter 09
Tuple और Set
Advertisement