Code Explanation:
Import the Math Module
import math
Imports Python’s math module.
This module provides mathematical functions like sqrt() and pow().
Create a List of Numbers
nums = [9, 25, 49]
A list named nums is created.
It contains three perfect square numbers: 9, 25, and 49.
Calculate Square Roots Using List Comprehension
roots = [math.sqrt(i) for i in nums]
math.sqrt(i) computes the square root of each number in the list.
List comprehension runs sqrt() on 9, 25, and 49 and stores the results in roots.
So roots becomes: [3.0, 5.0, 7.0]
Square the First Root
sq = math.pow(roots[0], 2)
roots[0] is 3.0
This reverses the square root operation for the first element.
Print Required Values
print(roots[1], sq)
roots[1] is the second square root → 5.0
sq is 9.0
Final output:
5.0 9.0
Final Output
5.0 9.0
.png)

0 Comments:
Post a Comment