Class 12 Python Programs Collection
Class 12 Python Programs Collection
The `factorial` function employs recursion by defining `factorial(n)` to call itself with the argument `n-1` until `n` is zero, at which point it returns 1. While this recursion is conceptually simple and elegant, it can impact computational efficiency negatively by consuming more memory through call stack usage compared to an iterative approach, especially for large values of `n`. Thus, it may not be optimal for factorial calculations without tail call optimization, which is not present in Python.
The `calculator` function performs basic arithmetic operations between two numbers `a` and `b` based on the specified `operation`. It can add, subtract, multiply, or divide depending on whether the `operation` parameter is '+', '-', '*', or '/' respectively. It returns the result of the operation.
NumPy's `dot` function performs matrix multiplication, which involves taking the dot product of rows of the first matrix with columns of the second. This method is computationally efficient due to NumPy's implementation in C and its ability to handle large arrays in memory efficiently. Matrix multiplication is fundamental in scientific computing, including computer graphics, simulations, and solving linear equations.
The `binary_search` function enhances search efficiency by using a divide-and-conquer approach that reduces the search interval by half with each step. It starts with the middle element of a sorted array. If this matches the target, it returns the index. If the target is smaller, the search continues in the left subarray; if larger, in the right subarray. This method significantly reduces the number of elements compared to a linear search, operating in logarithmic time complexity, O(log n)
The `Student` class illustrates key object-oriented programming concepts such as encapsulation and data abstraction. By defining attributes like `name`, `age`, and `grade` within a class, it encapsulates student data and methods related to them, creating a clear interface for object manipulation. This approach allows for managing complexity through modular programming and provides a blueprint to instantiate multiple student objects consistently.
The Python program demonstrates file handling by using context managers (`with` statement) to open and manage file resources efficiently. It opens 'input.txt' for reading and 'output.txt' for writing. The context manager ensures the files are properly closed after operations, reducing resource leakage risks. The program reads the data from the input file and writes it to the output file, showcasing basic file manipulation techniques.
The `is_prime` function checks if a number `n` is less than or equal to 1; if so, it returns `False` as such numbers are not prime. For numbers greater than 1, it iteratively checks divisibility of `n` by any number from 2 up to the square root of `n`. If `n` is divisible by any of these numbers, the function returns `False`, indicating `n` is not a prime number. Otherwise, it returns `True` for prime numbers.
The `remove_duplicates` function uses a set to eliminate duplicates from a list, taking advantage of the set's property of allowing only unique elements. It converts the list into a set to remove duplicates and then back to a list to maintain the original data structure type. This approach is efficient due to the constant time complexity of element insertion in sets.
The `Stack` class implements a basic stack data structure with two main operations: `push`, which adds an item to the end of the list representing the stack, and `pop`, which removes and returns the last item from the stack. This LIFO (Last In, First Out) structure is significant in data manipulation as it is used extensively in algorithms involving history mechanisms, recursive function management, and syntax parsing.
The `count_vowels_consonants` function uses list comprehensions and string membership tests to count vowels by checking each character against a predefined set of vowels and similarly counting consonants by ensuring characters are alphabetic and not vowels. This operation is significant in text analysis as it helps in evaluating linguistic data characteristics, such as readability and phonetic content distribution, which can be crucial in fields like linguistics and natural language processing.