Python Tuples

Python Tuples

June 14, 2018
2 minutes read time
1 likes
1 comments
335 times viewed


header image

Description

This article will guide you about the use of tuples in python. It will explain various functionalities and builtin functions associated with it.

Content

Tuple

Basic Tuple Operations

Concatenation

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

Repetition

>>> ("kode", )*4
('kode', 'kode', 'kode', 'kode')

Membership

>>> 3 in (1, 2, 3, 4, 5)
True

Indexing

>>> T=('C++', 'Java', 'Python')
>>> T[2]
'Python'
>>> T[-2]
'Java'

Slicing

>>> T = (1, 2, 4, 7, 3, 9)
>>> T[2:4]
(4, 7)
>>> T[2:]
(4, 7, 3, 9)
>>> T[:2]
(1, 2)
>>> T[:]
(1, 2, 4, 7, 3, 9)

Tuple Builtin Functions

len(tuple)

Gives the total length of the tuple.

>>> tuple1, tuple2 = (123, 'xyz', 'zara'), (456, 'abc')
>>> len(tuple1)
3
>>> len(tuple2)
2

max(tuple)

Returns item from the tuple with max value.

>>> tuple1, tuple2 = ('maths', 'che', 'phy', 'bio'), (456, 700, 200)
>>> max(tuple1)
'phy'
>>> max(tuple2)
700

min(tuple)

Returns item from the tuple with min value

>>> tuple1, tuple2 = ('maths', 'che', 'phy', 'bio'), (456, 700, 200)
>>> min(tuple1)
'bio'
>>> min(tuple2)
200

tuple(s)

Converts s to a tuple.

>>> list1= ['maths', 'che', 'phy', 'bio']
>>> tuple(list1)
('maths', 'che', 'phy', 'bio')
>>> tuple("astik")
('a', 's', 't', 'i', 'k')


We are done



Like this post

1   Like


Share this post on

Google Plus LinkedIn


About the author




Join the discussion

Nothing to preview

Post Comment



Comments


sureshbesant
1 year, 1 month ago

I ‘d mention that most of us visitors are endowed to exist in a fabulous place with very many wonderful individuals with very helpful things.

[python training](http://<a href="https://www.besanttechnologies.com/python-training-in-electronic-city">python  Training in Bangalore </a>