MateriasProgramación 110-03

Tuplas

Métodos

t.count(s: string): number
t.index(s: string): number

Sets

In Python, a set is an unordered collection of unique elements. Here’s what you need to know:

  • No duplicates: Every item in a set is unique.,
  • Unordered: The elements have no fixed order, so you can’t access them by index.,
  • Mutable: You can add or remove items after creation, but the items themselves must be immutable (like numbers, strings, or tuples).,

Basic usage:

# Create a set
my_set = {1, 2, 3, 4}
print(my_set) # Output: {1, 2, 3, 4}

# Adding elements
my_set.add(5) # {1, 2, 3, 4, 5}

# Removing elements
my_set.remove(2) # {1, 3, 4, 5}

# Check membership 
print(3 in my_set) # True

# No duplicates allowed
my_set = {1, 2, 2, 3}
print(my_set) # Output: {1, 2, 3}

# Subset
my_set.issubset(set(5,3,2,6))

# Superset
my_set.issuperset(set(2,5,5,6))

Set operations:

  • Union: Combines elements from both sets: a | b or a.union(b),
  • Intersection: Only elements in both: a & b or a.intersection(b),
  • Difference: Elements in one but not the other: a - b,
  • Symmetric Difference: Elements in either, but not both: a ^ b
Built with LogoFlowershow