Tuples are used to store multiple items in a single variable.

A Tuple is a collection which is ordered , unchangeable, and allow duplicate items.

Tuple are written with round brackets .

Tuple items are ordered, unchangeable, and allow duplicate values.

	
	my_tuple =('iphone','samsung','lg')
	
	print(my_tuple)
	

create an empty Tuple:

	
	my_tuple= ()
	
	len(my_tuple)  => 0
	

create Tuple with One Item:

	
	my_tuple= ('iphone',)	# Note a comma after the object.
	
	len(my_tuple)  => 1 
	

The tupple() Constructor :

	
	my_tuple =tuple(('iphone','samsung','lg')) 		# Note a double rounded brackets
	
	print(my_tuple)
	

Tuples Properties :


Tuple Length

len() function is use to determine the length of a Tuple.

	
	my_tuple = ('iphone','samsung','lg')
	
	print(len(my_tuple))
	

Accessing Tuple Values:

Tuples support indexing and slicing same as String and List and It can be nested as well.

	
	x = my_tuple[-1]		#indexing
	
	print(my_tuple[1:3])	#slicing
	

Change Tuple Values

Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called.

But there is a workaround. We can convert the tuple into a list, change the list, and convert the list back into a tuple.

		
	
	my_tuple = ('iphone', 'samsung', 'lg')
	
	x = list(my_tuple)
	
	x[1] = "Motorola"
	
	my_tuple = tuple(x)
	
	print(my_tuple)
	

Add Items to Tuple

Since tuples are immutable, they do not have a build-in append() method, but there are other ways to add items to a tuple.

Remove Items

As above for Remove item(s) we need to convert the tuple into a list, remove item(s) and convert it back into a tuple.

The del keyword delete the Tuple completely:

	
	 del my_tuple	 
	 

Unpacking a Tuple

When we create a tuple, we normally assign values to it. This is called "packing" a tuple.

But, in Python, we can also extract the values from tuple into variables. This is called ""unpacking""

	
	fruits = ("apple", "banana", "cherry")

	(green, yellow, red) = fruits

	print(green)
	
	print(yellow)
	
	print(red)
	

If the number of variables is less than the number of values, we can add an * to the variable name and the values will be assigned to the variable as a list. and If the asterisk is added to another variable name than the last, Python will assign values to the variable until the number of values left matches the number of variables left.

	
	fruits = ("mango", "Lichi", "apple", "banana", "cherry")

	(orange, *yellow, green) = fruits

	print(green)
	
	print(yellow)
	
	print(red)
	

Loop Through a Tuple

Join Tuples

Multiply Tuples

Tuple Methods