KEMBAR78
Tuples in Python 2 Object Oriented Programming.pptx
Tuple in Python Continued
….
By,
M. Zuhair Arfeen
Immutability of Tuples
 Key Property: Tuples are immutable.
 You cannot modify, add, or remove elements after
creating a tuple.
 Example: Attempting to modify a tuple:
my_tuple = (1, 2, 3)
my_tuple[0] = 5 # Error: TypeError: 'tuple' object
does not support item assignment
Nested Tuples
Tuples can contain other tuples (nested structure).
nested_tuple = (1, (2, 3), (4, 5, 6))
print(nested_tuple[1]) # Output: (2, 3)
print(nested_tuple[1][0]) # Output: 2
Tuple Methods
 Indexing & Counting Methods
 tuple.index(value)
 Returns the index of the first occurrence of value.
 my_tuple = (10, 20, 30, 10)
 print(my_tuple.index(10)) # Output: 0
 tuple.count(value)
 Returns the count of the number of times value
appears.
 print(my_tuple.count(10)) # Output: 2
Tuple Use Cases
Use Cases of Tuples:
1.Data Integrity: Use tuples when you want to
ensure data cannot be changed.
2.Dictionary Keys: Tuples can be used as keys in
dictionaries because they are immutable.
3.Fixed Collections: Group related items that
should remain constant, like coordinates (x, y).
When to Use Tuples?
 When you need data that shouldn’t be changed.
 When memory optimization is critical.
 When using elements as dictionary keys.
 When you're dealing with a collection of
heterogenous data.
Conclusion
 Summary:
 Tuples are ordered, immutable collections in
Python.
 They are memory efficient and provide faster
operations compared to lists.
 Use them when you need read-only collections of
data.

Tuples in Python 2 Object Oriented Programming.pptx

  • 1.
    Tuple in PythonContinued …. By, M. Zuhair Arfeen
  • 2.
    Immutability of Tuples Key Property: Tuples are immutable.  You cannot modify, add, or remove elements after creating a tuple.  Example: Attempting to modify a tuple: my_tuple = (1, 2, 3) my_tuple[0] = 5 # Error: TypeError: 'tuple' object does not support item assignment
  • 3.
    Nested Tuples Tuples cancontain other tuples (nested structure). nested_tuple = (1, (2, 3), (4, 5, 6)) print(nested_tuple[1]) # Output: (2, 3) print(nested_tuple[1][0]) # Output: 2
  • 4.
    Tuple Methods  Indexing& Counting Methods  tuple.index(value)  Returns the index of the first occurrence of value.  my_tuple = (10, 20, 30, 10)  print(my_tuple.index(10)) # Output: 0  tuple.count(value)  Returns the count of the number of times value appears.  print(my_tuple.count(10)) # Output: 2
  • 5.
    Tuple Use Cases UseCases of Tuples: 1.Data Integrity: Use tuples when you want to ensure data cannot be changed. 2.Dictionary Keys: Tuples can be used as keys in dictionaries because they are immutable. 3.Fixed Collections: Group related items that should remain constant, like coordinates (x, y).
  • 6.
    When to UseTuples?  When you need data that shouldn’t be changed.  When memory optimization is critical.  When using elements as dictionary keys.  When you're dealing with a collection of heterogenous data.
  • 7.
    Conclusion  Summary:  Tuplesare ordered, immutable collections in Python.  They are memory efficient and provide faster operations compared to lists.  Use them when you need read-only collections of data.