Python Tuples  

Python Tuples  

In Python programming, a tuple is a data storage element structure similar to a list data type. But while you can modify Python list elements after declaring them, an important difference in Python tuples is that Python tuples are not modified after declaration in the program. This means that once you declare a tuple in a Python program, the default storage elements of the tuple cannot be modified, added, or removed. In Python programming, the tuple data type is often used to store related pieces of information that are never modified independently.

Python Tuples

So let’s get to know more about tuples in Python programming.

Creating a Tuple in Python Programs.

In Python programs, you can create a new tuple by declaring tuple data element values ​​inside left and right parenthesis braces () separated by a comma operator. Just here you can never customize/modify tuple elements once they are declared.

# let here Creating a tuple of integers in Python program

integer = (9, 8, 7, 6, 5, 4, 3)

print(integer)

# let Creating a tuple of strings in Python program

gender = (“male”, “female”, “unux”)

print(gender)

# let Creating a group-type tuple in Python program

group_tuple = (9, “vcanhelpsu”, 99.01, false)

print(group_tuple)

Accessing tuple elements in Python.

In Python program you can access individual default storage elements of a tuple by using square brackets [] and the storage index location of the storage tuple element (index location which starts from 0 and continues till the end).

opsystem = (“microsoft windows”, “apple mac os”, “google android”, “linux”, “unix”)

print(opsystem[0]) # the result is – “microsoft windows”

print(opsystem[1]) # the result is – “apple mac os”

print(opsystem[-3]) # the result is – “google android” (negative indexing -3 from right side)

Tuple packing and unpacking in Python.

Tuple packing in Python programming is considered when you create a tuple without using brackets. And Python tuple unpacking is considered when you assign the elements of a declared tuple to multiple variables in a Python program.

# decimal variable name Python tuple packing example

decimal = 99, 87, 66, 88, 97

print(decimal) # the result is – (99, 87, 66, 88, 97)

# assign decimal variable values ​​in p,q,r,s,t variable in Python tuple unpacking example

p, q, r, s, t = decimal

print(p) # the result is – 99

print(q) # the result is – 87

print(r) # the result is – 66

print(s) # the result is – 88

print(t) # the result is – 97

Python Tuple Immutable Nature.

You know that tuple data types declared in Python program are immutable, which means you can never modify tuple elements after tuple declaration in Python program. If you try to do this in a program, it will generate a program error.

desktop = (“apple”, “micrsoft”, “hp”, “lenovo”)

desktop[1] = “acer” # this generate program error

print(desktop[1])

Python Tuple Operations.

You already know that declared tuple data structures in Python programs are immutable, but you can perform many program operations on these declared tuple data types, such as tuple concatenation (+), tuple repetition (*), tuple member checking (in), and finding the length of a tuple (len()).

tuplea = (100, 400, 900, 1000)

tupleb = (300, 200, 700, 999)

# let join two tuple element in single tuple

group_tuple = tuplea + tupleb

print(group_tuple) # the result is – 100, 400, 900, 1000, 300, 200, 700, 999)

# let repeat or multiply declare tunple in python

mututate = tuplea * 2

print(mututate) # the result is – (100, 400, 900, 1000, 100, 400, 900, 1000)

# let findout the member of tuple membership

print(400 in tuplea) # the result is – true

print(700 in tupleb) # the result is – true

When to use tuples in Python programs.

Tuple immutable data – Remember that when you do not need to change the data in a Python program, you can use tuple data type in that program, so that the integrity of the declared tuple of the data is maintained.

Tuple performance – Accessing data declared in a Python program using tuples is generally faster than accessing Python list data element.

Tuple return value – Functions declared in a Python program can return tuple return value to provide multiple pieces of related information.

The main difference between Python tuples and lists.

Tuple mutability – Tuple data types declared in a Python program are completely immutable, while list data types declared in a Python program are mutable.

Tuple syntax – Tuples are created in Python programs using open and close parenthesis braces () while in Python programs lists are declared using square brackets [].

Tuple performance – Under certain conditions, Python tuple data types can be fast and efficient for certain tasks due to their immutability.