Page 435 - AI_Ver_3.0_class_11
P. 435

10.  A csv file ‘cars.csv’ contains the following data.
                                              Brand        Model       Year       Price

                                              Toyota       Camry       2021       945000
                                              Honda        Civic       2022       896000
                                              Ford         Mustang     2021       867000
                                              Chevrolet    Malibu      2021       785000
                                              Nissan       Altima      2022       625000

                     Write a Python Program to add more data to the above file.
                        import csv
                        file=open("cars.csv", "a")
                        wr=csv.writer(file)
                        wr.writerow(["Tata", "Nano",2022,225000] )
                        print("Row added")
                        file.close()
                     Output:

                     Row added
                 11. Write a Python program to display details of the above file ‘cars.csv’.
                        import csv
                        file=open("cars.csv", "r")
                        data=csv.reader(file)
                        for row in data:
                          print(row)
                        file.close()
                     Output:
                        ['Brand', 'Model', 'Year', 'Price']
                        ['Toyota', 'Camry', '2021', '945000']
                        ['Honda', 'Civic', '2022', '896000']
                        ['Ford', 'Mustang', '2021', '867000']
                        ['Chevrolet', 'Malibu', '2021', '785000']
                        ['Nissan', 'Altima', '2022', '625000']
                        ['Tata', 'Nano', '2022', '225000']
                 12.  Create a 1D array using NumPy with values from 20 to 40. Then reshape this array into a 2D array.

                        import numpy as np
                        # Create a 1D array with values ranging from 20 to 40
                        array_1d = np.arange(20, 41)
                        # Reshape the 1D array into a 2D array with 3 rows and 7 columns
                        array_2d = array_1d.reshape(3, 7)
                        # Print the 2D array
                        print(array_2d)
                     Output:
                        [[20 21 22 23 24 25 26]
                         [27 28 29 30 31 32 33]
                         [34 35 36 37 38 39 40]]



                                                                                          Practical Questions   433
   430   431   432   433   434   435   436   437   438   439   440