Home ยป Creating a Tuple in Python

Creating a Tuple in Python

Java SE 11 Programmer II [1Z0-816] Practice Tests
Java SE 11 Developer (Upgrade) [1Z0-817]
1 Year Subscription
Java SE 11 Programmer I [1Z0-815] Practice Tests
Spring Framework Basics Video Course
Oracle Java Certification

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

  1. Use parenthesis (): A tuple is created by enclosing a comma separated items inside round brackets.
  2. Use a tuple() constructor: Create a tuple by passing the comma separated items inside the tuple().
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])

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More