What math can Python do that a calculator can’t?

Question

Is there any math I can do with Python that a standard calculator can’t do?

The cool thing about programming is that you can make use of code other people have written by using modules. So even if you can’t do something with basic Python, a module may exist to do a given type of math. Modules extend the abilities of your program by allowing you to use that code written by other people.

One example is the math module, which allows you to find the absolute value of a number, round numbers, and calculate factorial of a number, among other things. And this is only one simple module of the many available to you!

Lots of other modules exist for things like visualizing data, collecting and storing massive sets of data, and even getting random Star Wars facts!

16 Likes

So, in other words, a standard calculator cannot do things like add non-number items together. Or, duplicate concepts from non-number items in an exponential way.

5 Likes

Python is not a calculator. It is a programming language with the aid of the machine’s math co-processor.

``````>>> from math import factorial
>>> factorial(7)
5040
>>>
``````

That was too easy.

``````>>> from math import sqrt, pi
>>> sqrt(pi)
1.7724538509055159
>>>
``````

That made the machine work very hard, and we only see the half of the result the machine has in hold.

Python can do pseudo math such as repetition and splat on print statements.

``````y = x = 8
f = 1
for _ in range(y):
f = int(not f)
print (*(['.', '*'] * x)[f:x + f])
``````

A clever person might even get this to work on a graphing calculator. It’s all in the algorithm.

I’m still looking for the is prime button on my calclator…

11 Likes

how can i get the modules

1 Like

You can access modules with the import function

``````import sys
import math
# I Believe with this way you have to use the call function

math.sqrt(x)

``````

If you dont want to load all math modules and just one method
like mtf said.

``````from math import factorial
from math import pi
from math import sqrt
``````

But that way is a little redundant thats why he writes it like.

``````from math import sqrt, pi, factorial
``````

All basically the same idea.

2 Likes

Guys some clarification in here please

You can also define a float using scientific notation, with e indicating the power of 10:

this evaluates to 150:

float4 = 1.5e2

Is it like 1.5^10?
Isn’t “e” supposed to = 2.7

1 Like

No, that would be 1.5 raised to the 10th power.

``````1.5e2  =>  1.5 * 10 ** 2  =>  150.0
``````
1 Like

Hi MTF,

Please do bare with me a little bit more, what does “**” mean?
So that is like 1.5*(10^2), which means that e = 10
so 3e2 = 3(10*2) = 300 Right?
Thanks

Medo

Same thing as `^`. It is the exponentiation operator.

``````3 * 10 ^ 2 => 3 * 10 ** 2 => 3 * 10 * 10 => 300
``````

Order of operations gives precedence to the exponent over multiplication.

Not quite. `e` or `E` indicates a power of 10.

``````1 000 000  =>  1e6
``````

Please explain that again… I’m a little bit confused

So, basically, modules are libraries?

They are more succinct than that.

``````def foo(bar):
return bar
``````

The above is a module.

The namespace where we run code is itself a module.

Libraries are more what the name describes: a comprehensive collection of abstracted code concepts usually in the form of a class. From them we inherit all the methods that are built into them. This is on top of the Standard Library that Python runs on.

Bottom line, the terms are not synonymous. When a library is imported it occupies the same namespace as the standard library. It’s like it was always there. The language is broken into these segments so that memory is not compromised unless it absolutely needs to be. The core library is ample enough to do most things, language wise. Libraries add labor saving tools and significantly more horsepower than what we might compose in core terms.

1 Like