# Question 1 – NumPy Array Operations # Write Python code to perform the following tasks: # 1. Create a NumPy array containing numbers 1 to 20. # 2. Reshape the array into a 4 × 5 matrix. # 3. Extract: # - the second row # - the third column # 4. Compute: # - mean # - maximum value # - sum of all elements import numpy as np # 1. Create a NumPy array containing numbers 1 to 20 arr = np arange ( 1 , 21 ) # 2. Reshape the array into a 4 × 5 matrix matrix = arr reshape ( 4 , 5 ) print ( "4x5 Matrix:" ) print ( matrix ) # 3. Extract the second row and the third column second_row = matrix [ 1 ] # index 1 = second row third_column = matrix [:, 2 ] # index 2 = third column print ( "\nSecond Row:" ) print ( second_row ) print ( "\nThird Column:" ) print ( third_column ) # 4. Compute mean, maximum value, and sum mean_value = np mean ( matrix ) max_value = np max ( matrix ) sum_value = np sum ( matrix ) print ( "\nMean:" , mean_value ) print ( "Maximum Value:" , max_value ) print ( "Sum of all elements:" , sum_value ) 4x5 Matrix: [[ 1 2 3 4 5] [ 6 7 8 9 10] [11 12 13 14 15] [16 17 18 19 20]] Second Row: [ 6 7 8 9 10] Third Column: [ 3 8 13 18] Mean: 10.5 Maximum Value: 20 Sum of all elements: 210 # Question 2 – NumPy Random Data Analysis # Generate 50 random numbers between 0 and 100 using NumPy. # Then write code to: # 1. Find the mean # 2. Find the standard deviation # 3. Display all numbers greater than 70 import numpy as np # Generate 50 random numbers between 0 and 100 random_numbers = np random uniform ( 0 , 100 , 50 ) print ( "Random Numbers:" ) print ( random_numbers ) # 1. Find the mean mean_value = np mean ( random_numbers ) # 2. Find the standard deviation std_deviation = np std ( random_numbers ) # 3. Display numbers greater than 70 greater_than_70 = random_numbers [ random_numbers > 70 ] print ( "\nMean:" , mean_value ) print ( "Standard Deviation:" , std_deviation ) In [1]: In [2]: print ( "\nNumbers greater than 70:" ) print ( greater_than_70 ) Random Numbers: [26.18427078 52.77964036 70.94357907 84.57339288 20.41642947 6.31001372 15.13796082 91.74080553 55.56109812 17.11941639 56.7958677 40.55004194 12.19345365 83.71081642 89.90336479 19.27581212 77.53693223 62.12968626 39.42529189 69.57537697 42.86655963 80.67553903 66.68682405 35.80310604 25.54530746 61.04857358 24.31253011 71.68362251 44.58537775 62.63002699 89.85773504 66.90991574 60.68929007 59.40243847 99.76006784 30.24535592 87.37895082 69.80952826 14.66788194 22.67433853 36.1600819 46.88246375 1.28967347 69.11858622 33.49852795 7.5565087 80.30364462 9.43546322 71.22180745 94.23702282] Mean: 51.17600002013299 Standard Deviation: 27.51534765714298 Numbers greater than 70: [70.94357907 84.57339288 91.74080553 83.71081642 89.90336479 77.53693223 80.67553903 71.68362251 89.85773504 99.76006784 87.37895082 80.30364462 71.22180745 94.23702282] # Question 3 – Pandas DataFrame Creation and Analysis # Create the following dataset using Pandas DataFrame: # Student Age Marks # Ali 21 85 # Sara 22 92 # Ahmed 20 78 # Ayesha 21 88 # Usman 23 67 # Perform the following tasks: # 1. Display the first three rows # 2. Calculate the average marks # 3. Display students with Marks greater than 80 # 4. Sort the DataFrame by Marks in descending order import pandas as pd # Create the dataset data = { "Student" : [ "Ali" , "Sara" , "Ahmed" , "Ayesha" , "Usman" ], "Age" : [ 21 , 22 , 20 , 21 , 23 ], "Marks" : [ 85 , 92 , 78 , 88 , 67 ] } df = pd DataFrame ( data ) print ( "Original DataFrame:" ) print ( df ) # 1. Display the first three rows print ( "\nFirst Three Rows:" ) print ( df head ( 3 )) # 2. Calculate the average marks average_marks = df [ "Marks" ] mean () print ( "\nAverage Marks:" , average_marks ) # 3. Display students with Marks greater than 80 print ( "\nStudents with Marks greater than 80:" ) print ( df [ df [ "Marks" ] > 80 ]) # 4. Sort the DataFrame by Marks in descending order print ( "\nDataFrame sorted by Marks (Descending):" ) sorted_df = df sort_values ( by = "Marks" , ascending = False ) print ( sorted_df ) In [3]: Original DataFrame: Student Age Marks 0 Ali 21 85 1 Sara 22 92 2 Ahmed 20 78 3 Ayesha 21 88 4 Usman 23 67 First Three Rows: Student Age Marks 0 Ali 21 85 1 Sara 22 92 2 Ahmed 20 78 Average Marks: 82.0 Students with Marks greater than 80: Student Age Marks 0 Ali 21 85 1 Sara 22 92 3 Ayesha 21 88 DataFrame sorted by Marks (Descending): Student Age Marks 1 Sara 22 92 3 Ayesha 21 88 0 Ali 21 85 2 Ahmed 20 78 4 Usman 23 67 # Question 4 – Data Manipulation in Pandas # Using the DataFrame from Question 3: # 1. Add a new column called Grade. # 2. Assign grades using the rule: # Marks ≥ 90 → A # 80–89 → B # < 80 → C import pandas as pd # Original DataFrame data = { "Student" : [ "Ali" , "Sara" , "Ahmed" , "Ayesha" , "Usman" ], "Age" : [ 21 , 22 , 22 , 20 , 23 ], "Marks" : [ 85 , 92 , 88 , 78 , 67 ] } df = pd DataFrame ( data ) # 1 & 2. Add Grade column based on marks def assign_grade ( marks ): if marks >= 90 : return "A" elif marks >= 80 : return "B" else : return "C" df [ "Grade" ] = df [ "Marks" ] apply ( assign_grade ) print ( df ) Student Age Marks Grade 0 Ali 21 85 B 1 Sara 22 92 A 2 Ahmed 22 88 B 3 Ayesha 20 78 C 4 Usman 23 67 C # Question 5 – Data Visualization using Matplotlib # Using the following data: # students = ['Ali','Sara','Ahmed','Ayesha','Usman'] # marks = [85,92,78,88,67] # Write Python code to: # 1. Plot a bar chart of student marks # 2. Label the x-axis as "Students" # 3. Label the y-axis as "Marks" # 4. Add the title "Student Marks Visualization" # Generate 100 random numbers from a normal distribution using NumPy and plot a # histogram using Matplotlib. import numpy as np import matplotlib.pyplot as plt # Given data In [6]: In [9]: students = [ 'Ali' , 'Sara' , 'Ahmed' , 'Ayesha' , 'Usman' ] marks = [ 85 , 92 , 78 , 88 , 67 ] # 1. Plot a bar chart of student marks plt figure ( figsize = ( 6 , 4 )) plt bar ( students , marks ) # 2. Label x-axis plt xlabel ( "Students" ) # 3. Label y-axis plt ylabel ( "Marks" ) # 4. Add title plt title ( "Student Marks Visualization" ) plt show () # Generate 100 random numbers from a normal distribution random_data = np random normal ( 0 , 1 , 100 ) # Plot histogram plt figure ( figsize = ( 6 , 4 )) plt hist ( random_data , bins = 10 , color = 'skyblue' , edgecolor = 'black' ) plt title ( "Histogram of Random Normal Distribution" ) plt xlabel ( "Values" ) plt ylabel ( "Frequency" ) plt show () 100 In [ ]: