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




PYTHON - JOIN TWO TUPLES


How to join two Tuples?


Let us say, we have a Tuple that contains three names, 'Mohan', 'Kriti' and 'Salim'. And we have a second Tuple with two names 'Sia' and 'Andrew'. And we want to join them.


It can be achieved by using the '+' operator


Example :


x = ("Mohan", "Kriti", "Salim")
y = ("Sia", "Andrew")
z = x + y
print(z) 


Output :



  ('Mohan', 'Kriti', 'Salim', 'Sia', 'Andrew')

So, in the above code we have created a 'Tuple' and initialised to the variable 'x'.


x = ("Mohan", "Kriti", "Salim")

Below is how the values are positioned in the Tuple,


java_Collections

Also we have another Tuple that contains, 'Sia' and 'Andrew'.


y = ("Sia", "Andrew")

java_Collections

Next, we have used the '+' operator to add the the Tuples 'x' and 'y'.


z = x + y

And the Tuples 'x' and 'y' is joined and initialised to a new Tuple 'z'.


java_Collections

And we get the below output,


('Mohan', 'Kriti', 'Salim', 'Sia', 'Andrew')

How to repeat the Tuple elements?


Let us say, we have a 'Tuple' that contains three names, 'Mohan', 'Kriti' and 'Salim'. And we want to repeat the names two times and assign to another 'Tuple'.


It can be achieved by using the '*' operator


Example :


x = ("Mohan", "Kriti", "Salim")
y = x * 2
print(y) 


Output :



  ('Mohan', 'Kriti', 'Salim', 'Mohan', 'Kriti', 'Salim')

So, in the above code we have created a 'Tuple' and initialised to the variable 'x'.


x = ("Mohan", "Kriti", "Salim")

Below is how the values are positioned in the Tuple,


java_Collections

Next, we have used the '*' operator to repeat each element of the Tuple 'x' two times.


y = x * 2

And the elements of the Tuple 'x' is repeated 2 times and initialised to a new Tuple 'y'.


java_Collections

And we get the below output,


('Mohan', 'Kriti', 'Salim', 'Sia', 'Andrew')

Where 'Mohan', 'Kriti' and 'Salim' is repeated twice.