Page 312 - CTS - CSA TP - Volume 2
P. 312

COMPUTER SOFTWARE APPLICATION - CITS




           EXERCISE 136 :   Perform basic operations using built-in
                                         modules


           Objectives

           At the end of this exercise you shall be able to
           •  develop python programs  to Perform basic operations using built-in modules.

           Procedure


           TASK 1: Math Module
           Code:
           import math
           # Example 1: Square Root
           number = 25

           square_root = math.sqrt(number)
           print(f”Square root of {number}: {square_root}”)
           # Example 2: Power
           base = 2

           exponent = 3
           power_result = math.pow(base, exponent)
           print(f”{base} raised to the power of {exponent}: {power_result}”)
           Explanation:





           •  This line imports the math module, which provides access to various mathematical functions and constants in
              Python.







           •  In this section, the code calculates the square root of the number 25 using the sqrt() function from the math
              module. The result is stored in the variable square_root, and then it is printed out with an f-string.











           •  Here, the code calculates 2 raised to the power of 3 using the pow() function from the math module. The result
              is stored in the variable power_result, and then it is printed out with an f-string.
              In summary, the code demonstrates how to use the sqrt() function to calculate square roots and the pow()
              function to calculate powers with the help of the math module.





                                                           297
   307   308   309   310   311   312   313   314   315   316   317