tuple is collection of multiple items in single variable.
ordered
duplicate
immutable
Output
('ankit', 'manu', 'sachin')
tuple datatype type()
Output
<class 'tuple'>
access tuples items
slicing
x=("ankit",
"manu",
"sachin")
print(x[0])
#-1 indexing from last value
print(x[-1])
print(x[0])
#-1 indexing from last value
print(x[-1])
Try it Yourself »
Output
ankit
sachin
sachin
range of indexing
slicing
x=("ankit",
"manu",
"sachin")
print(x[0:2])
print(x[-3:-1])
print(x[1:])
print(x[:3])
print(x[0:2])
print(x[-3:-1])
print(x[1:])
print(x[:3])
Try it Yourself »
Output
('ankit','manu')
('sachin','manu')
('manu',)
('ankit','manu','sachin')
('sachin','manu')
('manu',)
('ankit','manu','sachin')
jion tuple
Output
('ankit', 'manu', 'sachin', 1, 2, 3)
index
Output
3
0
0
count
count
y=(1,2,3,1,1,6,7,6,2,1)
#count number of value in item
print( z. count(1))
print( z. count(7))
#count number of value in item
print( z. count(1))
print( z. count(7))
Try it Yourself »
Output
4
1
1
Python arrays
- Tuple:tuple is written in Parentheses (), collection of immutable and ordered values. allow duplicate.
- List:tuple is written in square brackets [], collection of mutavle and ordered values. allow duplicate.
- Dict:tuple is written in curly brackets {}, collection of unordered and mutable values. not allow duplicate.
- Set:tuple is written in curly brackets {}, collection of unordered , unindexed and immutable values. not allow duplicate.
No comments:
Post a Comment
Tell us how you like it.