Python Interview Questions 1713797151
Python Interview Questions 1713797151
In [50]:
Question-1
Use a tuple to create 1,2,3 Dimension array and also check the dimension of array
In [51]:
In [52]:
In [53]:
1 Arry1
Out[53]:
localhost:8888/notebooks/1. Python/1. Python programming beginner and intermediate/3. Python practice questions/1. Campus X practice quest… 1/12
7/6/23, 7:46 PM Questions_Numpy_Pandas - Jupyter Notebook
In [54]:
1 Arry2
Out[54]:
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10]])
In [55]:
1 Arry3
Out[55]:
In [56]:
Question-2
In [57]:
1 Arry1[0]
Out[57]:
10
Question-3
localhost:8888/notebooks/1. Python/1. Python programming beginner and intermediate/3. Python practice questions/1. Campus X practice quest… 2/12
7/6/23, 7:46 PM Questions_Numpy_Pandas - Jupyter Notebook
In [58]:
1 Arry2[0,1]
Out[58]:
Question-4
In [59]:
1 Arry2[1,4]
Out[59]:
10
Question-5
Access the 3rd element of 2nd array of 1st array by using of 3d array.
In [60]:
1 Arry3
Out[60]:
In [61]:
1 Arry3[0,1,2]
Out[61]:
Question-6
By use of negative indexing print the last element from 2 dimensional array
localhost:8888/notebooks/1. Python/1. Python programming beginner and intermediate/3. Python practice questions/1. Campus X practice quest… 3/12
7/6/23, 7:46 PM Questions_Numpy_Pandas - Jupyter Notebook
In [62]:
1 Arry2
Out[62]:
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10]])
In [63]:
1 Arry2[-1,-1]
Out[63]:
10
Question-7
Create one dim array and slice the elements from index 1 to 5 by creating 1D array
In [64]:
1 Arry1
Out[64]:
In [65]:
[11 12 13]
Question-8
localhost:8888/notebooks/1. Python/1. Python programming beginner and intermediate/3. Python practice questions/1. Campus X practice quest… 4/12
7/6/23, 7:46 PM Questions_Numpy_Pandas - Jupyter Notebook
In [66]:
Out[66]:
Question-9
By using concept of slicing return every other element from index 1 to 5 by creation of 1 D array.
In [67]:
(2, 6, 10)
Question-10
Create 2D array, from 2nd element slice the elements from index 1 to 4.
localhost:8888/notebooks/1. Python/1. Python programming beginner and intermediate/3. Python practice questions/1. Campus X practice quest… 5/12
7/6/23, 7:46 PM Questions_Numpy_Pandas - Jupyter Notebook
In [68]:
1 import numpy as np
2
3 # Creating a 2D array using a nested list
4 array_2d = np.array([[1, 2, 3],
5 [4, 5, 6],
6 [7, 8, 9]])
7
8 # Displaying the 2D array
9 print("2D array: ",array_2d)
10
11 # Slicing elements from index 1 to index 4
12 sliced_array = array_2d[:, 1:4]
13
14 # Displaying the sliced array
15 for row in sliced_array:
16 for element in row:
17 print(element, end=" ")
18 print()
2D array: [[1 2 3]
[4 5 6]
[7 8 9]]
2 3
5 6
8 9
Question-11
Create 2D array, from 2nd element slice the elements from index 1 to 4.
In [69]:
1 a2 = np.arange(12,dtype=float).reshape(3,4)
In [70]:
1 a2[1,:]
Out[70]:
Question-12
localhost:8888/notebooks/1. Python/1. Python programming beginner and intermediate/3. Python practice questions/1. Campus X practice quest… 6/12
7/6/23, 7:46 PM Questions_Numpy_Pandas - Jupyter Notebook
In [71]:
1 data = {
2 'Name': ['Tina', 'Sam', 'Virat', 'Lisa'],
3 'Age': [25, 28, 30, 22]
4 }
5
6 df = pd.DataFrame(data)
7 print(df)
Name Age
0 Tina 25
1 Sam 28
2 Virat 30
3 Lisa 22
Question-13
In [72]:
1 df.shape
Out[72]:
(4, 2)
Question-14
localhost:8888/notebooks/1. Python/1. Python programming beginner and intermediate/3. Python practice questions/1. Campus X practice quest… 7/12
7/6/23, 7:46 PM Questions_Numpy_Pandas - Jupyter Notebook
In [73]:
1 fruits_df={"Fruit_Name":["Watermelon","Mango","Banana"],
2 "Price":[100,60,50],
3 "Quantity":[1,3,5]
4 }
5
6 Fruit1 = pd.DataFrame(fruits_df)
7 Fruit1
Out[73]:
0 Watermelon 100 1
1 Mango 60 3
2 Banana 50 5
In [74]:
Out[74]:
0 Grapes 90 2
In [75]:
Out[75]:
0 Watermelon 100 1
1 Mango 60 3
2 Banana 50 5
3 Grapes 90 2
Question-15
Create a line graph, bar graph and pie chart using matplotlib and also add the labels.
localhost:8888/notebooks/1. Python/1. Python programming beginner and intermediate/3. Python practice questions/1. Campus X practice quest… 8/12
7/6/23, 7:46 PM Questions_Numpy_Pandas - Jupyter Notebook
In [76]:
Line Graph
In [77]:
1 x_values = [5, 6, 4, 4, 8]
2 y_values = [1, 2, 3, 5, 8]
3
4 # Creating a line graph
5 plt.figure(figsize=(8, 4))
6 plt.plot(x_values, y_values, marker='*', linestyle='-', color='blue')
7 plt.xlabel('X-axis')
8 plt.ylabel('Y-axis')
9 plt.title('Line Graph')
10 plt.show()
Bar Graph
localhost:8888/notebooks/1. Python/1. Python programming beginner and intermediate/3. Python practice questions/1. Campus X practice quest… 9/12
7/6/23, 7:46 PM Questions_Numpy_Pandas - Jupyter Notebook
In [78]:
1 categories = ['Category A', 'Category B', 'Category C', 'Category D', 'Category E']
2 y_values = [10, 5, 8, 12, 6]
3
4 # Creating a figure with a specific size
5 plt.figure(figsize=(8, 4))
6
7 # Creating a bar graph with the given categories and y-values
8 plt.bar(categories, y_values, color='green')
9
10 plt.xlabel('Categories') # Setting the label for the x-axis
11 plt.ylabel('Values') # Setting the label for the y-axis
12 plt.title('Bar Graph') # Setting the title for the graph
13
14 plt.grid(True) # Enabling the grid lines on the graph
15
16 plt.show() # Displaying the bar graph
Pie chart
localhost:8888/notebooks/1. Python/1. Python programming beginner and intermediate/3. Python practice questions/1. Campus X practice que… 10/12
7/6/23, 7:46 PM Questions_Numpy_Pandas - Jupyter Notebook
In [79]:
localhost:8888/notebooks/1. Python/1. Python programming beginner and intermediate/3. Python practice questions/1. Campus X practice que… 11/12
7/6/23, 7:46 PM Questions_Numpy_Pandas - Jupyter Notebook
localhost:8888/notebooks/1. Python/1. Python programming beginner and intermediate/3. Python practice questions/1. Campus X practice que… 12/12