Home >Backend Development >Python Tutorial >Create a Python program using tuple literals
In Python, a tuple is an immutable sequence type typically used to store a collection of items.
Python tuples are very similar to Python lists in terms of nesting, indexing, and repetition, but one difference is that tuples are immutable, while lists are mutable, which means we can change the elements of the list, but not Do the same with tuples. Another difference is that lists are declared using square brackets [] while tuples are declared using simple brackets ().
In this article, we will discuss how to create tuples in Python using tuple literals. We'll introduce the syntax for defining tuples and provide examples of creating tuples using elements of different types.
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 many ways to create tuples, but in this article, we will see how to create tuples using tuple literals.
Before using tuple literals to create tuple literals, let us first understand the meaning of literals in Python. The data we assign to variables and constants when we write code is called literals.
The values of these literals will not change during the entire execution of the program. There are many types of literal in Python, but if we have to refer to a collection type object, such as a list, tuple or dictionary, then we have to use a so-called "Literal Collection".
A tuple literal is an example of a collection of literals that can be used to create a tuple object in Python.
In the following example, we will create a tuple using parentheses and comma declarations.
tuple1 = ( 1 , 2 , 3 , 4 , 5 , 6 , 7 ) print("the tuple is", tuple1 )
The output of the above code is as follows -
the tuple is ( 1 , 2 , 3 , 4 , 5 , 6 , 7 )
We will see how to create a tuple using another list. This method is also called type conversion. Essentially, what we do is create an object of type list and convert it to an object of tuple type. Using this method is an unconventional way to create a list, but is mentioned in this article as an alternative in case you need it.
In the example below, we will create a list from another list. The tuple created will have the same elements as the list, but cannot be changed. We can do this by using the tuple() method, which is a built-in method provided by python to convert the data type in it to a tuple.
List1 = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] tuple1 = tuple(List1) print("the tuple is", tuple1 )
The output of the above program is as follows -
the tuple is ( 1 , 2 , 3 , 4 , 5 , 6 , 7 )
We will see how to create tuples using form strings . With this approach, all characters of the string will be distinct elements of the created tuple. We can do this by using the tuple() function, which will convert the data type in it to a tuple.
In the example below, we will create a list from strings . With this method, all characters in String will become elements of the tuple in the order in which they appear in String.
string1 = "This is a tuple" tuple1 = tuple(string1) print("the tuple is" , tuple1 )
The output of the above program is as follows -
the tuple is ( ‘T’ , ‘h’ , ‘i’ , ‘s’ , ‘ ’ , ‘i’ , ‘s’ , ‘ ’ , ‘a’ , ‘ ’ , ‘t’ , ‘u’ , ‘p’ , ‘l’ , ‘e’ )
In this article, we discussed how tuples are created and their properties. We saw 3 different ways of creating tuples. In the first method, we create a tuple using parentheses with commas separating the elements. In the second method, we create a tuple from the list, in this method all the elements of the tuple are the same as the elements of the list from which it was created. In the third method, we create tuples from strings. All characters of a string are elements of a tuple.
The above is the detailed content of Create a Python program using tuple literals. For more information, please follow other related articles on the PHP Chinese website!