Using Python as a Calculator

Python language can be used as a calculator to compute different arithmetic operations (such as addition, subtraction, multiplication, and division). Python can also be used to perform, logical, trigonometric, mathematical, and statistical computations. Let us start with using Python as a Calculator.

Arithmetic Operators

The basic Python arithmetic operators are +, -, *, and/. These operators can combine objects and operators to make expressions refer to an object with a type. These operators help us to perform daily life computations.

5 + 4  # addition of two or more numbers
4 - 4  # subtraction of two ore more numbers
4 * 5  # multiplication of two or more numbers
5 / 4  # division of two or more numbers

5 // 4  # gives quotient
5 % 4   # gives the remainder (modulo)
5 ** 2  # raised to the power

Comparison Operators

The comparison operators (such as <, >, !=, <=, >=, and ==) are used to make a comparison between numbers. These operators result in either True or False values.

5 == 4  # equality 
5 != 4  # not equal to
5 > 4   # greater than 
5 <  4  # less than 
5 <= 4  # less than or equal to 
5 >= 4  # greater than or equal to

Type of Objects in Python

The type() function can be used to check the object type of the variable.

type(4)                  # integer
type(3.4)                # float
type("Python Workshop")  # string 
type(True)               # boolean
type(None)               # none Type
type("1, 2, 3, 4")       # string
type(5 != 4)             # boolean

Variables in Python

In computer, a variable is a name that refers to a value. Variables are used to store different types of objects, that is variables are used to store different types of data information such as integers, floats, strings, and boolean. The variables created/ defined in a language can be further used to perform different computations such as the addition of values stored in variables. One can also compute the square root of the value stored in a variable or can perform any other required calculations such as log, e raised to a power, sine, cosine, or tangent of an angle, etc.

x = 5
y = 2
print(x / 5)  # divides the value of x by 5
print(x % y)  # finds the remainder after dividing value of x by value of y

Note that the print() function is used to display the computation on a display device (such as a monitor or computer screen).

One can define different types of variables and then develop the required computational formula to perform real-life applications. In the example below, a formula is developed to compute the area of a circle.

title = "Finding the Area of a Circle"
pi = 3.14159
radius = 5
area = pi * (radius **2)

Note that no output will be displayed from the above line of codes. It is because no print function is used or no variable name is written on a single line. To get the final computation stored in the $area$ variable, one needs to use print() function.

print("\nArea of the certain circle is\n", area)
Using Python as a Calculator

The string and variable $area$ are used as an argument to the print() function.

Variable Naming Conventions

Variables have types which are the types of values the variable refers to.

  • Variable names must begin with letters
  • Variable names are case-sensitive in Python (x and X are different variables)
  • Variable names can contain letters and numbers
  • Underscore can be used to allow multiple words
  • Python-reserved keywords cannot be used as variable names

Note that variables’ names are names only, they have nothing to do with computation, however, they may help to understand the object, code, and computation being performed. It means that you can use any word or character to use it as a variable, but a variable should be descriptive and self-explanatory.

x = 2
y = 5
print ('Square of x is', x * x)
print ('y =', y)
print ('x + y =', x + y)

# output
Square of x is 4
y = 5
x + y = 7

Rounding of Numbers

The numbers can be rounded off if there are a lot of digits after decimal.

round(250.23435)     # round to the nearest whole number


round(250.23435, 1)  # round to 1 decimal place

# output

round(256.3435, -1)  # round to the 10's place


Mathematical Functions in Python

There are many mathematical functions available in the math module. You need to import the math module.

# first import the math module
import math

math.log(5) 		 # log bas 10
math.log(5, 2) 		 # log base 2
print(math.exp(10))  # e^(10) raises "e" to the power of its argument
# or print(2.7182**10) 

math.sqrt(64)		 # square root of 64
abs(-30)			 # absolute value of -30

math.floor(2.0)		 # rounds a number DOWN to the nearest integer
math.ceil(2.2)		 # rounds a number UP to the nearest integer
math.pi				 # returns the pi value

Creating Multiple Variables in Python

In Python, multiple variables in a single statement can be created. For example,

x, y = 2, 5
print("x =", x, "\ny=", y)

# output
x = 2 
y = 5

Another example of creating multiple variables in a single statement.

import math
a, b, c = 3, math.pi, math.sqrt(2)
Multiple variables in Python