Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - TUPLES


A 'Tuple' is a Collection that holds multiple values, of different Data Types. In a 'Tuple' the elements are ordered, the values are unchangeable and allows duplicate values.


The declaration of a 'Tuple' 'Data Type' is quite easy. You can place multiple values inside brackets '( )' and Python will understand that it is a 'Tuple'.


Creating a 'Tuple' with values of different Data Types


Example :


x = (5, "John", "Python")
print(x)     


Output :



  (5, 'John', 'Python')

So, in the above code we have created a 'Tuple' using brackets '( )'.


And put an Integer type value (i.e. 5) and two String type value (i.e. 'John' and 'Python')


x = (5, "John", "Python")

And initialised to the variable 'x'.


java_Collections

So, we can see that two different data types are assigned to a 'Tuple'.


In the next line we have printed the 'Tuple' using the print statement.


print(x)

Now, if we see the output,



Output :



  (5, 'John', 'Python')

Creating a 'Tuple' with one element


So, we are trying to create a 'Tuple' with one element i.e. 'John'.


Let us look at the below example(Well! That shows the wrong way of creating a Tuple with one element).


Example :


x = ("John")
print(type(x))
print(x) 


Output :



  <class 'str'>
  John

So, in the above code we have 'tried' creating a 'Tuple' using brackets '( )'.


And put the name 'John' in the variable 'x'.


x = ("John")

And initialised to the variable 'x'.


java_Collections

But if you look at the Data Type of 'x'.


print(type(x))

It is of String type and not Tuple.


<class 'str'>

And if you see the output,


John

It does not have the brackets '( )' around it.


So, with the below line,


x = ("John")

Although we have used Brackets '( )' to create a 'Tuple', still a String type variable is created.


So, that's a wrong way.


Let us correct the above example.


Example :


x = ("John",)
print(type(x))
print(x) 


Output :



  <class 'tuple'>
  ('John',)

So, in the above code, all we have done is, created a 'Tuple' using brackets '( )'.


And since, there is just one element, we have put a comma ',' after the name 'John'.


x = ("John",)

And initialised to the variable 'x'.


java_Collections

But if you look at the Data Type of 'x'.


print(type(x))

It is of String type and not Tuple.


<class 'tuple'>

And it is a 'Tuple' now.


Note : Just remember, if there is just one element in a tuple, just put a comma (i.e. ',') after that 'x = ("John",)'. And this rule is applicable if the tuple has only one element.

Iterating a Tuple using 'for loop'


Example :


x = (5, "John", "Python")
for i in x:
    print(i)


Output :



  5
  John
  Python

Similarly, in the above code we have created a 'Tuple' using brackets '( )'.


x = (5, "John", "Python")

And initialised to the variable 'x'.


java_Collections

In the next line we have used the 'for loop' to Iterate through the 'Tuple'.


for i in x:
    print(i)

Now, if we see the iterations of for loop,


for i in x:

1st Iteration


In the first Iteration the first value of the 'Tuple' 'x' (i.e. 5) is taken and put into the variable 'i'.


java_Collections

And the print statement, prints the value of 'i'.



Output :



  5

2nd Iteration


Similarly, in the second Iteration the second value of the 'Tuple' 'x' (i.e. 'John') is taken and put into the variable 'i'.


java_Collections

And the print statement, prints the value of 'i'.



Output :



  5
  John

3rd Iteration


Similarly, in the third Iteration the third value of the 'Tuple' 'x' (i.e. 'Python') is taken and put into the variable 'i'.


And the print statement, prints the value of 'i'.


Output :



  5
  John
  Python

Now, if you see the final output. You can find that the values of the 'Tuple' are displayed in the same way they were inserted.


i.e. First '5' is printed, then the name 'John' and finally 'Python' is printed.


And if you see the Tuple,


x = (5, "John", "Python")

It is printed in the same order it was inserted. And this is why a Tuple is said to be 'Ordered'.


Next, let us see, how to access the elements of the Tuple.