
Syntax: Exponents
Raise one number to any power with the Python exponentiation operator.
Exponents in Python
The next math operator to learn about is the exponentiation (**) operator. As you can probably guess, the exponentiation (**) operator is used to raise one numeric value to the power of another.
Typical syntax usage for the addition operator are as follows:
-
What raising one numeric value to the power of another:
power = 5 ** 3 -
When using one variable to raise one numeric value to the power of another:
base = 7 power = base ** 5 -
When using two variables to raise one numeric value to the power of another:
base = 5 exponent = 5 power = base ** exponent
Practice: Float Exponentiation
Based on the above information, please complete the following steps:
- Define a variable named
same_side_length, and assign it2.1. - Define a variable named
opposite_side_length, and assign it2.8. - Define a variable named
hypotenuse_lengthand, using operators (including exponentiation) and the variables above, assign it the length of the hypotenuse of the right-triangle above. - Print the value contained in the
hypotenuse_lengthvariable.- Note: if it's been a while since you calculated the hypotenuse of a right triangle, check out the formula here.
HINT*: Click here for a hint
You should see the length of the hypotenuse:
> 3.5
Note: the square-root of a number is equivalent to raising the number to the power of 0.5.
SOLUTION*: Click here for an example solution
same_side_length = 2.1
opposite_side_length = 2.8
hypotenuse_length = (same_side_length ** 2.0 + opposite_side_length ** 2.0) ** 0.5
print(hypotenuse_length)
When you have completed the above, you are ready to move on to the next exercise!