Define Duplicate Items in a Python Tuple



You can directly enter duplicate items in a Python tuple as it doesn't behave like a set(which takes only unique items). 

example

myTpl = (1, 2, 2, 2, 3, 5, 5, 4)

You can also use operators on tuples to compose large tuples.For Example

myTpl = (1,) * 5
print(myTpl)

Output

This will give the output

(1,1,1,1,1)

You can also join tuples using + operator. 

example

myTpl = (1,) * 3 + (2,) * 2
print(myTpl)

Output

This will give the output

(1,1,1,2,2)
Updated on: 2020-03-05T06:13:47+05:30

766 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements