Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

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.

c = 5.0

LAB

Objectives

  • becoming familiar with the concept of storing and working with different data types in Python
  • experimenting with Python code.

Scenario

Here is a short story:

Once upon a time in Appleland, John had three apples, Mary had five apples, and Adam had six apples. They were all very happy and lived for a long time. End of story.

Your task is to: 

  • create the variables: john, mary, and adam
  • assign values to the variables. The values must be equal to the numbers of fruit possessed by John, Mary, and Adam respectively
  • having stored the numbers in the variables, print the variables on one line, and separate each of them with a comma
  • now create a new variable named total_apples equal to addition of the three former variables
  • print the value stored in total_apples to the console
  • experiment with your code: create new variables, assign different values to them, and perform various arithmetic operations on them (e.g., +, /, // , etc. ). Try to print a string and an integer together on one line, e.g., "Total number of apples:" and total_apples.

Solving simple mathematical problems

David Khieu
Module by David Khieu, updated more than 1 year ago

Description

Solving simple mathematical problems
No tags specified