python-for-beginners-with-code
python-for-beginners-with-code
Easy Python Projects For Beginners with code
1 post
Don't wanna be here? Send us removal request.
Text
Easy Python Projects For Beginners with code
Build Your Calculator Using Python
Python is a great programming tool when playing with numbers and mathematical expressions. We will help you to make a simple python program run in the command-line. This is a tutorial that presents how to build your own calculator using a python program for beginners. We will use variables, conditional statements, math operations, functions for building our calculator.
STEP 1- Asking inputs from the user
Before moving to the tutorial I remind you, you have to install python on your computer. For accepting user-generated input from the keyboard, we have to use python’s built-in function input(). Inside parentheses of input() function for asking users, we can give a string. We have to input two numbers.
We have to input two numbers. For asking two numbers, we should give space at the end of the given strings. Because this gives a space between string and number.
num_1 = input('Enter your first number: ') num_2 = input('Enter your second number: ')
After writing two lines, save the program as calculator.py.We can run the program on the terminal by using the command python calculator.py.
Output:
Enter your first number:2 Enter your second number:5
Depending on our need, the input numbers can be either integer or float. For this, whole numbers are suitable. So, we use the int() function for converting inputs to the integer data type.
num_1 = int(input('Enter your first number: ')) num_2 = int(input('Enter your second number: '))
Now, we can give integers. It doesn’t show an error.
Enter your first number: 3 Enter your second number: 567
STEP 2- Adding Operators
Next we need to add our four mathematical operators,such as: + (Addition),- (Subtraction), * (Multiplication), / (Division).
Let’s look at how we can add two numbers within print().
num_1 = int(input('Enter your first number: ')) num_2 = int(input('Enter your second number: ')) print(num_1 + num_2)
After running the program,
Enter your first number: 2 Enter your second number: 5 7
Now, we use string formatters, for proper text format and getting feedback.
num_1 = int(input('Enter your first number: ')) num_2 = int(input('Enter your second number: ')) print('{} + {} = '.format(num_1, num_2)) print(num_1 + num_2)
When the program runs, we will get the output as,
Enter your first number: 2 Enter your second number: 5 2 + 5 = 7
So, use the same format for the rest of the mathematical operators.
num_1 = int(input('Enter your first number: ')) num_2 = int(input('Enter your second number: ')) # Addition print('{} + {} = '.format(num_1, num_2)) print(num_1 + num_2) # Subtraction print('{} - {} = '.format(num_1, num_2)) print(num_1 - num_2) # Multiplication print('{} * {} = '.format(num_1, num_2)) print(num_1 * num_2) # Division print('{} / {} = '.format(num_1, num_2)) print(num_1 / num_2)
If we run the above code, it will take all the operators. For avoiding such situations, we want to limit the operators, ie; only run one operator at a time. So, we have to use conditional statements.
STEP 3- Conditional Statements
We are giving freedom to the user for selecting the operator. So, we use the input() function for getting an operator symbol from the user.
operation = input(''' Please type in the math operation you would like to complete: + for addition - for subtraction * for multiplication / for division ''')
Now we are using conditional statements. Here we use one if statement, three else-if statements and one else statement. Else statement is used for handling the error or invalid entry.
operation = input(''' Please type in the math operation you would like to complete: + for addition - for subtraction * for multiplication / for division ''') num_1 = int(input('Enter your first number: ')) num_2 = int(input('Enter your second number: ')) if operation == '+':    print('{} + {} = '.format(num_1, num_2))    print(num_1 + num_2) elif operation == '-':    print('{} - {} = '.format(num_1, num_2))    print(num_1 - num_2) elif operation == '*':    print('{} * {} = '.format(num_1, num_2))    print(num_1 * num_2) elif operation == '/':    print('{} / {} = '.format(num_1, num_2))    print(num_1 / num_2) else:    print('invalid operator, try again.')
Let’s see the output for * operation.
Please type in the math operation you would like to complete: + for addition - for subtraction * for multiplication / for division * Please enter the first number: 2 Please enter the second number: 5 2 * 5 = 10
We are aiming for a fully functional program. But now it is impossible because we can’t perform first, second and fourth operations without using this code again. Make it possible using functions.
STEP 4- Defining Functions
Let’s put our code inside a function named as to calculate(). For running this program, we have to call this function from the bottom. Consider another important part of this program, the user has a choice to continue or discontinue the calculation. For this, we are adding another function called def calculate_again(). Add this on below def calculate().
def calculate():    operation = input(''' Please type in the math operation you would like to complete: + for addition - for subtraction * for multiplication / for division ''')    num_1 = int(input('Please enter the first number: '))    num_2 = int(input('Please enter the second number: '))    if operation == '+':        print('{} + {} = '.format(num_1, num_2))        print(num_1 + num_2)    elif operation == '-':        print('{} - {} = '.format(num_1, num_2))        print(num_1 - num_2)    elif operation == '*':        print('{} * {} = '.format(num_1, num_2))        print(num_1 * num_2)    elif operation == '/':        print('{} / {} = '.format(num_1, num_2))        print(num_1 / num_2)    else:        print('invalid operator, try again.')    # Add again() function to calculate() function    again() def calculate_again():    calcu_again = input(''' Do you want to calculate again? Please type Y for YES or N for NO. ''')    if calc_again.upper() == 'Y':        calculate()    elif calc_again.upper() == 'N':        print('See you later.')    else:        again() calculate()
You can run the program ‘calculator.py’ using the command ‘python calculator.py’.Now users can calculate as much they need without rewriting the code.
Conclusion
This tutorial will help you for creating your own calculator using python on the command line. This is a simple program to learn the basics for beginners. Try to learn more under proper guidance. Learn Python Programming from expertise. Demand for python developers are on the hike and the average junior Python developer salary is $89,776.
8 notes · View notes