Harbola DataScience

Read and Post Coding related Questions!

Solve Physics with Python – Electrostatics Question

I was going through my first chapter of my ncert textbook. And saw a lot calculations going around. Lots and lots of formulae and units and dimensions. Which I thought could be done better a computer system than a human.

I tried to automate the calculation at very basic level without any intelligence or AI. Making it hardcoded and keeping it simple.

Prerequisite- If you are unfamiliar with coulombs law go through this article.

formula of coulomb law

So to achieve this I thought to assign some different variable(f(force), q1(charge1), q2(charge), r(distrance), Eo(epsilion) which can be changed as per question and used interchangbly in advanced part of this.

In advanced part we will also try to see how we can handle mis-matched units and converting them so to put them in the formula. For now we suppose that input will be given in SI units. And we’ll find the value of force for given charge and distance values. Further we will also see how to write intelligent code that automatically makes make formula for finding distance or charges on the basis of given input values.

find the value of force
hardcoded values for question 1 ncert coulomb’s law
# Python code
# user input based values
# force is equal to k*q1*q2/r*r  where k is equal to 9 * 10**9

q1 = 2*10**-7
q2 = 3*10**-7
r = 0.3
k = 9*10**9
nume = k*q1*q2
deno = r**2
ans = nume/deno

You can run this on your ide and see that you are getting output of 0.006 which is the right ans. 🙂 We successfully created our very basic code. And similarly for more formulae and concepts you can do the same. You can contact me to discuss more about this.

If you wanna try out with some random values and check the value of force that you get

can put your custom values.

You can make a gui app which will have a quite descent User interface with help of tkinter. If you want to take it to a next level and facing any difficulty to start or anything related, contact me @ chitranshuharbola@gmail.com


above code is straight forward. We are simply multiplying and dividing. Now we’ll make is little general so that it can be used is more questions. With single peice of code you can find any value. Be it force, charge or distance. Let us see how.

general code

python code –

try it yourself on ide
k = 9*10**9

ques = input("what value you want to find, charge? force? or distance? ")

if ques == "force":
    q1 = eval(input("enter the value to charge 1: "))
    q2 = eval(input("enter the value to charge 2: "))
    r = eval(input("distance btw charges: "))
    num = k*q1*q2
    deno = r**2 
    print("force is equal to: ",num/deno,"N")
if ques == "charge":
    f = eval(input("enter the value to force: "))
    q2 = eval(input("enter the value to charge 2: "))
    r = eval(input("distance btw charges: "))
    num = f*r**2
    deno = q2*k
    print("charge is equal to: ",num/deno,"C")
if ques == "distance":
    f = eval(input("enter the value to force: "))
    q2 = eval(input("enter the value to charge 2: "))
    q1 = eval(input("enter the value to charge 1: "))
    num = q1*q2*k
    deno = f
    ans = num/deno
    print("distance is equal to: ",ans**.5,"m")

Now it will ask you what you want to find and ans will be on your screen inputing required values.

Check this post :- 99+ Data Science Interview Questions for freshers

output –

you can see in first I find value of force and in second i tried to find distance with a single piece of code. Ans as expected got exact values. 🙂

We successfully built a program that automatically detect what formula to use. There could be some other easier ways of doing this but I tried a straight forward methord using if else ladder. Hope this helps. Post your views about this and questions in comment section. Reach out to me through my gmail.

Chitranshu Harbola

Self taught programmer, Web Developer and an aspiring Machine learning engineer cum Data Science student

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top