Python is an object-oriented programming language ideally designed for rapid prototyping of complex applications. Like Perl, Python source code is also available under the GNU General Public License (GPL).

In this article we will see how to access, add, replicate and updating Tuples in Python with examples.

1) What are Tuples in python ?

A tuple is similar to a list with a difference that one cannot change the elements of a tuple once it is assigned whereas in a list, elements can be changed. Tuples makes the code safer if one wants ?write-protect? data that does not need to be changed.

2) Define Tuples in python

The main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated.
tuple = ( 'tbNext', 786 , 2.23, 'Python', 70.2  )
print tuple
Output: Output will be something like this:

('tbNext', 786, 2.23, 'Python', 70.2)


3) What is Difference between python tuple and list ?

Tuples are fixed size and immutable in nature whereas lists are dynamic and mutable. Tuples are faster than lists, while defining a constant set of values and using only to iterate through it, use a tuple instead of a list.

Some tuples can be used as dictionary keys (specifically, tuples that contain immutable values like strings, numbers, and other tuples) while lists can never be used as dictionary keys because of their non immutability.
# Defing a list
list = [ 'tech', 786 , 2.23, 'burps', 70.2 ]
print list

# Defing a tuple
tup = ( 'tech', 786 , 2.23, 'burps', 70.2);
print tup
Output: Output will be something like this:

['tech', 786, 2.23, 'burps', 70.2]
('tech', 786, 2.23, 'burps', 70.2)


4) Packing and Unpacking in Python tuple ?

In packing, we place value into a new tuple while in unpacking we extract those values back into variables from the tuple.
# packing and unpacking in tuples
tup = ("Tech", 20, "Site")
(name, age, type) = tup
print name
print age
print type
Output: Output will be something like this:

Tech
20
Site


5) Comparing tuples in Python

Comparison operators in Python can work with tuples, it starts with comparing the first element from each of the tuples upto the end based on index from both comparing tuples.

As soon as t1[i] and t2[i], determines the result comparison stops:
tup1 = (1,2,10)
tup2 = (2,3,1)

print tup1 < tup2
print tup1 == tup2
print tup1 > tup2
Output: Output will be something like this:

True
False
False


6) Using tuples as keys in python dictionaries

Tuples can be used as a key if we need to create a composite key to use in a dictionary.
# Define tuples
tup1 = (8787877676,'Andrew')
tup2 = (9878767656,'Jonty')

# Define map with tuple as a key
map = {tup1:"Tech",tup2:"Burps"}

# Get values from map with tuple as a key
print(map[tup1])
print(map[tup2])
Output: Output will be something like this:

Tech
Burps


7) Slicing of python tuples

Specific sets of sub-elements can be obtained from tuples or list:
# Define tuples
tup1 = (1,2,3,4,5)

print(tup1[2:4])
Output: Output will be something like this:

(3, 4)


In this article we have seen, how to access, add, replicate and updating Tuples in Python with examples. In upcoming articles we will see more complex use cases and tricks of python.
  • By Techburps.com
  • Sep 30, 2017
  • Python