**Here**, we are executing **Traditional (Simple) mini-computer and Magic Calculator utilizing Python3.**

We will fabricate a mini-computer program in this article utilizing python3. In the event that you scan for a python adding machine program on the web, you will discover huge numbers of them and the majority of the fundamental adding machine programs, similar to one **beneath**:

### 1) Traditional Calculator Program:

```
# Program make a simple calculator that can
# add, subtract, multiply and divide using functions
# This function adds two numbers
def add(x, y):
return x + y
# This function subtracts two numbers
def subtract(x, y):
return x - y
# This function multiplies two numbers
def multiply(x, y):
return x * y
# This function divides two numbers
def divide(x, y):
return x / y
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
# Take input from the user
choice = input("Enter choice(1/2/3/4):")
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if choice == '1':
print(num1,"+",num2,"=", add(num1,num2))
elif choice == '2':
print(num1,"-",num2,"=", subtract(num1,num2))
elif choice == '3':
print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == '4':
print(num1,"/",num2,"=", divide(num1,num2))
else:
print("Invalid input")
```

### Output:

### Clarification:

This number cruncher program is made utilizing basic if…else condition, we made 4 capacities (expansion, subtraction, duplication, and division) for our 4 unique activities.

We had a print direction for out fundamental outputs and activity variable stores the estimation of clients decision which a number and play out an activity related to that number and will deliver the outcome.

**Truly intriguing??? BOOO!!!!!** I realize this mini-computer can do just 4 activities and just can do one activity in its life cycle.

This is the thing that you can discover on the web, here in the next area we utilized a superior way to deal with manufacture an adding machine program.

### 2) Magic Calculator | A Better Approach:

**The thing to recall: **Before we start, simply realize we make use Regex (standard Expression) here, so in the event that you are inexperienced with it don’t stress we got you secured, we will traverse this as expected to exist apart from everything else.

In the cutting edge adding machine, that you have presumably found in windows and Linux these won’t approach you for picking an activity they perform undertakings you type.

What’s more, on hitting enter key it will create the outcome, and in this segment, we will make a number cruncher program.

### Program:

```
import re
print("Our Magical Calculator")
print("Type x to close")
previous = 0
run = True
def performMath():
global run
global previous
equation = ""
if previous == 0:
equation = input("Enter Equation: ")
else :
equation = input(str(previous))
if equation == 'x':
print("GoodBye!!!")
exit(0)
else:
equation = re.sub('[a-zA-Z,:()" "]',"",equation)
if previous == 0:
previous = eval(equation)
else:
previous = eval(str(previous) + equation)
while run:
performMath()
```

### Output:

### Clarification:

Alright… I concur it’s a ton to get a handle on so we will comprehend the above program in steps.

To begin with, we imported the Regex library, and some output for a client and characterize a variable like run = True and past = 0. In while we called our **performMath()** work we made previously. Since while is checking run so it’s an unbounded circle.

Presently, **performMath() **is our significant segment of the program as it the one playing out every one of the tasks.

In the initial two lines of **performMath()** we utilized worldwide watchwords with the goal that the run and past factor we will use in the capacity we alluded as a worldwide variable rather than work nearby factor.

On the off chance that past will be zero that is we just began our program we so our adding machine will request input else it will attach in the past condition itself.

In the second if…else case, it checks on the off chance that the client squeezed ‘x’ to stop on the off chance that not, at that point we called re.sub() that the regex work which takes 3 parameters 1.pattern 2.substitute 3.string. re.sub() is substitution which replaces ‘design’ with ‘substitute’ in the ‘string’.

Presently we will investigate some essential regex articulation we utilized above, we utilized [] (square sections) since it characterizes the arrangement of characters that need to remember for the example, and we can utilize hyphen (- ) to set the characters

to run wherein case we pick between beginning to end and start to finish including different images and supplanted them with clear spaces just to expel them from the condition string and put away it in the condition itself.

In the third if…else case, we checked if past holds an earlier computation if not to assess the condition with eval() work generally assess it in the wake of adding recently determined an incentive with the present condition.

**Note: **eval() work is something we shouldn’t use (when all is said in done) in our python code since it can assess whatever is passed as its parameter, which means it can really compute all increase, division, expansion and subtraction just as exponential and the ascertaining leftover portion too. Be that as it may, it has a clouded side too that we have to stay away from, that is eval() capacity can really

run any python order that went as its parameter regardless of whether it’s as basic as** a print() **it will execute the direction so as the security perspective we shouldn’t utilize it so we utilized Regex here to dispose of the considerable number of letters and keep just numerical characters.

In the event that you are interested in **Regex**, you can peruse here: Google python Regex