How can I define duplicate items in a Python tuple?

PythonProgramming

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)
raja
Published on 18-Apr-2018 14:18:54
Advertisements