Tuples in Python



Tuples are a special data structure similar to the list data structure. If we relate tuple to list, tuples are faster because of nature of tuples as immutable.



Syntax
tpl1 = (1, 2, 3)

ex:

tpl1 = (1, 2, 3)
print tpl1

tpl1 = 100, 20,30,'Ram','hello!'
print tpl1

print tpl1[0]
len(tpl1)

tpl1.index(30)
print tpl1[2:3]


Tuple Manipulation functions
( )                                   An empty tuple
tpl1 = (0,)                       A one-item tuple
tpl2 = (0, 'ax', 1, 3)        A four-item tuple
tpl1[i]                                       Indexing
tpl1[i:j]                          slicing
len(tpl1)                         length
tpl1 + tpl2                      Concatenation
tpl2 * 3                          repetition
for x in tpl2                     Iteration
tpl1.count(1)                  Occurrence of numbers in tuple
tpl1.index(2)                 Index number of the value “2” in tuple

Tuples in Python Reviewed by Rupesh on 11:41 Rating: 5

No comments:

All Rights Reserved by Technology from Developers Eye © 2014 - 2015
Powered By Blogger, Designed by Aadics
Disclaimers:: The information provided within this blogsite is for general informational purposes only. While we try to keep the information up-to-date and correct, there are no representations or warranties, express or implied, about the completeness, accuracy, reliability, suitability or availability with respect to the information, products, services, or related graphics contained in this blogsite for any purpose.The author does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause.

Contact Form

Name

Email *

Message *

Powered by Blogger.