Tuples and their methods in Python
Python tuples are an important data type in the Python programming language. They are similar to lists but with one key difference: tuples are immutable, meaning they cannot be changed once they are created. This blog post will explore the ins and outs of Python tuples and their various uses.
Basically, tuples are used to store multiple values in a single variable.
Tuples are immutable, which means once you create a Tuple, then it remains ideal for all time.
Features of Tuple :
1. Immutable: Once a tuple is created, it cannot be modified. This means that you cannot add, remove or change any elements in a tuple.
2. Ordered: The elements in a tuple are ordered, which means that the elements are indexed and can be accessed by their index.
3. Heterogeneous: A tuple can contain elements of different data types, such as integers, floats, strings, and even other tuples.
4. Indexing and slicing: Elements in a tuple can be accessed using indexing and slicing. This means that you can retrieve individual elements, or subsets of elements, from a tuple.
5. Nesting: Tuples can be nested within other tuples. This means that you can create complex data structures using tuples.
6. Iteration: Tuples can be iterated over using loops. This means that you can iterate over all the elements in a tuple and perform some action on each element.
7. Size: Tuples can contain any number of elements, from zero to many. However, once a tuple is created, its size cannot be changed.
8. Memory efficiency: Tuples are more memory-efficient than lists because they are immutable. This means that tuples can be stored in a more compact form in memory.
Creating a Tuple :
In Python, a tuple is defined by enclosing a sequence of values in parentheses.
To create a Tuple, simply create a variable and assign multiple values to it under the parentheses.
Syntax :
Variable_name = (Values / Elements)
Example :
my_tuple = (1,2,3)
print(my_tuple)
#output : 1,2,3
Tuple Indexing and Slicing :
Like lists, tuples can be indexed and sliced. Indexing works the same way as with lists: we use square brackets to access individual elements.
For example:
my_tuple = ("Rohan","Aayan","Ayush")
print(my_tuple[0]) # output : Rohan
print(my_tuple[1]) # output : Aayan
print(my_tuple[2]) # output : Ayush
Like Indexing, Slicing also works the same way as with the list
my_tuple = (1, 2, 3, 4, 5)
print(my_tuple[1:4])
# Output: (2, 3, 4)
# Here we have sliced elements from index 1 to 4
Tuple Unpacking :
Tuple unpacking is a powerful feature of Python tuples
It allows us to assign the elements of a tuple to individual variables.
By using this feature, we can easily able to use tuple values as the values of individual variables.
Suppose there is a Tuple with values 10, 5, and 8. Now we have to assign each value to an individual variable. after assigning simply take the addition of those values.
Example :
Numbers = (10,5,8)
a,b,c = Numbers
print("A = "+str(a))
print("B = "+str(b))
print("C = "+str(c))
print("")
addition = a + b + c
print("Addition is : "+str(addition))
Packing Tuples :
Tuple packing is the opposite of tuple unpacking.
It allows us to create a tuple from a sequence of variables.
In that, we can create variables with values and assigns them as the values of a Tuple.
Suppose, we have created 3 variables, fruit1, fruit2, and fruit3. Now we have to create Tuple named Fruits and then assign the variables as the values of the tuple.
Example :
Fruit1 = "Apple"
Fruit2 = "Banana"
Fruit3 = "Coko"
Fruits = (Fruit1, Fruit2, Fruit3)
print("Fruits : "+str(Fruits))
Methods of Tuple in Python :
Here are some of the methods available for tuples in Python:
1. count( )
2. index( )
Note that since tuples are immutable, there are no methods to add or remove elements from a tuple.
1. count( ) Method :
This method returns the number of times a specified element appears in the tuple.
Syntax :
Tuple_obj.count(element)
Example :
tpl = (1,2,3,4,5,3,2,1,4,5,6,3,4,2,1)
count_of_2 = tpl.count(2)
print("In Tuple tlp, element 2 is appears "+str(count_of_2)+" Time")
2. index( ) Method :
This method returns the index of the first occurrence of a specified element in the tuple.
Syntax :
TupleObj.index(element)
Example :
tpl = (1,2,3,4,5,3,2,1,4,5,6,3,4,2,1)
index_of_5 = tpl.index(5)
print("In Tuple tlp, element 5 is appears at index "+str(index_of_5))
Nested Tuples :
A nested tuple in Python is a tuple that contains one or more tuples as its elements.
It means, it is also a tuple, but it contains multiple tuples inside a tuple as its element.
Example :
Nested = (1,2,3,("Apple","Banana","Mango"),"John","Doe")
Here is an example of a Nested Tuple. It contains Numbers, Strings, and another tuple.
Accessing the values of inside tuples from a Nested Tuple.
By using indexing, we can easily access the values of the inside tuple.
Example :
Tuples are also used in various ways, but at the beginner's level, It's sufficient for us.
I hope it will help you to learn Python Programming More...!
0 Comments
Please do not add Any Spam link in Comments !