Python as a Calculator
Back to basics it seems. I was told to use IDLE but.. I don’t want to. Instead I’m going to use the Python interpreter in Terminal because it’s provides the same function.
Also, I learned a new term. The >>> is called a prompt. These are the little things that I expect to learn as well as those knowledge gaps I mentioned in the Python Log 0.
In this video, we used Python as a calculator doing basic arithmetic using the basic operators: +, -, *, / as well as exponentiation ** and integer division //.
I don’t need to go into how +, -, *, / work. In the case of exponentiation, I always thought ^ would be the better character to use. There is probably a reason why ** is a better option but I just haven’t found it yet.
Found the reason though since I haven’t worked with it yet so.. here it is: https://wiki.python.org/moin/BitwiseOperators
For //, this makes sense in a way. If I’m dividing I want to know the accurate result so having decimal values makes sense. But what if I wanted to round to the closest whole number? Well we would need something to do that. Maybe a method like .round() (this may or may not be already in existence).
Yes .round() exists, I really got to memorize and use more methods: https://www.w3schools.com/python/ref_func_round.asp
Though just adding another backslash (//) makes more sense. The thing to remember is that if you use // and one of the values has a decimal number you’re going to get a decimal number returned.
5 // 2 = 2 but 4.9 // 2 = 2.0 where 4.9 / 2 = 2.45, so there’s that.
To note, whole numbers (0–9) are called integers while whole numbers with decimals numbers (0.0–9.9) are called floating point numbers, int and float respectively.
Now this operator % is called modulo which is meant to find the remainder. I rarely use this so I forgot it exists. 4 % 2 = 2 and 5 % 3 = 2.
Fun fact. If you want to know the fractional form of your result, for example 27 / 8:
(a) 27 // 8 = 3
(b) 27 % 8 = 3Take (a) as your whole number and (b) as your numerator giving you (a) (b)/(c) is 3 3/(c) where (c) is your divisor (8) so 3 3/8. How do we know this is correct?27 / 8 = 3.375
3 + (3 / 8) = 3.375Is it helpful to know this? Probably not, but it’s a nice to know and can make people 'ohh' and 'ahh' if they haven’t already figured it out. So I guess it’s for a slight ego boost.
Eventually we get into BEDMAS or PEDMAS which is essentially the rule you follow to evaluate an arithmetic expression properly.
Always in this order: Brackets (Parentheses), exponents, division or multiplication, addition or subtraction.
So for instance (4 + 6) + 9 ** 8 // 6 * 2–26 = x.
First step is to solve all brackets first: (4 + 6) = 10
10 + 9 ** 8 // 6 * 2–26 = xNow exponents: 9 ** 8 = 43046721
10 + 43046721 // 6 * 2–26 = xNow division and multiplication
[(43046721 // 6) * 2 = 7174453 * 2 = 14348906
10 + 14348906–26 = xNow addition and subtraction
[(10 + 14348906) — 26] = 14348916–26 = 14348890So 14348890 = xSimple.
When I find an equation I like to solve them. It’s fun and makes my day.
Next I was given two terms I knew and how to avoid those kinds of errors but not their meanings.
Syntax: the rules that describe valid combinations of Python symbols.
Semantics: the meaning of a combination of Python symbols.
Now if you asked me to define those two terms you would get a jumbled response, but if you asked me to correct a program that had syntax errors I could easily correct. Believe me, I get them all the time.
So that’s lesson 1 and I learned a bit. Was it fun? Kind of, I got to solve an equation and that’s always fun in my book.