# Tuples | Python Programming Language

Tuples are sequences which are immutable, hold data inside circular brackets and can contain any type of objects. For example – (10, 29, 39) or (82, 29, [19, 37], “ComputerScienceHub”) both of these are tuples.
Below are some examples of Python Tuples.

``````x = (37, 28, 21, 39)
y = ("Python", "JavaScript", "C#")
z = ("Hello", 29, 46, "How are you?")
# All of these are tuples in Python``````

## 3 Ways to create tuples in Python

Creating tuples in Python is super easy, as there exist a number of ways to do this. Below I’ve listed all of those and have also mentioned which way I personally prefer to use.

### Examples showing How to create tuples in Python?

``````# Creating tuple using circular parenthesis
x = (27,91,45,92,100)
y = (646, 929, 1673)
z = ("I live in Melbourne", "I'm a programmer")

# Creating tuple without using circular parenthesis
a = "Microsoft", "Apple", "Tesla"
b = 18, 65, 82
c = "India", "USA", "UK"

# Creating tuple using tuple() Python function
t = tuple("How are you?", "How's your day")
u = tuple("Samsung", "Xiamo", "SmartPhone")
v = tuple("Business", "Life", "Entrepreneurship")
``````

## How to access elements of a tuple in Python?

For accessing element of a tuple just put index of element to be accessed inside square brackets and put it after name of tuple. For example – If we have a tuple x = (19, 28, 28) then to access element at index 1 we can do x. It’s super easy and simple.

Note that you can also use negative values inside square brackets here, in that case Python would start from right to left side of tuple with rightmost value being at index -1.
For example => For tuple x = (74, 28, 82) x = 74 and x[-1] = 82

This method of accessing elements from a tuple can even further be used for accessing elements if tuple is nested(have list/tuple/dicitonary inside it). Like x = (10, [27, 29], { 1: “Hi”, 2: “How”}, “I am programmer”) is a nested tuple.
Below code example shows how to use square brackets for accessing elements inside a nested Python tuple.

``````x = (10, [27, 29], { 1: "Hi", 2: "How"}, "I am programmer")

x         # Returns 10
x         # Returns [27, 29] a list

x      # Returns first element inside list returned by x
x      # Returns second element inside list returned by x

x         # Returns dictionary { 1: "Hi", 2: "How"}
x      # Returns value of key 1 inside dictionary returned by x
x      # Returns value of key 2 inside dictionary returned by x
x      # Raises keyError as there is no key 3 in dictionary x``````

Note 🖐🏻
I would be really careful while accessing element of some element of a nested tuple. As you saw in above example for x it raised KeyError. That’s why for dealing with such situations I’ll use if else statements just to make sure element exist before accessing it.

## Adding new elements to tuple in Python?

No, you cannot add new elements to a tuple in Python as tuples are immutable by design. Once a tuple is created it can never be changed by adding/removing values from it. If you want to change some existing tuple then you need to create a new one.
For example – If you have a tuple x = (19, 28, 29) and you want to add 10 number to end of this then.

### Adding element to a tuple by changing it to a list

1. Convert tuple first to a list.
y = list(x)
2. Append value to end of newly created list
y.append(10)
3. Convert list back to tuple by using tuple() python function
new_tuple = tuple(y)
``````x = (19, 28, 29)      # A Python tuple
y = list(x)           # Changed tuple to list [19, 28, 29]
y.append(10)          # Added 10 to end of list y [19, 28, 29, 10]
new_tuple = tuple(y)  # Changed list back to tuple

# new_tuple will be (19, 28, 29, 10)``````

If your unaware of What are lists in Python? Check out this article.