Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 138 - 2615_KEC
P. 138

    Output
                       Exam Scores: (85.5, 90.2, 78.9, 92.1, 88.6)
                       (90.2, 78.9, 92.1)

                      TUPLE METHODS

                      Tuples are immutable. So, add and remove methods are not provided with tuples. There are only
                      two methods for tuples, which are described in the table given below:

                             Method                                     Explanation
                            count(a)    It returns number of times ‘a’ occurs in the tuple

                            index(a)    It returns index of first occurrence of the element in tuple

                      Program 8: A program to demonstrate the use of tuple methods.

                          Program8.py
                       File  Edit  Format    Run   Options   Window    Help

                       devices = ("Laptop", "Tablet", "Mobile", "Laptop", "Smartwatch")

                       print("Devices Tuple:", devices)

                       print("Count of 'Laptop':", devices.count("Laptop"))
                       print("Index of 'Mobile':", devices.index("Mobile"))

                          Output

                       Devices Tuple: ('Laptop', 'Tablet', 'Mobile', 'Laptop', 'Smartwatch')
                       Count of 'Laptop': 2
                       Index of 'Mobile': 2

                                DICTIONARY

                      In Python, a dictionary is a collection that stores data in the form of key-value pairs. Each key is

                      unique and is used to access the corresponding value. Dictionaries are unordered, meaning the
                      elements do not have a specific order, and they are mutable, meaning the data can be changed
                      after the dictionary is created. Instead of using indexing like lists, you use keys to access the
                      values. A dictionary is a heterogeneous collection, meaning it can store different types of data
                      such as strings, numbers, or even other collections. A dictionary is enclosed in curly braces {}, with
                      each key-value pair separated by a colon [:].

                      136   Premium Edition-VIII

| 133 134 135 136 137 138 139 140 141 142 143 |