🎓 All Courses | 📚 Python Programming Syllabus

📋 Study this course on TaskLoco
#python-programming#tuples#sets#data-structures

Tuples & Sets

Tuples — ordered, immutable sequences.

coords = (10, 20)
point = (3.14, 2.71, 1.41)

coords[0]   # 10
x, y = coords  # unpacking

Use tuples for data that shouldn't change: coordinates, RGB values, database records.


Sets — unordered collections of unique values.

colors = {"red", "green", "blue"}
colors.add("yellow")
colors.remove("red")
"green" in colors   # True

Set operations:

a = {1, 2, 3, 4}
b = {3, 4, 5, 6}

a | b   # union: {1,2,3,4,5,6}
a & b   # intersection: {3,4}
a - b   # difference: {1,2}
a ^ b   # symmetric diff: {1,2,5,6}

When to use sets: Removing duplicates from a list (list(set(my_list))), membership testing (sets are O(1) vs list O(n)).


YouTube • Top 10
Python Programming: Tuples & Sets
Tap to Watch ›
📸
Google Images • Top 10
Python Programming: Tuples & Sets
Tap to View ›

Reference:

Wikipedia: Set

image for linkhttps://en.wikipedia.org/wiki/Set_(abstract_data_type)

📚 Python Programming — Full Course Syllabus
📋 Study this course on TaskLoco

TaskLoco™ — The Sticky Note GOAT