0% found this document useful (0 votes)
24 views

Tuples

xx

Uploaded by

aviationforce204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Tuples

xx

Uploaded by

aviationforce204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Tuples are represented as group of comma-separated values of any date type within

parentheses, e.g., following are some tuples: p = (1, 2, 3, 4, 5)


q = (2, 4, 6, 8)
r= ('a', 'e', 'i', 'o', 'u')
h = (7, 8, 9, 'A', 'B', 'C')

NOTE
Values of type list are mutable i.e., changeable - one can change/add/delete a
list's elements. But the values of type tuple are immutable i.e.,non-changable; one
cannot make changes to a tuple.

The Tuples are depicted through parentheses i.e., round brackets, e.g., following
are some tuples in Python :
() (7,)
(1, 2, 3)
(1, 2.5, 3.7, 9)
('a', 'b', 'c')
('a', 1, "b', 3.5, 'zero')
('One', 'Two', 'Three')

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

Creating Tuples
To create a tuple, put a number of expressions, separated by commas in parentheses.
That is, to create a tuple you can write in the form given below:
T = ()
T = (value, ...)
This construct is known as a tuple display construct.

Creating Empty Tuple


The empty tuple is (). You can also create an empty tuple as:
T = tuple()

Creating Single Element Tuple


Making a tuple with a single element is tricky because if you just give a single
element in round brackets, Python considers it a value only, e.g.,
>>> t = (1)
>>> t
1

(1) was treated as an integer expression, hence t stores an integer 1, not a tuple.

To construct a tuple with one element just add a comma after the single element as
shown below:
>>> t = 3,
>>> t
(3,)
To create a one-element tuple, make
sure to add comma at the end
Now t stores a tuple, not integer.

Creating Tuples from Existing Sequences


You can also use the built-in tuple type object (tuple()) to create tuples from
sequences as per the syntax given below:
T= tuple(<sequence>)
where <sequence> can be any kind of sequence object including strings, lists and
tuples.

Consider following examples:


>>> t1 = tuple('hello')
>>> t1
('h', 'e', '1', '1', '0')
>>> L = ['w, 'e', 'r', 't', 'y']
>>>t2 = tuple(L)
>>>t2
('w, 'e', 'r', 't', 'y')

Creating Tuple from Keyboard Input


You can use this method of creating tuples of single characters or single digits
via keyboard input.
Consider the code below:
t1= tuple(input('Enter tuple elements:'))
Enter tuple elements: 234567
>>> t1
('2', '3', '4', '5', '6', '7')
But most commonly used method to input tuples is eval(input()) as shown below:
tuple = eval(input("Enter tuple to be added:")) print("Tuple you entered :", tuple)
when you execute it, it will work somewhat like:
Enter tuple to be added: (2, 4, "a", "hjkjl, "[3, 4]) Tuple you entered: (2, 4,
"a", "hjkjl", [3, 4])

Tuple Operations

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, as per following syntax:
for <item> in <Tuple>:
process each item here

For example, following loop shows each item of a tuple T in separate lines:
T= ('P', 'u', 'r', 'e')
for a in T:
print(T[a])
The above loop will produce result as:
P
u
r
e

Joining Tuples
The + operator, the concatenation operator, when used with two tuples, joins two
tuples. Consider the example given below:
>>>tpl1 = (1, 3, 5)
>>> tp12 = (6, 7, 8)
>>> tp11 + tp12
(1, 3, 5, 6, 7, 8)

Repeating or Replicating Tuples


Like strings and lists, you can use* operator to replicate a tuple specified number
of times,e.g.,
>>> tp11 * 3
(1, 3, 5, 1, 3, 5, 1, 3, 5)

Like strings and lists, you can only use an integer with a * operator when trying
to replicate a tuple.

Slicing the Tuples


Tuple slices, like list-slices or string slices are the sub part of the tuple
extracted out. You can use indexes of tuple elements to create tuple slices as per
following format:
seq= T[start: stop]

Recall that index on last limit is not included in the tuple slice. Consider the
following example:
>>> tpl = (10, 12, 14, 20, 22, 24, 30, 32, 34)
>>> seq=tpl [3:-3]
>>> seq
(20, 22, 24)

If you 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 per following format:
seq=T[start: stop: step]

Consider some examples to understand this.

>>> tpl
(10, 12, 14, 20, 22, 24, 30, 32, 34)
>>>tpl[0: 10: 2]
(10, 14, 22, 30, 34)
>>>tpl[2: 10:3]
(14, 24, 34)
>>> tpl[:: 3]
(10, 20, 30)

Unpacking Tuples
Creating a tuple from a set of values is called packing and its reverse, i.e.,
creating individual values from a tuple's elements is called unpacking.
Unpacking is done as per syntax:
<variable1>, <variable2>, <variable3>, ... =t

where the number of variables in the left side of assignment must match the number
of elements in the tuple.

For example, if we have a tuple as:


t = (1, 2, 'A', 'B')

The length of above tuple t is 4 as there are four elements in it. Now to unpack
it, we can write:
w, x, y, z=t
print(w,"-", x,"-",y,"-",z)
The output will be:
1-2-A-B

Tuple Functions
1. The len() method
This method returns length of the tuple, i.e., the count of elements in the tuple.
Syntax:
len(<tuple>)
>>> employee("John', 10000, 24, 'Sales') >>> len(employee)
2. The max() method
The len() returns the count of elements in the tuple
This method returns the element from the tuple having maximum value. Syntax:
max(<tuple>)
>>>tpl = (10, 12, 14, 20, 22, 24, 30, 32, 34)
>>> max(tpl)
34
3. The min() method
Maximum value from tuple ipl is returned
This method returns the element from the tuple having minimum value.
Syntax
min(<tuple>)
>>> tpl (10, 12, 14, 20, 22, 24, 30, 32, 34)
>>> min(tpl)

4. The index() method


It returns the index of an existing element of a tuple.
Syntax:
<tuplename>.index(<item>)
>>> t1 = [3, 4, 5, 6.0]
>>> t1.index(5)
2
But if the given item does not exist in tuple, it raises ValueError exception.

5. The count() function


The count() method returns the count of a member element/object in a given sequence
(list/tuple). Syntax:
<sequence name>.count(<object>)
>>> t1 = (2, 4, 2, 5, 7, 4, 8, 9, 9, 11, 7, 2)
>>> t1.count(2)
3

6. The tuple() method


This method is actually constructor method that can be used to create tuples from
different types of values.
Syntax :
tuple(<sequence>)
Creating empty tuple >>> tuple()
()
Creating tuple from a string
>>> t = tuple("abc")
>>>t
('a', 'b', 'c')
<>Creating a tuple from a list
>>> t = tuple([1,2,3])
››› t
(1, 2, 3)
Creating a tuple from keys of a dictionary
>>> t1 = tuple ( {1:"A", 2:"B"})
>>> t1
(1, 2)

You might also like