Mathematical Operations
Cool. Can python also do other mathematical operations? Let's try it out. You can use the following in the python language:
- Multiplication - *
- Division - /
- Addition - +
- Subtraction - -
- Modulo - % (Modulo operator gives the reminder after division)
Try playing around with these operators at the prompt. Don't worry, you will not break the computer. Type whatever you want and see what the computer displays.
Right now you have learnt to ask the computer to do math problems for you.
Let's try to make the computer do a math homework problem.
Problem: Raja has 5 apples. Rani has 4 apples. How many apples do both of them have together.
How do you generally solve this? You will do something like this on paper.
Number of apples Raja has=
Number of apples Rani has=
Total number of apples=Number of apples Raja has + Number of apples Rani has=5+4=
Now let's see how you would do this in python. Type exactly as below.
Number_of_apples_raja_has=5
Number_of_apples_rani_has=4
Total_apples=Number_of_apples_raja_has+Number_of_apples_rani_has
print(Total_apples)
See, so simple. It's like you wrote on paper. Only thing is there are some rules for python. If you see, "Number of apples rani has" became "Number_of_apples_rani_has". For now think that it's because python does not ike spaces too much. So we replaced the space with '_'. Or you could have used "Numberofapplesranihas". Anything is fine as long as space is not there. And the variable name should not start with a number or contain anything other than alphabets, numbers and '_'
Number_of_apples_rani_has is called as a variable in python.
So what did you do above.
You told the computer to store apples raja has in Number_of_apples_raja_has variable and apples rani has in Number_of_apples_rani_has variable.
Then you told the computer to add those two variables.
Finally you asked the computer to print the answer.
It's as simple as that. Tell the computer one step at a time, in a language it can understand and it will do the tasks and give you the result.