KEMBAR78
Tuples in Python Object Oriented Programming.pptx
Tuples in Python
By,
M. Zuhair Arfeen
Introduction to Tuples
 What is a Tuple?
A tuple is an ordered collection of elements
in Python.
Unlike lists, tuples are immutable—once
created, the elements cannot be changed.
Syntax: tuple = (element1, element2, ...)
Why Use Tuples?:
 Advantages of Tuples:
 Immutability:Prevents accidental modification of
data.
 Memory Efficiency:Tuples use less memory
compared to lists.
 Faster than Lists:Since they are immutable,
operations on tuples are generally faster.
 Use Case: Best for read-only data or grouping
related items that shouldn’t change.
Creating Tuples
 Creating an Empty Tuple:
 empty_tuple = ()
 Creating a Tuple with Elements:
 tuple_with_elements = (1, "Python", 3.14)
Accessing Tuple Elements
 Indexing:
 Like lists, tuples are indexed. First element
has an index of 0.
 my_tuple = ("apple", "banana", "cherry")
 print(my_tuple[0]) # Output: "apple"
… continued
 Negative Indexing:
Use negative indices to access elements from the
end.
print(my_tuple[-1]) # Output: "cherry“
 Slicing:
You can slice tuples like lists to get a range of
elements
print(my_tuple[0:2]) # Output: ("apple", "banana")
Tuple Operations
 Concatenation: Tuples can be concatenated
using the ‘+’ operator.
tuple1 = (1, 2, 3)
tuple2 = (4, 5)
result = tuple1 + tuple2 # Output: (1, 2, 3, 4, 5)
 Repetition: You can repeat tuples using the *
operator.
tuple1 = (1, 2, 3)
result = tuple1 * 2 # Output: (1, 2, 3, 1, 2, 3)
Tuple Functions
 Common Tuple Functions:
 len():
 Returns the number of elements in the tuple
 len(my_tuple) # Output: 3
 max() and min()
 max((3, 5, 2)) # Output: 5
… continued
sum(): Sums all elements in a numeric tuple.
sum((1, 2, 3)) # Output: 6
tuple():
Converts a list or other iterable into a tuple.
tuple([1, 2, 3]) # Output: (1, 2, 3)

Tuples in Python Object Oriented Programming.pptx

  • 1.
  • 2.
    Introduction to Tuples What is a Tuple? A tuple is an ordered collection of elements in Python. Unlike lists, tuples are immutable—once created, the elements cannot be changed. Syntax: tuple = (element1, element2, ...)
  • 3.
    Why Use Tuples?: Advantages of Tuples:  Immutability:Prevents accidental modification of data.  Memory Efficiency:Tuples use less memory compared to lists.  Faster than Lists:Since they are immutable, operations on tuples are generally faster.  Use Case: Best for read-only data or grouping related items that shouldn’t change.
  • 4.
    Creating Tuples  Creatingan Empty Tuple:  empty_tuple = ()  Creating a Tuple with Elements:  tuple_with_elements = (1, "Python", 3.14)
  • 5.
    Accessing Tuple Elements Indexing:  Like lists, tuples are indexed. First element has an index of 0.  my_tuple = ("apple", "banana", "cherry")  print(my_tuple[0]) # Output: "apple"
  • 6.
    … continued  NegativeIndexing: Use negative indices to access elements from the end. print(my_tuple[-1]) # Output: "cherry“  Slicing: You can slice tuples like lists to get a range of elements print(my_tuple[0:2]) # Output: ("apple", "banana")
  • 7.
    Tuple Operations  Concatenation:Tuples can be concatenated using the ‘+’ operator. tuple1 = (1, 2, 3) tuple2 = (4, 5) result = tuple1 + tuple2 # Output: (1, 2, 3, 4, 5)  Repetition: You can repeat tuples using the * operator. tuple1 = (1, 2, 3) result = tuple1 * 2 # Output: (1, 2, 3, 1, 2, 3)
  • 8.
    Tuple Functions  CommonTuple Functions:  len():  Returns the number of elements in the tuple  len(my_tuple) # Output: 3  max() and min()  max((3, 5, 2)) # Output: 5
  • 9.
    … continued sum(): Sumsall elements in a numeric tuple. sum((1, 2, 3)) # Output: 6 tuple(): Converts a list or other iterable into a tuple. tuple([1, 2, 3]) # Output: (1, 2, 3)