Example HTML page

Python as a Calculator

Python as a calculator

In Python we can perform different evaluations with ease by using IDLE, which is known as Interactive Development Environment of Python. Let us discuss the following:

Arithmetic Operations

The arithmetic operations include addition, subtraction, multiplication and division. For example-

>>> 2+3
5
>>> 7-4
3
>>> 5*6
30
>>> 27/9
3.0
>>> 2+3*4-5
9
>>>

If we give only one operator then the respective operation will be performed easily. But in the case of multiple operators (in the above code 2+3*4-5) then the expression is evaluated on the basis of operator precedence.

Calculating Floating point expressions

In Python we can perform calculations on floating point numbers and this is shown in below:

>>> 3.2+1
4.2
>>> 4-3.7
0.2999999999999998
>>> 2.234*45
100.53
>>> 6.2/1.7
3.647058823529412
>>> 1*float(3.5+4.4)**3
493.03900000000004
>>> int(3.72)                     #We can convert floating point number into integer by using int( )
3
>>> int(2.19)
2
>>> round(3.8)	Conversion of float numbers by using round ( )
4
>>> round(3.2)
3

Truth testing

You can check if a value is equal to other value.

>>> 4==4
True
>>> 4==5
False
>>> 4<5	#You may also check a value by using inequalities
True
>>> 4>5
False
>>> 4<=5
True
>>> 4>=5
False                                                              

Working with Powers

We can express the power of a particular number by using ** or with the pow( ).

>>> 4**2
16
>>> pow(4,2)
16
>>> 4**(1/2)                            #for fractional numbers
2.0
>>> pow(4,1/2)
2.0
>>> 1.3*10**4                        #for scientific notations like 1.3 × 104
13000.0
>>> 2.7e4	  #Using E, that represents ten times to the power of number
27000.0

Working with Complex numbers

Complex numbers are those which exist in the form of a+ib or a+bi. As integers and floating point numbers, the complex numbers also supports the arithmetic operations by using +,-,* and / operators.

Ex:

>>> c=2j
>>> type(c)
<class 'complex'>
>>> c+c	#Complex operators
4j
>>> c*c
(-4+0j)
>>> c-c
0j
>>> c/c
(1+0j)
>>> b=2+3j	#sum of its real part and imaginary part
>>> b
(2+3j)
>>> d=4+6j
>>> d
(4+6j)
>>> b+d
(6+9j)
>>> b-d
(-2-3j)
>>> complex(4,5)
(4+5j)
>>> x=complex(-2,0.7)	   #Type conversion from int and float numbers to complex numbers.
>>> x
(-2+0.7j)


Working with Trigonometry

To access the trigonometric functions first we need to import math module. The import is a key word in python that helps to import the related modules. The math module is one amongst those modules. It comprises of various mathematical functions. Here we have shown two example functions in math module.

Ex 1:

>>> import math
>>> math.sin(30)
-0.9880316240928618
>>> math.tan(math.pi)
-1.2246467991473532e-16

Ex 2:

In interactive mode, the last printed expression is assigned to the variable ‘_’. This means that when you are using Python as a desk calculator, it is somewhat easier to continue calculations, for example:

>>> tax=13.7/100
>>> price=100.50
>>> price*tax
13.768499999999998
>>> price+_
114.2685
>>> round(_,3)
114.269

 

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest