Chapter 12 - Tuples
Chapter 12 - Tuples
Chapter 12 - Tuples
1
12.1 TUPLES IN PYTHON:
• Example:
• Tuples are immutable sequences, you cannot change elements of a tuple in place.
• Syntax;
T=()
T = ( value, …..)
• Syntax:
T= tuple ( )
• Making a tuples with a single element is tricky because if we just give a single
element in round bracket, python considers it a value only.
• Example:
t = (1)
t
2
Creating tuples from existing sequences:
• We can use the built-in tuple type object to create tuples from sequences.
• Syntax:
• We can use this method of creating tuples of single characters or single digits
through keyboard.
• Syntax:
Tuples vs Lists
• Indexing and slicing: T[i] returns item at index i, and T[i: j] returns a new list, con-
taining the objects between i and j.
• Membership operators: both in and not in operators work on tuples also. That is,
it tells if an element is present in the tuple or not and not in does the opposite.
• Concatenation and replication operators + and *: the + operator adds one tuple
to the end of another. The *operator repeats a tuple.
3
Difference between tuples and lists:
• Tuples are not mutable, while lists are. We cannot change individual elements of
a tuple in place, but lists allow to do so.
Traversing a tuple:
• Traversing a tuple means accessing and processing each element of it. The for
loop makes it easy to traverse or loop over the items in a tuple.
• Syntax:
for <item> in <Tuple>
Process each item here
Tuples in Python
• Tuple operation
• Tuple functions and methods
Tuple operations
Joining tuple:
• The + operator, the concatenation operator, when used with two tuples, joins two
tuples.
• Example:
>>>tp1 = (1, 3, 5)
>>>tp2 = (6, 7, 8)
>>>tp1 + tp2
• If we want to extract, not consecutive but every other element of the tuple, there
is a way out the slice steps. The slice steps are used as following:
seq = T[start : stop : step]
4
• Example:
tp1 = (10, 12, 14, 20, 22, 24, 30, 32, 34)
seq = tp1[3:-3]
seq
(20, 22, 24)
Comparing Tuples
• We can compare two tuples without having to write code with loops for it.
• For comparing two tuples, we can use comparasion operators. For comparison pur-
pose, Python internally compares individual elements of two tuples.
• Example:
a = (2, 3)
b = (2, 3)
a ==b
True
Unpacking tuples:
• Creating a tuple from a set of values is called packing and its reverse, creating indi-
vidual values from a tuples element is called unpacking.
• Syntax:
5
Deleting Tuples
• The del statement of python is used to delete elements and objects, but as tuples are
immutable, which also means that individuals elements of a tuple cannot be deleted.
• Syntax:
del<tuple_name>
• Example:
delt1[2]
• This method returns length of the tuple, the count of elements in the tuple.
• Syntax:
Len( <tuple>)
• This method returns the element from the tuple having maximum value.
• Syntax:
Max( <tuple>)
• This method returns the element from the tuple having minimum values.
• Syntax;
Min( <tuple>)
6
The index( ) method:
• Syntax:
<tuplename>.index(<item>)
• The count method returns the count of a member element/ object in a given se-
quence.
• Syntax:
<sequence name>.count(<object>)
Tuple( ) method
• This method is constructor method that can be used to create tuples from different
types of syntax.
• Syntax:
tuple(<sequence>)