Tuple in Python

Tuple in Python

Tuple is a type of data structure, which is a finite ordered sequence of entities or objects of different data types including numbers, strings, boolean, lists and other tuples. An $n$- tuple is typically an $n$ order tuple containing $n$ elements.

Properties of tuple :

  • Ordered – the items are defined in a definite positions from left to right
  • Indexed – items in the tuple can be accessed using indexing.
  • Immutable – tuples once defined can’t modified i.e. one can’t add, remove or change items.
  • Dissimilar objects type – the items in tuples can be numbers, strings, boolean, list or other tuples.

Example :

tuple =  ("I", ["am", "tuple"], ("of", "order"), 4)

In Python, tuples is just a comma-separated sequence which can be created with or without parentheses (). In general, we use parentheses to make the tuple easily identifiable.

#Create a tuple without parentheses
T = 1, 2, True, "String", ["list"], ("tuple")
type(T)  
>>tuple

#Create a tuple with a parentheses
T = (1, 2, True, "String", ["list"], ("tuple"))
type(T)  
>>tuple

Empty and singleton tuple

In Python, an empty tuple i.e. tuple with no objects can be created by an empty parentheses. A singleton tuple i.e. tuple with one object can be created by adding a comma , after the object inside a parentheses (optional).

# Empty tuple
T = ()

# Singleton tuple 
T = ("object",)
T = "object", 

Indexing and slicing

  • Accessing tuples items with indexing

The objects inside the tuples can be accessed in the same way that we do in case of lists. Individual objects can be accessed using an index i.e. integer in square brackets [].

  • Slicing a range from a tuple

Similar to lists, the tuple can be sliced to access a particular range of sequence. Slicing can be done by slicing operator Tuple[a:b] , where a and b are the indexing.

# Indexing in tuple
T = ("object-1", "object-2", "object-3", "object-4")
T[0]
>> 'object-1'
T[-1]
>>'object-4'

# Slicing of tuple
T = ("object-1", "object-2", "object-3", "object-4")
T[1:3]
>> ('object-2', 'object-3')

Mutations in an immutable tuple !

In the very beginning of this post, we defined the tuple as immutable i.e. one can’t change the objects in tuples. But, the immutability is applicable only to the top level of the tuple itself, therefore the items inside the list of any tuple can be modified i.e. the list inside tuple can be mutable.

# Modifying the object in list
T = (1, 2 ,"lab", [1, 2, 3])
T[3][1] = 1.5
print(T)
>> (1, 2, 'lab', [1, 1.5, 3])

# Adding the object in the list 
T[3].append("end")
print(T)
>>(1, 2, 'lab', [1, 'a', 3, 'end'])

# Deleting the object in the list
T[3].pop(3)
print(T)
>>(1, 2, 'lab', [1, 1.5, 3])

Tuple as an argument to function

While using Python or FEniCS, there can be case when the function take the arguments in the form of tuple, in such cases if you will try to use give any other data type as an argument to the function then, it will show some error.

Let us understand this with a simple example of a function fun(tuple) which takes in tuple tuple as an argument and can not give output if any other tuple is defined,

def fun(tuple):
    a, b, c = tuple
		sum  = a + b + c
		print(type(tuple))
    return sum

fun((1,2,3))
>> <class 'tuple'>
>> 6

fun(1,2,3)
>> TypeError

In FEniCS, we frequently use this syntax while defining the boundary conditions, the Constant function Constant((a,b)) here takes only one argument (a,b) in the form of tuple.

bc = DirichletBC(V, Constant((a,b)), left)

Effects of symbols in data types

In python, while assigning values to the variables there is a great possibility that one comma may change the data type of the object/variable. Here, we will see the influence of commas and brackets on the data type.

a = 1
print(type(a))
>> <class 'int'>

a = (1)
print(type(a))
>> <class 'int'>

a = ()
print(type(a))
>> <class 'tuple'>

a = (1,)
print(type(a))
>> <class 'tuple'>

a = [1,]
print(type(a))
>> <class 'list'>

References – learn-by-example, w3schools, mathworld.

Categories: Programming

Leave a Reply