Friday, April 14, 2017

Back in Business with Math and Variables!

For the first time in 3 weeks, I'm typing in Python again! Currently, I've finished chapter 2 of my programming book, which dealt with types, variables, and simple i/o and now I will be going on to chapter 3, which from the title teaches about "Branching, While Loops, and Program Planning". But so far from what I've learned from the past week, is the bread and butter of programming: Mathematics. Specifically how to program simple mathematical equations, such as the x+y=z variety. From my experience of learning Ti-BASIC and Applesoft, this is the point when programming begins to be useful and applicable, as we essentially recreate that tiny calculator that you get in first grade.

First of all we have the "+", "-", "/","//","%", and "*" operators to play around with. Writing an equation using these new operators in python is similar to writing it down and solving it on paper, but now the computer does all the thinking for you. For example, you can type in 5+5 and the computer will calculate the equation and determine that the answer is 10. The only problem is that the computer doesn't tell you the answer; you have to tell it to tell you the answer after it's solved it. This means that you have to use a print function, like you would in BASIC. So, what you end up typing in Print(5+5) and then the computer will give you the answer. 

An interesting thing about Python is that there are now Augmented Assignment Operators, which shorten down simple mathematical equations by attaching the "=" sign to the end of an operator . For example, x = x +5 becomes x+=5. This can help shorten and simplify larger equations that may make use of something else I've learned: variables! A variable by the way is a way to label and access information, and these things make programming a lot easier. By assigning a name for a specific value or a series of strings, you can use less space and run your program more efficiently. So if our program from above used variables, it would look something like this:

answer = 5 + 5
print(answer)

Another thing I learned, is that there are 2 different types of math in python and programming in general: integer and floating pointIntegers are whole numbers, which do not have a fractional part (numbers like 1, 27, 61, -100, 0, etc), while floating point numbers are numbers with a decimal point (numbers like 2.375, -99.1, 1.0, etc). Both are used for different purposes. Integer is used when we want a nicely rounded answer (the "//" operator is specifically used for this purpose), while floating point is used for accurate "true" answers. We can also convert existing values into Integers, floating points, or simple strings using the "float()", "Int()", and "Str()" functions respectively.

Here's a program that demonstrates everything I've learned so far:

# Challenge Program "Tipper"
# Demonstrates key programming concepts
# Calculates the necessary amount of a purchase for a 15% and 20% Tip
# Nathan Czaja, 4/14/2017

bill = int(input("what is your Bills total?"))

tip15 = bill*0.15
tip20 = bill*0.20
billtip15 = int(bill+tip15)
billtip20 = int(bill+tip20)

print("\a", "Total entered: $", bill, \
      "\n15% Tip: $", tip15, \
      "\nBill + 15% Tip: $", billtip15, \
      "\n\n20% Tip: $", tip20, \
      "\nBill + 20% Tip: $", billtip20)
      

input("\nPress enter to exit")


What this program does is calculate the amount of money needed for a 15% or a 20% tip. The operation of this program is pretty simple. For example, for variable bill, I can enter $200 dollars. The "int()" function turns the value entered into a usable integer, rather than a string that does nothing. Next, it gets multiplied with 15% and 20% to calculate the relative tips. The tips are then added to the price of the bill, to give a total owed. Finally, the next print() function displays the information calculated to the user.

The running program would look something like this:

what is your Bills total?200
Total entered: $ 200 
15% Tip: $ 30.0 
Bill + 15% Tip: $ 230 

20% Tip: $ 40.0 
Bill + 20% Tip: $ 240

Press enter to exit
>>>

These new tools are extremely helpful in STEM (Science Technology Engineering and Mathematics) applications, where equations run rampant and constant calculation becomes tiresome. Using Ohms Law (V = I *R) and the quadratic formula ( (-b+/-√(b^2 (4)ac))/2a) over and over agian can be repetitive and strenuous, so being able to use a machine to do the dirty work for you is a godsend, and is one of the reasons why computers came into being in the first place.

No comments:

Post a Comment