Page 451 - Computer Science V2.0 Class 11
P. 451

For example :
                      List1 = [10, 3, 'Orange Books', False]
                      Tuple1 = [25, 12, 'Orange Books', True]
                      print(List1[2])
                      print(Tuple1[3])
                      Output:
                      Orange Books
                      True
                    4.  With the help of an example show how can you return more than one value from a function.
                  Ans. A function in python can return multiple values in the form of tuple.
                      import math
                      def sphereVolumeSurfaceArea(radius):
                          volume = (4/3) * math.pi * radius**3
                          surfaceArea = 4 * math.pi * radius**2
                          return volume, surfaceArea

                      radius = float(input("Enter the radius of the sphere: "))
                      volume, surfaceArea = sphereVolumeSurfaceArea(radius)

                      print(f"Volume of the sphere: {volume:.2f}")
                      print(f"Surface area of the sphere: {surfaceArea:.2f}")
                    5.  What advantages do tuples have over lists?
                  Ans.  The advantages of tuples over lists is that tuples ensure that the data stored in tuple will not change.
                        If you want to store data as a sequence of any type of values and does not want to make any changes accidently, then you can use Tuple
                      instead of List.

                    6.  When to use tuple or dictionary in Python. Give some examples of programming situations mentioning their usefulness.
                  Ans.   Tuples are used to store the data which is not intended to change during the course of execution of the program. For example, if the
                      name of months is needed in a program, then the same can be stored in the tuple as generally, the names will either be repeated for a
                      loop or referenced sometimes during the execution of the program.
                        Dictionary is used to represent the unordered list of the data. The key and value are separated by colon. Dictionary is used to store
                      associative data like student’s roll no. and the student’s name. Here, the roll no. will act as a key to find the corresponding student’s
                      name. The position of the data doesn’t matter as the data can easily be searched by using the corresponding key.
                      For example :
                      #Tuple vs Dictionary


                        month = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')

                      instructor = {101: ('Amrit', 'TGT','Delhi'), 102: ('Bhawna', 'PGT', 'Noida')}

                      print("Name of sixth month : ", month[5])

                      print("Details of Instructor with code 101: ")
                      print(instructor[101])

                      Output:
                      Name of sixth month :  Jun
                      Details of Instructor with code 101:
                      ('Amrit', 'TGT', 'Delhi')




                                                                                                        Dictionaries  437
   446   447   448   449   450   451   452   453   454   455   456