Course Hero Logo

04 Data_Structure (Tuple, Dictionary) _ Comparisons Part2.pdf

Course Hero uses AI to attempt to automatically extract content from documents to surface to you and others so you can study better, e.g., in search results, to enrich docs, and more. This preview shows page 1 - 4 out of 16 pages.

Tuple_________________________________________________A tuple is a sequence of elements of any type and is immutable. In python, they are initialized insideparenthesis “()” instead of square brackets “[]”. To create a tuple, we simply have to do the following:CodeOutputexample_tuple = ("banana", "mango", "apple")print(type(example_tuple))<class 'tuple'>Tuples can hold values of any type. They can be homogeneous as well as heterogeneous. But we needto remember that once we declare those values, we cannot change them. For example:CodeOutputmixed_type = ('C','S','E', 1, 1, 0)for char in mixed_type:print(char,":",type(i))C : <class 'str'>S : <class 'str'>E : <class 'str'>1 : <class 'int'>1 : <class 'int'>0 : <class 'int'>#Trying to change ‘E’ to ‘O’mixed_type[2] = 'O'TypeError: 'tuple'object does not supportitem assignmentHere, we are getting the error message because we are not allowed to change values inside a tuple.Immutability of Tuple:The word “immutability” in python means an object with a fixed value/id. Here “id” is the identity of alocationof an object inmemory. For example:CodeOutput#1) Declare a Tuple named 'nth_tuple'nth_tuple = (21,21,34,47)#2) Items with the same value have the same id.if id(nth_tuple[0]) == id(nth_tuple[1]):print("True")print(id(nth_tuple[0]))True975683297568321
print(id(nth_tuple[1]))#3) Items with different values have different ids.if id(nth_tuple[0]) == id(nth_tuple[2]):print ("True")else:print ("False")print(id(nth_tuple[0]))print(id(nth_tuple[2]))False97568329757248#4) append function is not applicable in a tuplenth_tuple.append(5)AttributeError:'tuple' object hasno attribute'append'Tuples are efficient:Tuples provide no access to data values and are considered as faster than the lists. For example:CodeOutput#Execution time for tuple:import timeitprint(timeit.timeit('x=(1,2,3,4,5,6,7,8,9)',number=100000))0.001523201004602015#Execution time for List:print(timeit.timeit('x=[1,2,3,4,5,6,7,8,9]',number=100000)))0.00987649301532656In the above example, we have used the timeit() method imported from python library timeit tocalculate the difference between the execution time of tuple and list. It is seen that tuple takes lesstime to execute than a list.To access the Items of Tuple:1.Print the third item in the tuple:CodeOutputexample_tuple = ("Banana", "Mango", "Apple")print(example_tuple[2])Apple2
2.In tuple, negative indexing means beginning from the end. Example: -1 refers to the last item,-2 refers to the second-last item, etc.:CodeOutputexample_tuple = ("Banana", "Mango", "Apple")print(example_tuple[-1])print(example_tuple[-2])AppleMango3.While specifying a range in a tuple, the return value will give a new tuple with the specifieditems:CodeOutputexample_tuple = ("Banana", "Mango","Apple", "Orange","Grape","Jackfruit")print(example_tuple[2:5])('Apple','Orange','Grape')4.While specifying a range of negative indexes in a tuple, we can start the search from the end ofa tuple. Given example returns the items from index -5 (included) to index -1 (excluded):CodeOutputexample_tuple = ("Banana", "Mango","Apple", "Orange","Grape","Jackfruit")print(example_tuple[-5:-1])('Mango', 'Apple','Orange', 'Grape')Tuple Unpacking:To store elements of a tuple in separate variables is called unpacking. This operation allows us to take

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 16 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Spring
Professor
DIPANKAR CHAKI
Tags

Newly uploaded documents

Show More

Newly uploaded documents

Show More

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture