Page 443 - ComputerScience_Class_11
P. 443

3.  Nested List: A nested list is a list that contains one or more lists as its elements. It means a list inside another list,
                   used to store data in a structured or tabular form.
                 Program 9: To store a list inside another list in a single list variable.

                     Program 9.py
                  File  Edit  Format   Run   Options   Window     Help

                  # Creating a nested list (list inside a list )
                  numbers = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
                  # Printing the nested list
                  print("The nested data is:", numbers)




                     Output
                  The nested data is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]





                 12.4.3 Tuple
                 A tuple is a sequence data type in Python that stores multiple values in an ordered form. The elements of a tuple are
                 enclosed in parentheses () and separated by commas. Each element has an index number starting from 0 and a tuple
                 is immutable, which means its elements cannot be changed after creation. Syntax of tuples are as follows:

                 tuple_name = (item1, item2, item3, ...)
                 Program 10: To store data in a variable using the tuple sequence data type.
                     Program 10.py

                  File  Edit  Format   Run   Options   Window     Help

                  # Creating a tuple
                  number = (10, 20, 30, 40)
                  # Printing the complete tuple
                  print(number)
                  # Accessing the element at index 1
                  print("The element at index 1: ", number[1])
                  # Accessing the element at index 0
                  print("The element at index 0: ", number[0])




                     Output

                  (10, 20, 30, 40)
                  The element at index 1:  20
                  The element at index 0:  10




                       Note: As explained earlier, if a list is like a shopping bag where you can keep things in any manner, a tuple
                       is like a sealed box. Once you put items inside and close the lid, you can look at what is inside, but you
                       cannot change anything.




                                                                                               Data Types in Python  441
   438   439   440   441   442   443   444   445   446   447   448