Python
Operators
Python has different types of operators for different operations. To create a calculator we require arithmetic operators.
Arithmetic operators
Operator | Operator Name | Example |
---|---|---|
+ | Addition | 15+7 |
- | Subtraction | 15-7 |
* | Multiplication | 5*7 |
** | Exponential | 5**3 |
/ | Division | 5/3 |
% | Modulus | 15%7 |
// | Floor Division | 15//7 |
Exercise
n = 15m = 7ans1 = n+mprint("Addition of",n,"and",m,"is", ans1)ans2 = n-mprint("Subtraction of",n,"and",m,"is", ans2)ans3 = n*mprint("Multiplication of",n,"and",m,"is", ans3)ans4 = n/mprint("Division of",n,"and",m,"is", ans4)ans5 = n%mprint("Modulus of",n,"and",m,"is", ans5)ans6 = n//mprint("Floor Division of",n,"and",m,"is", ans6)
Explanation
Here 'n' and 'm' are two variables in which the integer value is being stored. Variables 'ans1' , 'ans2' ,'ans3', 'ans4','ans5' and 'ans6' contains the outputs corresponding to addition, subtraction, multiplication, division, modulus and floor division respectively.
Exercise 2 - Create a Calculator
Create a calculator capable of performing addition, subtraction, multiplication and division operations on two numbers. Your program should format the output in a readable manner!
Thanks