Python Tuples Sets

Python Tuples and Sets

In Python, tuples and sets are two different data structures used to store collections of items. Below is a detailed explanation of both, including syntax and examples.


Tuples

A tuple is an immutable (unchangeable) ordered collection of items. It is used when you want to store multiple values and ensure that they cannot be modified after creation.

Characteristics of Tuples

  • Immutable: Once created, the elements cannot be changed.
  • Ordered: The order of elements is preserved.
  • Allows Duplicates: Tuples can have duplicate elements.
  • Heterogeneous: Tuples can contain elements of different data types.

Syntax

  # Creating a tuple
  tuple_name = (element1, element2, element3, ...)
 

Examples

  # Example of a tuple
  my_tuple = (1, "apple", 3.14, "apple")

  # Accessing elements by index
  print(my_tuple[0])  # Output: 1
  print(my_tuple[1:3])  # Output: ('apple', 3.14)

  # Length of a tuple
  print(len(my_tuple))  # Output: 4

  # Nested tuple
  nested_tuple = (1, (2, 3), 4)
  print(nested_tuple[1][1])  # Output: 3
 

Key Operations

  • Indexing: Access elements by their position.
  • Slicing: Extract a portion of the tuple.
  • Concatenation: Combine two tuples.
  • Repetition: Repeat a tuple multiple times.
  # Concatenation
  tuple1 = (1, 2)
  tuple2 = (3, 4)
  print(tuple1 + tuple2)  # Output: (1, 2, 3, 4)

  # Repetition
  print(tuple1 * 3)  # Output: (1, 2, 1, 2, 1, 2)
 


Sets

A set is an unordered collection of unique items. Sets are mainly used when you need to store unique elements and perform operations like union, intersection, and difference.

Characteristics of Sets

  • Unordered: Elements do not have a fixed order.
  • Unique: Duplicate elements are automatically removed.
  • Mutable: Elements can be added or removed after creation.
  • No Indexing: Since sets are unordered, they do not support indexing or slicing.

Syntax

  # Creating a set
  set_name = {element1, element2, element3, ...}

  # Creating an empty set
  empty_set = set()
 

Examples

# Example of a set
  my_set = {1, 2, 3, 4, 4, 5}
  print(my_set)  # Output: {1, 2, 3, 4, 5} (duplicates are removed)

  # Adding elements to a set
  my_set.add(6)
  print(my_set)  # Output: {1, 2, 3, 4, 5, 6}

  # Removing elements from a set
  my_set.remove(3)
  print(my_set)  # Output: {1, 2, 4, 5, 6}

  # Checking membership
  print(2 in my_set)  # Output: True
  print(7 in my_set)  # Output: False
 

Key Operations

  • Union (|): Combines elements from both sets.
  • Intersection (&): Returns common elements.
  • Difference (-): Elements in one set but not the other.
  • Symmetric Difference (^): Elements in either set but not in both.
  set1 = {1, 2, 3}
  set2 = {3, 4, 5}

  # Union
  print(set1 | set2)  # Output: {1, 2, 3, 4, 5}

  # Intersection
  print(set1 & set2)  # Output: {3}

  # Difference
  print(set1 - set2)  # Output: {1, 2}

  # Symmetric Difference
  print(set1 ^ set2)  # Output: {1, 2, 4, 5}
 

Set Methods

Method Description
add() Adds a single element to the set.
remove() Removes a specific element (raises error if not present).
discard() Removes a specific element (does not raise error).
pop() Removes an arbitrary element.
clear() Removes all elements from the set.

Key Differences Between Tuples and Sets

Feature Tuple Set
Order Ordered Unordered
Mutability Immutable Mutable
Duplicates Allows duplicates No duplicates allowed
Indexing Supports indexing and slicing Does not support indexing

By understanding tuples and sets, you can efficiently store and manipulate data in Python, depending on the requirements of your program.

Previous Next