Follow

python tuples

tuple is collection of multiple items in single variable.


tuple is ordered, duplicate and not-changeable(immutable) values.
tuple place all item(values) in parenthesis(),values separated with commas(,) tuple store multiple datatype of value

ordered

tuples are orderes

duplicate

tuple allow duplicate values

immutable

python tuple are can't changeable mean python tuple cannot change , add, remove etc.

Python tuple

x=("ankit", "manu", "sachin")
print(x)

Try it Yourself »

Output

('ankit', 'manu', 'sachin')

tuple datatype type()

check data type

type()

x=("ankit", "manu", "sachin")
print(type(x))

Try it Yourself »

Output

<class 'tuple'>

access tuples items

access items from tuple (tuple slicing).

slicing

x=("ankit", "manu", "sachin")
print(x[0])
#-1 indexing from last value
print(x[-1])

Try it Yourself »

Output

ankit
sachin

range of indexing

slicing of tuple
frist character have zero(0) index noumber

slicing

x=("ankit", "manu", "sachin")
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')

jion tuple

jion two or more than two tuple using (+)

jion tuple

x=("ankit", "manu", "sachin")
y=(1,2,3)
z=x+y
print( z)

Try it Yourself »

Output

('ankit', 'manu', 'sachin', 1, 2, 3)

index

get index number of tuple item

index

y=(1,2,3,4,5,6,7)
print( z. index(4))
print( z. index(1))

Try it Yourself »

Output

3
0

count

search item and count it.

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))

Try it Yourself »

Output

4
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.