Python Programming Experiments Guide
Python Programming Experiments Guide
Experiment 6 involves importing the `matplotlib.pyplot` library, defining x and y values, and using `plt.plot()` to create a line graph which is then displayed using `plt.show()`. The outcome is a successfully plotted line graph showing a relationship between the x and y data points provided .
Array slicing in Experiment 11 involves accessing a subset of elements from a NumPy array using index ranges. The expression `arr[1:4]` retrieves elements from index 1 to 3, resulting in a sliced array `[20, 30, 40]`. This operation showcases NumPy's ability to efficiently manipulate and access subarrays .
Experiment 12 demonstrates reindexing by changing the DataFrame index order using `reindex()`. Missing labels result in NaNs for missing values. This process highlights challenges such as handling missing data and ensuring consistent data alignment. It underscores the importance of understanding index operations for effective DataFrame manipulation .
Experiment 3 defines a function `square` to compute the square of a number and a class `Demo` with a method `show` that prints a message. It illustrates the use of functions for discrete tasks and classes for encapsulating behavior and data under a unified structure. A `Demo` object is created to execute the class method alongside the function, demonstrating Python's modular structure allowing structured programming .
In Experiment 8, NumPy is used to calculate the correlation coefficient using `np.corrcoef()`, which computes the degree to which two datasets vary together. The output matrix shows a perfect positive linear relationship (value of 1) between the two datasets, indicating that as one increases, the other does too in a linear fashion .
The program checks if a number is prime by testing divisibility from 2 to one less than the number. If no divisors are found, it prints "Prime Number". If the input is 1, it prints "Not a Prime Number" since numbers less than or equal to 1 are not prime .
Seaborn is beneficial for creating aesthetically pleasing and informative statistical plots with minimal effort, allowing comprehensive customization and easy integration with Matplotlib. However, its abstraction layer can be limiting for highly complex and custom plot requirements, and users must be familiar with both Seaborn and Matplotlib for full customization potential. Experiment 14 shows these benefits visually .
Experiment 2 demonstrates operations like adding elements to a list and set, displaying tuple contents, and updating a dictionary. Lists are mutable and ordered, tuples are immutable and ordered, sets are mutable and unordered with no duplicates, and dictionaries store key-value pairs, allowing for fast retrieval by key. Each structure serves specific use cases depending on requirements such as mutability, ordering, and data retrieval .
In Experiment 9, a linear regression model is created using the `LinearRegression` class from the `sklearn.linear_model` library. Independent and dependent variables are initialized, the model is trained using the `fit` method, and predictions are made using `predict`. The predicted output corresponds linearly with the input data based on the trained model, demonstrating how data-driven predictions can be made .
The mean is calculated manually by summing all elements in the list and then dividing by the count of elements. This straightforward calculation is performed using basic arithmetic operations without relying on external libraries .