Tuesday 3 July 2012

How to be the smart student with Python

If you are a school student and currently learning or thinking about learning python, you should know how to hack your life with it. As a learner, I am trying to engulf the basics and I hope to offer you guys something better in the future when I actually learn something. For now I will show you some simple technique you can use to make your math life easier. You can make a simple program to solve complex equation that you know you have to do it in class repeatedly with python and use it whenever you need to solve or check a problem.

For the cool kids, this is not a cheat at all. If you can make a program about the equation, you will most likely know more about that equation than a regular students. This is because you have to break the equation down in step and understand it's method to solve it in steps. You will learn both programming and math at the same time.

Be warned, because I am not a real programmer and I am just a beginner in the python world. So consider my 'stupidity' in such. But anyway we need two tools for our project today. First is basics learning tool and another is software and compiler. Just read two to three chapter from the tutorial site and you will be able to make simple math program easily. If your math notation and formulas goes out of simple math, you can always Google it for how to use it. For math programming I recommend having a pen and paper next to you as well.




We will do an example of python program. To make a program we need a problem to solve. The problem is our math equation. Let's do a hard one first! We will solve the quadratic equation, which is:

And you should know there are two solution to this equation because of plus and minus. I am yet to figure our how to use that on python directly but I will use what I know so far. So let's make the equation simpler to get two result separately.

 
What we are gonna solve  from is ax² +bx+c

Ok, now let's go to the PyScripter>New Python module and delete everything. We need input for a b and c. But when we input something it becomes an 'string' (sentence/word) which we will use 'float'(number with decimal) to use it on computation. Anything after '=' meas to set a variable for that section and for us to remember the user input. Let's write first three line of our program.



a = float(input("Enter a: "))
b = float(input("Enter b: "))
c = float(input("Enter c: "))



Here we set some variable to remember the input and whatever inside " " will show in the pop up box. Run this with the little green play button fro the top and it should ask you for input.

Now to solve the problem and output them with print command. But since we broke down the equation into 2 parts, we will have to add two more variable and solve them separately. What we do next is:


x_1 = (((-b)-(((b**2)-(4*a*c))**.5))/2*a)
x_2 = (((-b)+(((b**2)-(4*a*c))**.5))/2*a)


And now to print them on the output:



print("X1 = ", x_1)
print("x2 = ", x_2)


Now if you run the program, it should definitely give you the right answer. But we don't want to load the editor to get the math solved. So, we will make it as command promt program. The only other thing you need to add now is pause command, so it will pause for the answer.

import os
os.system("pause")


Don't run! But save the program somewhere (.py) . Here's how your finished product look like:



If you have saved it, it's done! Now go ahead and double click it to see your program running as .exe and solve your math aster. I you want to solve repeatedly you can use some loop function with it as well or use it right from the editor. if you run it with double click, you should see something like this:



For another example, I created the program for Pythagorean theorem about right triangle. Which is a² + b² = c² . So here is the programm, I don't think it needs explanation, just look the program and it's fairly simple to engulf.



a = int(input("enter side a, write 0 for unknown"))
b = int(input("enter side b, write 0 for unknown"))
c = int(input("enter c, write 0 for unknown"))

d = (((c**2)-(b**2))**.5)
if a == 0:
    print("a = ", float(d))

if b == 0:
    print("b= ", float(((c**2)-(a**2))**.5))

if c == 0:
    print("c= ", float(((a**2)+(b**2))**5))

import os
os.system("pause")



Don't use the last part of the code if you want to use it right from the editor. You can learn more about math functions by searching on google :P And use this hack for your boring everyday repeated math and be more productive and feel smart!


No comments:

Post a Comment