Python Practical Programs for Class X
Python Practical Programs for Class X
To read and display the first 10 rows of a CSV file in Python, the 'pandas' library must be used. Import it using `import pandas as pd`. Read the CSV file using `df=pd.read_csv('D:/data.csv')`. To display the first 10 rows, use the DataFrame method `df.head(10)` which outputs the first 10 rows of data from the CSV file .
Python provides several advantages for data processing, including a vast array of libraries and tools like pandas for efficient data manipulation and matplotlib for comprehensive data visualization. This ecosystem enables users to execute complex data tasks with concise and readable code. Python's integration capability with other software systems and its strong community support further augment its utility in processing diverse datasets efficiently .
To create a line chart in Python using Matplotlib, first import `matplotlib.pyplot` as `plt` and `numpy` as `np`. Define your data points in arrays, e.g., `x=np.array([2,3,9,10])` and calculate the corresponding y-values, for example, `y=x*2`. Use `plt.plot(x,y)` to create the line chart. Label the axes with `plt.xlabel('x-axis')` and `plt.ylabel('y-axis')`, set a title with `plt.title('line chart')`, and finally, display the chart using `plt.show()` .
To reverse a string in Python, a function can be created using a for loop to iterate over the string. Define the function `def reverse_string(str):`, initialize an empty string `str1`, and iterate through the characters of the input string, concatenating them in reverse order with `str1=i+str1`. Finally, return `str1`. This constructs the reversed string by adding each character to the front of the accumulating result .
To display metadata information about a CSV file in Python, including data types and column counts, use the pandas library by importing it with `import pandas as pd`. Read the CSV file with `df=pd.read_csv('D:/data.csv')`, and then call `df.info()`, which prints a concise summary of the DataFrame including index dtype, column dtypes, non-null values, and memory usage .
To plot a scatter chart in Python, we need to use the matplotlib library. Begin by importing the necessary modules: `import matplotlib.pyplot as plt` and `import numpy as np`. Define the data points as arrays, like `x=np.array([2,9,8,5,6])` and `y=np.array([5,10,3,7,18])`. Then, label the axes with `plt.xlabel('x-axis')` and `plt.ylabel('y-axis')`, set the chart title with `plt.title('Scatter chart')`, and use `plt.scatter(x,y)` to create the scatter plot. Finally, display the plot using `plt.show()` .
Use the OpenCV and Matplotlib libraries to read and display an image in Python. Import `cv2` and `from matplotlib import pyplot as plt`. Load the image file using `cv2.imread('D:/london.jpg')` to read the image, and to display, use `plt.imshow(img)`, set a title with `plt.title('My favourite city')`, and remove axes using `plt.axis('off')`. Finally, display the image with `plt.show()` .
A line chart is preferable over a scatter chart when the primary goal is to display trends over time or continuous data relationships, as it effectively shows connections between points. Conversely, scatter charts are ideal for illustrating the correlation or distribution of two variables without inherent ordered relationships . Line charts provide a clearer view of the flow or progression of data, whereas scatter charts offer a visual representation of data distribution or clustering without suggesting continuity or order .
To calculate the mean, median, and mode of a dataset in Python, the 'statistics' module can be used. First, import the module using `import statistics`. Then, for a given list `d`, compute the mean with `statistics.mean(d)`, the median with `statistics.median(d)`, and the mode with `statistics.mode(d)` .
To calculate the factorial of a number in Python, use a function that employs a for loop. Prompt for user input with `n=int(input('Enter a number:'))`. Initialize a variable `f=1` for storing the result. Iterate via a for loop `for i in range(1,n+1):`, multiplying `f` by `i` in each iteration with `f=f*i`. Print the result after the loop completes. This logic accumulates the product of all positive integers up to `n`, effectively computing the factorial .