Python Programs for Student Data and Calculations
Python Programs for Student Data and Calculations
To determine the frequency of digits in a multi-digit number in Python, you can convert the number to a string to iterate over each character, then use a set to identify unique digits. For each digit, count the occurrences using the string `count()` method, which tallies how many times a specific character appears within the string .
Use assertions to ensure preconditions, such as `a > 0`, are met. To handle division, use `try` and `except` blocks to catch `ZeroDivisionError` if `b` is zero, printing an error message and exiting the program safely. The use of assertions and exceptions ensures robust input validation and error reporting .
First, open the input file in read mode to access its lines, which are read into a list after stripping newline characters. Sort this list and then open a new output file in write mode, where the sorted lines are written back. Finally, close both files and confirm the new file's existence to ensure that sorting was successful .
Read the student's name, USN, and marks in three subjects through input prompts. Calculate the total marks by summing the marks of the three subjects. The percentage is computed by dividing the total marks by the maximum possible marks (300), then multiplying by 100. Display the student details along with calculated total marks and percentage .
A person is classified as a senior citizen if their age is greater than 60. You can calculate the age by subtracting the year of birth from the current year, 2023. If the calculated age exceeds 60, the person is considered a senior citizen; otherwise, they are not .
To generate the Fibonacci sequence up to a specified length, N, initialize the first two numbers and iterate to compute subsequent numbers by summing the last two. If N equals 1, only the first number is printed. For larger N, a loop runs starting from 2 up to N-1, adding and updating the last two values until the sequence is complete .
Calculating the binomial coefficient N choose R involves using the factorial function. Programmatically, this is implemented by defining a recursive function `fact()` to compute the factorial of N, R, and N-R. Then, the binomial coefficient is given as `nCr = fact(n)/(fact(r)*fact(n-r))`. This uses the mathematical formula for combinations, which is `N! / (R!(N-R)!)` .
To calculate the mean, sum all elements in the list and divide by the number of elements. For variance, sum the squared differences between each element and the mean, then divide by the number of elements. The standard deviation is the square root of the variance. These calculations are all done programmatically using loops to iterate through the user-inputted list values .
To back up a folder into a ZIP file, use the `os`, `pathlib`, and `zipfile` modules. Begin by specifying the folder to compress. `pathlib.Path` is used to navigate and list all files within the directory, while `zipfile.ZipFile` creates the archive. Each file is added to the archive with its path relative to the folder being zipped. These operations confirm the archive's creation .
Use a dictionary to map words to their frequency. Open the text file, iterate line by line, and split each line into words after stripping spaces and converting to lowercase to avoid case mismatch. For each word, update the frequency count in the dictionary. Sort the dictionary by frequency in descending order and select the top 10 words to understand their distribution in the text file .