In the spirit of the SuperBowl tomorrow and Tax Season here in the US, I thought I’d follow up / end my review of Mathematical gotchas in Python coding, and how to make a practical application (at the very bottom) that I whipped up off the top of my head as mock Tax calculator but with a little more work could be a real application.
One thing I also wanted to beat over the head is the term Concatenation as I can barely even say it, from what I’ve read, Concatenation is basically a different way of saying you are “combining” strings to produce a result that does not throw errors while executing.
Input from programs is a value and NOT an integer unless specified!
From my last program I wanted to focus on one line that I didn’t nail in my haste to just get some notes down, as I wanted to get something going again with my studies here:
Actually I suppose you need some context so 3 lines of my previous code.
What is happening on Line 12 is that there is a “String” here that contains a mathematical equation, however input is never an Integer, for example:
The input is the String ‘8’ and not the integer just above, however in the VSC Line 12 we are “Concatenating” or kind of baking in that String to Integer conversion to allow the output to provide a mathematical or Integer result.
For example, we could also achieve this by adding a second line as shown here rather than concatenation in the line:
We have already assigned the the input Variable ‘Pizza’ a value of ‘8’ in string format, and then we told Python that the String of ‘8’ is an Integer Value, where as Concatenation is seen in the program of myAge + 1 being ‘baked into’ the program line with:
str(int(something) + 1)
This tells the program the value input for myAge or ShoeStr1ng is an Integer Value and not a String, however my infamous Curiosity killed the Cat question of the day – What if a Variable is entered after an Input is entered:
Shown here a Variable defined does override input, and as seen with / “Division” that will produce a float or decimal value, the Integer Division (Floored Quotient) // says noone gets ANY Pizza!
I had a second Cat Curiosity moment – Is it an order of how they are input?
YES IT DOES! If a Variable is hard coded that Pizza = 1 and a later line asks for input as to the value as a String of what Pizza is, it will overwrite the hard coded variable!
So order of operation is key when writing programs, I imagine this is kind of common sense if your studying programming, but on exam day that may be something to watch for as its something you may not think about because its so obvious its easy to overlook!
This actually leads into the next detail about Variables and Integers:
If it is assigned as a Variable, it IS an Integer value as demonstrated below
A quick example building off my Pizza, need some Soda to wash it down:
So we all get 2 slices of Pizza, and 3 cans of Soda for the SuperBowl tomorrow 🙂
I had forgotten that when assigned to a Variable, # values are Literal, and thus will be considered Integers whereas ‘input’ programs will take the input as a string that you can manipulate to be any of the following data types to program with:
When looking at these having just done my own taxes, I figured I would try my hand at writing a basic Tax program in Python using Floats this time rather than Integers:
Yes the file is Pizza.py (get it?), and actually found there is of course a cleverly named extension or program called Pizza Py, and even a restaurant named this as well so I was not the first to get around to this joke unfortunately.
Once again on line 7 I am doing concatenation turning the input value of ‘TotalPay’ into a Floating Integer as I asked for a decimal value input, which then allows it to be divisible by 3, telling the person how much 1/3rd of their total pay is that they now owe in taxes.
It tells me I owe the IRS some money, asks my total income for 2019 which isn’t very much in this example, then tells me how much I owe in taxes as the output.
Syntax could be cleaned up a bit, but its things like this that you can actually make some practical use programs pretty quick with little knowledge and some logic.
That is it for this article, just wanted to round off some of the basic programming!
I’ve had a very difficult time finding a groove studying programming along with Linux, so my articles have been a bit more sparse / short as it sometimes takes me a long time to wrap my brain around a concept, but I will continue pushing through a byte at a time 🙂