2.1.4.6 Variables - data-shaped boxes

Solving simple mathematical problems

Now you should be able to construct a short program solving simple mathematical problems such as the Pythagorean theorem:
The square of the hypotenuse is equal to the sum of the squares of the other two sides.
The following code evaluates the length of the hypotenuse (i.e., the longest side of a right-angled triangle, the one opposite of the right angle) using the Pythagorean theorem:
a = 3.0 b = 4.0 c = (a ** 2 + b ** 2) ** 0.5 print("c =", c)
Note: we need to make use of the ** operator to evaluate the square root as:
 (x)  = x(½)
and
c = √ a2 + b2 
Can you guess the output of the code?
Check below and run the code in the editor to confirm your predictions.
 


a = 3.0
b = 4.0
c = (a ** 2 + b ** 2) ** 0.5
print("c =", c)
  • Console 
c = 5.0

Comments

Popular Posts