Tuples in Python in Data Science

Tuples in Python in Data Science

 we cannot change the elements of a tuple once it is assigned whereas we can change the elements of a list.

– Immutable: Once they are created they cannot be changed – Ordered: They maintain their order once created – Indexable: Information can be accessed if we know the positioning of the item within the tuple – Can contain duplicate records:

What are Tuples?

What are Tuples?

Creating a Tuple

Creating a Tuple

A tuple is created by placing all the items (elements) inside parentheses (), separated by commas.

Different types of tuples # Empty tuple my_tuple = () print(my_tuple) # Tuple having integers my_tuple = (1, 2, 3) print(my_tuple)

here are various ways in which we can access the elements of a tuple. 1. Indexing my_tuple = ('p','e','r','m','i','t') print(my_tuple[0])   # 'p' print(my_tuple[5])   # 't'

Access Tuple Elements

Access Tuple Elements

Negative Indexing in Tuple

Negative Indexing in Tuple

The index of -1 refers to the last item, -2 to the second last item and so on. my_tuple = ('p', 'e', 'r', 'm', 'i', 't') # Output: 't'

Slicing

Slicing

We can access a range of items in a tuple by using the slicing operator colon # Accessing tuple elements using slicing my_tuple = ('p','r','o','g','r','a','m','i','z') # elements 2nd to 4th # Output: ('r', 'o', 'g') print(my_tuple[1:4])

Changing a Tuple

Changing a Tuple

This means that elements of a tuple cannot be changed once they have been assigned. But, if the element is itself a mutable data type like a list, its nested items can be changed my_tuple = (4, 2, 3, [6, 5]) # TypeError: 'tuple' object does not support item assignment # my_tuple[1] = 9 # However, item of mutable element can be changed my_tuple[3][0] = 9    # Output: (4, 2, 3, [9, 5])

Inbuilt functionality in Tuple

Inbuilt functionality in Tuple

finding the length of the tuple, printing the number of instances of a specific value and finding the minimum or maximum of a tuple #print the length of the tuple print(len(tuple1) )#print the count of values within a tuple print(tuple4.count("a")) #print the maximum value from a tuple print(max(tuple2))#print the

Our mission is to make Uttarakhand IT hub with our excellence in education in Data Science and Data analytics education.

www.thvistaacademy.com +919411778145