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

TUPLE

Python

Uploaded by

sifani0928
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

TUPLE

Python

Uploaded by

sifani0928
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Tuple Definition:

1. A tuple is a sequence of immutable objects. That is, you can change the value
of one or more items in a list; you cannot change the values in a tuple.

2. Tuples use parenthesis to define its elements. Whereas lists use square brackets.

Creating a Tuple:

Creating a tuple is as simple as putting different comma-separated values.


Optionally you can put these comma-separated values between parentheses.

Syntax: Tup1=(val1,val2,….)

Where val (or values) can be an integer, a floating number, a character, or a string.

Examples:

1) Tup1=( )
#creates an empty tuple.
print(Tup1)

output:

2) Tup1=(5) #creates a tuple with single


element print(Tup1)

Output:

3) Tup1=(1,2,3,4,5)
#creates a tuple of integers print (Tup1)

Tup2=(„a‟,‟b‟,‟c‟,‟d‟)

#creates a tuple of characters print(Tup2)

Tup3=(“abc”,”def”,”ghi”)#creates a tuple of

strings print(Tup3)
Tup4=(1.2,2.3,3.4,4.5,5.6)

#creates a tuple of floating point numbers print(Tup4)

Tup5=(1,”abc”,2.3,‟d‟)

#creates a tuple of mixed values print(Tup5)

Output:

1,2,3,4,5

„a‟,‟b‟,‟c‟,‟d‟

„abc‟,‟def‟,‟ghi‟

1.2,2.3,3.4,4.5,5.6

1,‟abc‟,2.3,‟d‟

4. A Tuple with parenthesis


Print( “a”,”bcd”,2,4.6)
Output:
A bcd2 4.6

5. Default Tuple without parenthesis


a,b=10,20
print(a,b)
Output: 10 20

Accessing values of tuples:

 Like strings and lists tuples indices also starts with 0.


 The operations performed are slice, concatenate etc.,
 To access values in tuple, slice operation is used along with the index.

Example :

1) Tup1=(1,2,3,4,5,6,7,8,9,10)

print(“Tup[3:6]=”,Tup1[3:6])

print(“Tup[:8]=”,Tup1[:4])
print(“Tup[4:]=”,Tup1[4:])

print(“Tup[:]=”,Tup1[:])

Output:

Tup[3:6]=(4,5,6)

Tup[:8]=(1,2,3,4)

Tup[4:]=(5,6,7,8,9,10)
Tup[:]=(1,2,3,4,5,6, 7,8,9,10)

The tuple values can be accessed using square brackets:

1) Tuple =(1,2,3,4,5.5,‟str‟)

Input:

1. print tuple

2. print tuple[5]

3. print tuple[1:5]
Output:

1.1,2,3,4,5.5,‟str‟

2.‟str‟
3.2,3,4,5.5

Updating tuples:

As we all know tuples are immutable objects so we cannot update the values but
we can just extract the values from a tuple to form another tuple.

Example:
1) Tup1=(1,2,3,4,5)
Tup2=(6,7,8,9,10)
Tup3=Tup1+Tup2
print(Tup3)
Output
(1,2,3,4,5,6,7,8,9,10)
2) Tup1=(1,2,3,4,5)

Tup2=(„sree‟,‟vidya‟,‟ram‟)
Tup3=Tup1+Tup2
print(Tup3)

Output:

(1,2,3,4,5,‟sree‟,‟vidya‟,‟ram‟)

Deleting elements of a tuple:

1. Deleting a single element in a tuple is not possible as we know tuple is a


immutable object.

Hence there is another option to delete a single element of a tuple i.e..,you can
create a new tuple that has all elements in your tuple except the ones you don‟t
want.

Example:

1) Tup1=(1,2,3,4,5)

del Tup1[3]

print Tup1

Output:

Traceback (most recent call last):


File "test.py", line 9, in <module>

del Tup1[3]

Type error: „tuple‟ object doesn‟t support item deletion

2) however, you can always delete the entire tuple by using del statement.

Tup1=(1,2,3,4,5)

del Tup1
print Tup1

Output:

Traceback (most recent call


last): File "test.py", line 9, in
<module> print Tup1;
NameError: name 'Tup1' is not defined

Basic tuple operations:

Like strings and lists,you can also perform operations like concatenation,
repetition,etc. on tuples. The only difference is that a new tuple should be
created when a change is required in an existing tuple.

Operation Expression Output

Length len((1,2,3,4,5,6)) 6

Concatenation (1,2,3)+(4,5,6) (1,2,3,4,5,6)

Repetition („Good..‟)*3 „Good ..Good..Good‟

Membership 5 in (1,2,3,4,5,6,7,8,9) True

Iteration for i in 1,2,3,4,5,6,7,8,910


(1,2,3,4,5,6,7,8,9,10):

print(i,end=‟ „)

Comparision(Use Tup1=(1,2,3,4,5) False


>,<,==)
Tup2=(1,2,3,4,5)

print(Tup1>Tup2)

Maximum max(1,0,3,8,2,9) 9

Minimum min(1,0,3,8,2,9) 0

Convert to tuple(“Hello”) („H‟,‟e‟,‟l‟,‟l‟,‟o‟)


tuple(converts a
sequence into a tuple) tuple([1,2,3,4,5]) (1,2,3,4,5)

Sorting( The sorted( ) t=(4,67,9) [4, 9, 67]


function takes elements
in a tuple and returns a sorted(t)
new sorted list (does
not sort the tuple
itself)).

1) Length of the tuple:

Ex:

Input:

Tup1= (1,2,3,4,5)

print len (Tup1)

Output:

2) Concatenation:

Ex:

Input:

Tup1=(1,2,3,4
)

Tup2=(5,6,7)

print tup1+tup2

Output:

(1,2,3,4,5,6,7)
3) Repetition:

Ex:

Input:

Tuple1=(„my‟)

print tuple1*3

Output:

(„my‟,‟my‟,‟my‟)

4.Membership:

Ex:

Input:

Tuple1=(1,2,3,4,6,
7)

5.Iteration:

Ex:

Input:

For i in (1,2,3,4,5,6,7,8,9,10):

print (i,end=‟ „)

Output:

1,2,3,4,5,6,7,8,9,10

6.Comparison:

Ex:

Input:

Tup1 = (1,2,3,4,5)

Tup2 =(6,7,8,9,10)
print(Tup1<tup2)

Output:

True

7. Maximum:

Ex:

Input:

Max(1,2,6,5,4
)

Output:

8. Minimum:

Ex:

Input:

Min(1,2,3,4,5
)

Output:

Convert to tuple:

Ex:

Input:

Tuple(“vidya”)

Output: („v‟,‟i‟,‟d‟,‟y‟,‟a‟)

You might also like