PYTHON TUPLE
Tuple:
A tuple in Python is a collection of ordered elements, similar to a
list, but with the key difference that tuples are immutable, meaning
they cannot be modified after creation. Tuples are defined using
parentheses ().
Tuples can contain elements of different data
types, including numbers, strings, booleans, or
even other tuples.
my_tuple = (1, 2, 3, "hello", True)
You can access elements of a tuple using
indexing, just like with lists:
print(my_tuple[0]) # Output: 1
print(my_tuple[3]) # Output: "hello"
Since tuples are immutable, you cannot modify
individual elements or add/remove elements after
the tuple is created. However, you can
concatenate or multiply tuples to create new
tuples:
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
concatenated_tuple = tuple1 + tuple2
print(concatenated_tuple) # Output: (1, 2, 3, 4, 5, 6)
repeated_tuple = tuple1 * 3
print(repeated_tuple) # Output: (1, 2, 3, 1, 2, 3, 1, 2, 3)
1
KUMARAN.V MSC IT,GUMMIDIPOONDI
PYTHON TUPLE
benefits of tupe
Immutability: Tuples are immutable, meaning once they are
created, their contents cannot be changed. This property ensures
data integrity and can be useful in scenarios where you want to
ensure that the data remains constant throughout its lifecycle.
Performance: Tuples are generally more memory-efficient and
faster to iterate over than lists, especially for large collections of
data. This is because tuples are simpler data structures with fixed
sizes, whereas lists may dynamically resize themselves as
elements are added or removed.
Sequence Packing and Unpacking: Tuples support packing
multiple values into a single variable and unpacking values from a
tuple into separate variables. This can make code more concise
and readable
Operation of tupes:
1. Indexing
2. Slicing
3. Concatenation
4. Repetitions
5. Membership
6. Comparison
Indexing:
Indexing to access individual elements within a data structure such
as a string, list, tuple, or dictionary.
program:
a=(20,40,60,”apple”,”ball”)
print(a[0])
output:20
2
KUMARAN.V MSC IT,GUMMIDIPOONDI
PYTHON TUPLE
a[2]
output:60
Slicing:
slicing is a technique used to extract a portion of a sequence (like a
string, list, or tuple) by specifying a range of indices.
program:
a=(20,40,60,”apple”,”ball”)
print(a[1:3])
output: (40,60)
Concatenation
concatenation refers to the process of combining two or more
sequences (such as strings, lists, or tuples) into a single sequence.
The + operator is commonly used for concatenation in Python.
program:
a=(20,40,60,”apple”,”ball”)
b=(2,4)
print(a+b)
output: (20,40,60,”apple”,”ball”,2,4)
Repetition
Repetition refers to the process of creating a new sequence by
repeating an existing sequence multiple times. This operation is
achieved using the * operator.
program:
print(b*2)
3
KUMARAN.V MSC IT,GUMMIDIPOONDI
PYTHON TUPLE
output: (2,4,2,4)
Membership:
Membership refers to the concept of checking whether a value
belongs to a particular sequence or collection.
program:
a=(2,3,4,5,6,7,8,9,10)
5 in aTrue
100 in
aFalse 2 not
in a False
Comparison:
comparison refers to the process of evaluating the relationship
between two objects or values to determine their relative order or
equality. Python provides comparison operators to perform these
evaluations. The result of a comparison is a Boolean value (True or
False), indicating whether the comparison is true or false.
program:
a=(2,3,4,5,6,7,8,9,10)
b=(2,3,4)
Equal Equal (==) Example: a==b output:________
not Equal (!=) Example: a!=b output:________
less or Equal (<=) Example: a<=b output:________
Greater or Equal (>=) Example: a>=b output:________
4
KUMARAN.V MSC IT,GUMMIDIPOONDI