Delete Set public Set private Add tags Delete tags
  Add tag   Cancel
  Delete tag   Cancel
  • • DevOps notes •
  •  
  • AI
  • Tags
  • Login

Tuples, sets/shaare/2QdV2w

  • python
  • python

Tuples (tuple)

Tuples are ordered, immutable sequences defined with parentheses (). Once created, their contents cannot be changed.

Characteristics and Use Cases

  • Ordered: items maintain position
  • Immutable: cannot add, remove, or change after creation
  • Useful for fixed records like coordinates, version numbers, or as dictionary keys
host_port = ("127.0.0.1", 3000)
red_rgb = (255, 0, 0)
tuple_single_value = ("only-value",) # To create a single-item tuple, add a trailing comma
print(type(host_port))
print(type(tuple_single_value))

print(f"Host: {host_port[0]}")
print(red_rgb[-2:])
print(type(red_rgb[-2:]))

# host_port[0] = "192.168.1.1" # Uncommenting will raise a TypeError because tuples are immutable

Sets (set)

  • Characteristics: Unordered, Mutable, Unique items only (duplicates removed)
    • The items of a set must be immutable.
  • Use Cases: Membership testing, removing duplicates, set operations (union, intersection, difference).

Set Operations

  • Membership Testing: Check if an item exists in a set using the in keyword.
  • Adding Items: Use add() to add an item to a set.
  • Removing Items: Use remove() to remove an item (raises an error if the item doesn't exist) or discard() to remove an item (doesn't raise an error if the item doesn't exist).
  • Set Operations:
    • Union: Combine all unique items from two sets using union() or |.
    • Intersection: Find common items between two sets using intersection() or &.
    • Difference: Find items in one set but not in another using difference() or -.
unique_ports = set([80, 443, 22, 80, 8080, 443])
server_names = {"web01", "web02"}

print(unique_ports)
print(22 in unique_ports)
print(22 in server_names)

unique_ports.add(3000)
print(unique_ports)
unique_ports.remove(22)
print(unique_ports)
# unique_ports.remove(22) # Will raise KeyError because item 22 is not in the set anymore
unique_ports.discard(22)
print(unique_ports)
# set_of_lists = set([[1, 2], [3, 4]]) # Will throw a TypeError, since lists are mutable
# set_of_sets = {{1, 2}, {3, 4}} # Will throw a TypeError, since sets are mutable
set_of_tuples = {(1, 2), (3, 4)}
print(set_of_tuples)
print((1, 2) in set_of_tuples)
print((1, 3) in set_of_tuples)
2 months ago Permalink
cluster icon
  • Classes and Objects : Classes and Objects Beyond Built-ins: Python lets you define your own data types using class. Class: A blueprint or template for creating objects. De...
  • Generics typing : Introduction to Generics Generic types let you write reusable, type-safe functions and classes that work uniformly across different data types. They ...
  • The Iteration Protocol : The Iteration Protocol We use for item in sequence: all the time. But how does Python get each item? Iterable: An object that can be looped over. It...
  • Filesystem Operations : Filesystem Operations (os & shutil) DevOps scripts often need to create, delete, copy, and move files and directories as part of automation workflows...
  • Implementing Retries and Timeouts : Implementing Retries and Timeouts External services can be slow or unreliable, causing scripts to hang or fail unexpectedly. Timeouts and retries hel...


(97)
Filter untagged links
Fold Fold all Expand Expand all Are you sure you want to delete this link? Are you sure you want to delete this tag? The personal, minimalist, super-fast, database free, bookmarking service by the Shaarli community