Solving math problems using python (Quick Code-Python)

Shreedhar Vellayaraj
Geek Culture
Published in
4 min readAug 31, 2021
(credit: Quanta Magazine)

This is the second chapter of the Quick Code-Python series, to recap the last blog we’ve seen how to assign values to a variable, operation precedence, how BODMAS helps python calculate equations, variable names that we can and cannot use, and also different data types available in python. Click here to check that out.

Note: I’ve written the Quick Code-Python series in a linear fashion so that even a newbie can follow along the series to get a grasp of what python can actually do. For folks that are good with the fundamentals, you can follow along with the series to refresh your concepts or choose any of the chapters in the series that you want to know more about.

In this blog, we’re going to put all the theories into practice. There are some fun problems that we could easily solve using python. The main goal of this blog is to be able to convey how easy and simple it is to get results for a problem using python.

A. Calculating the Area and Perimeter of a Rectangle

Area of a rectangle: a*b

Perimeter of a rectangle: 2*(a +b)

a = 2
b = 3
area = a*b
perimeter = 2 * (a+b)
print(area) #printing the area
O/P : 6
print(perimeter)#printing peri
O/P : 10

B. Using BODMAS to solve a simple equation

Brackets Orders Division Multiplication Addition Subtraction

x = 14
y = 23
z = 6
f = 10
answer = (x — y) * z / f
print (“Result =”, answer)
O/P : Result = -5.4e = ((x - y) * z) / f
print("Result =", answer)
O/P : Result = -5.4e = (x - y) * (z / f);
print("Result =", answer)
O/P : Result = -5.4

C. Testing if a number is even or odd

It’s very easy to find if a number is even or odd. Just apply the modulus operator that would give you the remainder of the division.

If the reminder is one, then it is odd

if the reminder is zero, then it is even

another way to find even or odd 😂 (credit: me.me)
a = 6
b = 2
c = a%b
print(c)
O/P : 0 # number is evena = 7
b = 2
c = a%b
print(c)
O/P : 1 # number is odd

We can, of course, add some lines in the print statement saying the number is even or odd but for now, just for understanding purposes I’ll put them in a comment line(#….)

D. (Task 1) takes a number and squares it. (Task 2) take the square root of the result you get from the task(1)

#(task 1)take a number and square itx = 6
y = x**2
print(y)
O/P : 36#(task 2) take square root of the result you get from task(1)import math #math library
z = math.sqrt(y) #using sqrt()
print(z)
O/P : 6

We use a library called math, which is a built-in python library to do quick mathematical operations. The function math.sqrt takes the square root of the result y that we got from (task 1)

E. Calculate the area and volume of the Cube.

Area of a Cube : 6*a*a

Volume of a Cube : a*a*a

a = 30
area = 6 * a * a
volume = a * a * a
print(area)
O/P : 5400
print(volume)
O/P : 27000

F. Increment a number by 5 times

Take a number eg. 6 and add +1(increment) to it five times

There are two methods to do this,

# method number one: 
x = 6
x+=1
x+=1
x+=1
x+=1
x+=1
print(x)
O/P : 11#method number 2
x = 6
x+=5 #increment 5 times
print(x)
O/P : 11

As a programmer, we are always looking for the least lines of code to write in order to solve a problem, so that it improves our code presentation, helping us to debug easier. I suggest using method 2 to increment numbers.

G. Write a code to solve the equation z = |x − y| * (x + y)

x = 4
y = 3
z = abs(x-y) * (x+y)
print(z)
O/P : 7

H. Converting Celsius to Fahrenhit

The formula to convert celsius to fahrenhit is,

F = 9/5* C +32

Celsius = 45
Fahrenheit = 9.0/5.0 * Celsius + 32
print(Fahrenheit)
O/P : 113.0

I. Converting KMPH to MPH

The formula to convert KMPH to MPH is,

MPH = 0.6214 * KMPH

kmph = 100
mph = 0.6214 * kmph
print(mph)
O/P : 62.13999999999999

That’s all for this blog, hope you’ve learned easy ways to solve problems using python. Check out the previous chapter here of the Quick Code-Python series.

Stay tuned :)

--

--