Mid Term Exam: Python Programming 2023
Mid Term Exam: Python Programming 2023
Type conversions in programming allow for interoperability between different data types, ensuring that functions and operators work cohesively. In Python, they are particularly important for performing arithmetic on user input (typically strings), formatting outputs, and rendering numerical data into printable formats for reporting or debugging purposes .
Python tuples are immutable, meaning once they are created, their elements cannot be changed. In contrast, sets are mutable, allowing for modifications such as adding or removing elements after creation .
A Python program can calculate an employee's net salary by subtracting deductions (tax, insurance) from gross salary. The algorithm involves getting user inputs for gross salary and deductions, calculating the net salary, and outputting the result. The flowchart would visually represent steps: input, calculate deductions, compute net salary, and display output—a clear schematic of the logical process .
Python type conversions change data types, using functions like int() to convert strings to integers, float() to convert strings or integers to floats, and str() to convert numbers to strings. Examples include converting a string '123' to int, '45.67' to float for numerical operations, and converting an integer to a string for concatenation in outputs. Each conversion adapts data handling and operations accordingly .
A Python program can compute the frequency of each letter in a sentence by utilizing a dictionary to count occurrences. For each character in the input sentence, check if it's already a key in the dictionary. If it is, increase the count; if not, add it with an initial count of one. This efficiently tallies frequency .
Iteration relies on repetitive control structures (such as loops) to execute statements multiple times, while recursion involves a function calling itself with modified parameters. An iterative algorithm for summing numbers would use a loop, incrementing a sum variable. A recursion algorithm would involve a function calling itself with decremented parameters until a base case is reached. Each approach offers a different clarity and performance balance depending on the problem .
Type conversions in Python involve converting data from one type to another, facilitating operations that require specific data formats. Examples include 'int()' for converting strings to integers for arithmetic, 'float()' for decimal precision, and 'str()' for output formatting. These conversions ensure compatibility across different functions and operations, critical in processing diverse data inputs effectively .
Python handles string manipulation through built-in functions. Counting characters and words utilizes len() and split() functions respectively, while reversing is achieved through slicing. A program can implement these by taking input, applying these functions, and printing counts and the reversed string, demonstrating Python's versatile handling of text data .
To access a list element in Python, use its index within square brackets. Updating is done by assigning a new value to a specific index, while deletion is achieved with the del statement. A demonstration program would create a list, update an element at index 0, delete an element at index 1, and print the list after each operation to show changes .
Python supports multiple iterative structures such as for loops and while loops. The 'continue' statement skips the current iteration and proceeds to the next, while 'break' exits the loop entirely. For example, a 'for' loop iterating through a list could use 'continue' to skip processing if a certain condition is met, and 'break' to exit when a target value is found .