Python Programs for Class XII
Python Programs for Class XII
To calculate income tax using a nested if-else statement, first determine the taxable income by subtracting up to ₹1.5 lakh of savings under Section 80C from the gross income. Then apply tax slabs: no tax for income less than ₹2.5 lakh; a progressive tax from 0% to 5% based on age for income between ₹2.5 lakh to ₹5 lakh; a 20% tax for income between ₹5 lakh to ₹10 lakh .
A short script to calculate LCM uses the formula 'lcm(a, b) = abs(a*b) // gcd(a, b)', relying on 'gcd(a, b)' from the math module. The multiplicative property, stating 'a * b = gcd(a, b) * lcm(a, b)', helps derive lcm, highlighting the relationship between LCM, GCD, and multiplication .
To determine the longest line's length in a text file, iterate over each line of the file, use 'len(line)' to find its length, and track the maximum length encountered. Use the open() function to read the file, and a method like 'file_long()' that reads each line, compares lengths, and returns the longest .
The 'nonlocal' keyword in Python allows a variable in a nested function to refer to a variable defined in the nearest enclosing non-global scope. It's crucial for modifying bindings in outer functions within nested ones. Used in 'add(b)' to modify 'a', its effective use requires comprehension of scope chains and closures .
To use the 'sqrt' function from the Python math module, import the module using 'import math' and call 'math.sqrt(n)'. This will return the square root of 'n'. To advise a programmer, suggest typing 'import math' at the start of their script, then using 'math.sqrt(value)' wherever they need square roots calculated .
When dealing with large files, reading by chunks using 'read(size)' is efficient to avoid memory overload. Unlike reading the entire file at once with 'read()', handling piece-by-piece allows processing without significant resource use. This distinction is significant in production environments where performance and resource constraints are priorities .
Errors in floating-point number formatting, like precision loss or misrepresentation, arise without format specifications. Avoid mistakes with string formatting, using format specifiers such as '.nf' for n decimal places, or '.0f' for integer output, ensuring consistent decimal precision .
'func1(a, b)' recursively prints divisors of 'a', dividing 'a' by 'b' each time if 'a' is divisible by 'b', or incrementing 'b' otherwise. For input (24, 2), it outputs divisors of 24, printing "2 2 2 3" as it breaks 24 into 2's and finally a 3 .
Dictionaries in Python facilitate organized data storage by mapping keys to values, allowing efficient retrieval and manipulation. For student records, dictionaries with keys like admission numbers enable O(1) average-time complexity access to student data, supporting structured and rapid lookups .
To shift elements of a list 'n' positions to the left in Python, slice the list and concatenate. For example, given list Arr = [10, 20, 30, 40, 12, 11] and n=2, the left shift is achieved by combining Arr[n:] (elements from index n to end) with Arr[:n] (elements from start to index n), resulting in [30, 40, 12, 11, 10, 20].