Chapter 12 - Tuples

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

LESSON 12 : TUPLES

1
12.1 TUPLES IN PYTHON:

• The tuples are depicted through parentheses, round bracket.

• Example:

() #tuple with no member, empty tuple

(7, ) #tuple with one member

(1, 2, 3) #tuples with integer, etc.,,

• Tuples are immutable sequences, you cannot change elements of a tuple in place.

12.2 CREATING TUPLE

• To create a tuple, put a number of expressions, separated by commas in


parentheses.

• Syntax;

T=()

T = ( value, …..)

Creating empty tuple:

• The empty tuple is ( ).

• Syntax:

T= tuple ( )

Creating single element 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:

T = tuple( < sequence> )

Creating tuple from keyboard:

• We can use this method of creating tuples of single characters or single digits
through keyboard.

• Syntax:

T1 = tuple( input(‘enter tuple element: ’) )

Tuples vs Lists

Similarity between tuple and lists:

• Length: function len(T) returns the number of items in the tuple T.

• 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.

• Accessing individual elements: the individual elements of a tuples are accessed


through their indexes given in the square bracket.

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

Slicing the tuple:


• Tuple slice, like list-slice or string slices are the sub part of the tuple extracted out.
• Syntax:
seq = T[start : stop]

• 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:

<variable1>, <variable2>, <variable3>, ….. = t

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]

12.4 Tuple functions and methods

The len( ) method:

• This method returns length of the tuple, the count of elements in the tuple.

• Syntax:

Len( <tuple>)

The max( ) method:

• This method returns the element from the tuple having maximum value.

• Syntax:

Max( <tuple>)

The min( ) method:

• This method returns the element from the tuple having minimum values.

• Syntax;

Min( <tuple>)

6
The index( ) method:

• It returns the index of an existing element of a tuple.

• Syntax:

<tuplename>.index(<item>)

The count( ) method:

• 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>)

You might also like