Python Programming Lab Report CSE3011
Python Programming Lab Report CSE3011
The process of multiplying two matrices involves iterating over each element of the result matrix, where for each element at position (i, j), the sum of the products of the elements of the ith row of the first matrix and the jth column of the second matrix is calculated. The resulting matrix for A = [[1, 2, 3], [4, 5, 6]] and B = [[7, 8], [9, 10], [11, 12]] is [[58, 64], [139, 154]]. This is done by performing the following operations: for the first element 58, ((1*7) + (2*9) + (3*11)), and so on for each element in the result matrix .
The program simulates an elliptical orbit using Pygame by calculating x and y positions of elliptical paths for planetary objects using trigonometric functions. The key variables include X_center and Y_center for the center of the ellipse, X_ellipse and Y_ellipse for the radii of the ellipse in x and y directions, and degree to iterate through angles 0 to 360 to plot points around the ellipse. It uses the cos and sin functions to determine x and y positions of the orbiting objects, adjusts for the screen coordinates, and dynamically updates the graphics in the game window with each loop iteration .
The code structure creates a class named 'A' and defines a method named 'view' within it, which simply prints a message. An object of this class is instantiated with the statement 'b = A()'. Then, the 'view' method is called on this object using 'b.view()' to execute and print the message "This is a simple class!" to the console .
The function is defined to accept a single string parameter and prints that parameter. During invocation, the user is prompted to input a string, which is passed to the function. The function then prints the input string to demonstrate that it can dynamically handle various input values and return exactly what was entered. This allows any string given as input to be directly printed, showcasing basic string handling within a function in Python .
The simulation uses Pygame to draw a red ball that moves across the screen. The key components include defining the screen size, the ball’s initial position, its speed, and the mechanism to detect collisions with the screen edges. As the ball moves, its position is updated based on the speed. When it hits the boundary, the direction of its velocity is reversed using conditional statements, simulating a bounce by inverting the corresponding speed component. These mechanics ensure realistic movement within the defined space .
The program dynamically handles strings by first asking the user how many strings they want to enter. It then uses a loop to input each string and appends each string to a list named 'a'. After all strings are collected, the program iterates through the list and prints each string to the console. This approach allows for dynamic input and storage in Python lists, facilitating easy manipulation and access to the stored data .
The method used in the program involves reading the text from a file, preprocessing the text to remove punctuation and convert all words to lowercase, and then splitting the text into individual words. The program uses Python's Counter class from the collections module to count the frequency of each word. It then identifies the top n most frequent words using the most_common method, which returns the n most common elements and their counts from the most common to the least. This list is printed to the console .
The program identifies and prints the first n prime numbers by using a while loop that continues until n prime numbers are found. It checks each number starting from 2 to see if it can be divided evenly by any number up to its square root. If it cannot be divided by any of these, it is prime and added to the list of primes. The program uses a counter to keep track of how many primes have been found and increments the counter each time a prime is added to the list .
The recommended approach involves prompting the user to input an ID and a password, which are then compared against predefined values stored in variables (e.g., 'VS' for ID and 'hello@123' for password). If the input matches these values, the program prints a welcome message. While this offers basic authentication, the example lacks security practices such as password hashing or the use of secure storage mechanisms, making it unsuitable for real-world applications without further enhancement .
The program prompts the user to input a line of text, splits the input string into words using the split() method, which divides the string by spaces, and then counts the total number of elements in the resulting list using len(). This count reflects the number of words in the input string, which is printed out to the user as the word count result .