903
In this example we show you how to create a tuple in python
A tuple can have items of different data types like integers, floats, lists, strings, etc;
Example
There are 2 ways to create a tuple
- Use parenthesis (): A tuple is created by enclosing a comma separated items inside round brackets.
- Use a
tuple()
constructor: Create a tuple by passing the comma separated items inside thetuple()
.
number_tuple = (10, 20, 30) print(number_tuple) # string tuple string_tuple = ('Mercury', 'Venus', 'Mars') print(string_tuple) # mixed type tuple mixed_tuple = ('Mercury', 30, 40, [50, 60]) print(mixed_tuple) # create a tuple sample_tuple = tuple(('Venus', 30, 40, [50, 60])) print(sample_tuple)
When you run this you will see something like this
>>> %Run createtuple.py (10, 20, 30) ('Mercury', 'Venus', 'Mars') ('Mercury', 30, 40, [50, 60]) ('Venus', 30, 40, [50, 60])