Python Tutorial: Building a Calculator Function for Basic Arithmetic
Python Tutorial: Building a Calculator Function for Basic Arithmetic
Hey everyone! Today, we're going to learn how to take two numbers as input and perform basic arithmetic operations using functions in Python.
Step 1: Getting User Input
First, we need to capture two numbers from the user. We'll use the input() function, convert the results to integers, and print them out.
user_input_one = int(input('Enter number A: '))
user_input_two = int(input('Enter number B: '))
print(user_input_one)
print(user_input_two)
Simple, right? We take the input, store it in a variable, and display it. While one-off calculations don't always need variable names, it's best practice to name them if you plan to reuse them or pass them into functions later.
Step 2: Basic Arithmetic
Now, let's perform addition, subtraction, multiplication, and division with these inputs.
user_input_one = int(input('Enter number A: '))
user_input_two = int(input('Enter number B: '))
print(user_input_one + user_input_two)
print(user_input_one - user_input_two)
print(user_input_one * user_input_two)
print(user_input_one / user_input_two)
If I enter 1 and 2, the output looks like this:
3
-1
2
0.5
Note: This tutorial is based on Python 3. Python 2 might give you different results (especially for division).
Step 3: Implementing Functions (The Black Box)
Performing calculations directly is fine, but in real-world programming, we use Functions. Think of a function as a "Black Box": you provide an input, the box processes it, and it spits out a result.
Why use functions?
Organization: It makes your code cleaner and more structured.
Reusability: You can use the same logic multiple times without rewriting it.
Readability: It’s much easier for other developers to understand your logic.
Let's create a function that handles all four operations at once:
def cal_result(a, b):
print(a, '+', b, '=', a + b)
print(a, '-', b, '=', a - b)
print(a, '*', b, '=', a * b)
print(a, '/', b, '=', a / b)
user_input_one = int(input('Enter number A: '))
user_input_two = int(input('Enter number B: '))
cal_result(user_input_one, user_input_two)
Crucial Tip: In Python, you must declare a function before you call it. If you move the def block to the bottom of your script, you'll run into a NameError. Python reads code from top to bottom, so always "Define First, Use Later."
Step 4: Adding Logic with Conditional Statements
What if we only want to perform one specific operation at a time? We can update our function to handle an operator (+, -, *, /) using if-elif statements.
def cal_result(a, b, op):
if op == '+':
return a + b
elif op == '-':
return a - b
elif op == '*':
return a * b
elif op == '/':
return a / b
user_input_one = int(input('Enter number A: '))
user_input_two = int(input('Enter number B: '))
user_input_op = input('Enter operator (+, -, *, /): ')
result = cal_result(user_input_one, user_input_two, user_input_op)
print("The result is", result)
Now, the program only displays the result for the specific operator you chose!
There's no "single right way" to handle the output—you can print it inside the function or return it to the caller. It all depends on your specific needs.
This is a basic implementation and doesn't include error handling (like division by zero). For more advanced Python tips, check out my other posts!