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:
No comments: