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


#1

Question

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

Answer

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!


FAQ: Learn Python - Python Syntax - Numbers
#2

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.


#3

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…