Python Program to create a Tuple using tuple literal


In Python, a tuple is an immutable sequence type that is commonly used to store a collection of items.

A python tuple is very similar to a list in python in terms of nesting, indexing and repetition but one difference is that a tuple is immutable whereas the list is mutable which means we can change the elements of a list but we cannot do the same with a tuple. One other difference is that list uses the square bracket [] for declaration but tuples use simple bracket ().

In this article, we will be discussing how to create a tuple using a tuple literal in Python. We will go over the syntax for defining a tuple and will provide examples of how to create tuples using different types of elements.

We can declare a tuple in the following way −

tup1 = ("this" , "is" , “a” , "tuple")
tup2 = (1, 2, 3, 4, 5 )
tup3 = ("a", "b", "c", "d")

There are different ways we can create a tuple but, in this article, we will see how we can create a tuple using tuple literal.

Using Tuple Literal

Before using the tuple literal to create a tuple literal let us learn what do we mean by a literal in python. The data that is assigned to variables and constants while we write the codes is called literals.

The values of these literals do not get changed during the entire execution of the program. There are many types of literals that are available in python, but if we have to refer to a collection type object like a list, tuple, or a dictionary then we have to use what we call a “literal collection”.

A tuple literal is an example of a literal collection and that can be used to create a tuple object in python.

Example

In the following example, we will be creating a tuple by declaring it using brackets and commas.

tuple1 = ( 1 , 2 , 3 , 4 , 5 , 6 , 7 )
print("the tuple is", tuple1 )

Output

The output for the above code is as follows −

the tuple is ( 1 , 2 , 3 , 4 , 5 , 6 , 7 )

Using Another List

We will see how to create a tuple using a form another list. This method is also called type conversion. Essentially what we are doing is creating a list type object and converting it to a tuple type object. Using this method is an unconventional way of creating a list but is mentioned in this article as an alternative method in case you need one.

Example

In the following example, we will be creating a list from another list. The tuple created will have the same elements as the list except it cannot be changed. We can do this by using tuple() method that is a built-in method provided by python that will convert the data type inside of it into a tuple.

List1 = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]
tuple1 = tuple(List1)
print("the tuple is", tuple1 )

Output

The output of the above program is as follows −

the tuple is ( 1 , 2 , 3 , 4 , 5 , 6 , 7 )

Creating Tuple From a String

We will see how to create a tuple using a form string. By this method all the characters of the string will be different elements of the tuple created. We can do this by using tuple() function it will convert the data type inside of it into a tuple.

Example

In the following example, we will be creating a list from a string. By this method all the characters from the string will be the elements of the tuple in the order, in which they are present in the string.

string1 = "This is a tuple"
tuple1 = tuple(string1)
print("the tuple is" , tuple1 )

Output

The output of the above program is as follows −

the tuple is ( ‘T’ , ‘h’ , ‘i’ , ‘s’ , ‘ ’ , ‘i’ , ‘s’ , ‘ ’ , ‘a’ , ‘ ’ , ‘t’ , ‘u’ , ‘p’ , ‘l’ , ‘e’ )

Conclusion

In this article, we discussed about tuples how we can create a tuple and what are its properties. We saw 3 different ways to create a tuple. In the very first method we created a tuple by using brackets and the elements are separated by commas. In the second method we created a tuple from a list, in this method all the elements of the tuple are the same as the elements of the list from which it is created. In the third method we created the tuple from a string. All the characters of the string are individual elements of the tuple.

Updated on: 17-Feb-2023

535 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements