Python-How to do Algebraic Math with Exponents and Square Roots

2.2k Views Asked by At

so I am having issues trying to do basic math in Python. I can do basic math, but when I add in exponents, square roots, etc, I have errors with the IDE. How do I do this?

Here are a few of my problems that I am having issues with:

(n(n-1))/2)
(4)* pi * r ** 2=
(r(cos(a)**2) + r(sin(b))**2)**(.5)
((y**2) - (y**1))/((x**2) - (x**1))=
2

There are 2 best solutions below

0
On

You can use math.pow() or the simpler ** syntax:

There are two ways to complete basic maths equations in python either:

use the ** syntax. e.g:

>>> 2 ** 4
16
>>> 3 ** 3
27

or use math.pow(). e.g:

>>> import math
>>> math.pow(5, 2)
25.0
>>> math.pow(36, 0.5)
6.0

As you can see, with both of these functions you can use any real power so negative for inverse or decimals for roots.

In general, for these types of equations, you want to look into the math module. It has lost of useful functions and defined constants that you may find useful. In particular for your specific problems: math.pi and these trig. functions.

I hope these examples and the links I made are useful for you :)

0
On
  1. (n*(n-1))/2 should work if you have already given n a numeric value (e.g. n=2 or something). Your original expression has unbalanced parentheses (three closing parens and only two opening parens) and it is missing the multiplication sign (*) which is necessary in python, otherwise n(n-1) would be interpreted as the function n supplied with the input n-1, in which case you get a message like "TypeError: 'int' object is not callable", assuming you had previously defined n=2 or the like. It's telling you that the integer n cannot be called like a function, which is how it interprets n().

  2. To get pi (3.14159...) in python, you should import the math package and then use math.pi like this:

    import math
    r = 2
    x = 4*math.pi*r**2

    You don't need parentheses around the 4. Parentheses are used for grouping, when you need operations to be done in a different order than the standard order of operations. You don't need the trailing equal sign (that's a syntax error).

  3. In the third expression you are using implicit multiplication notation which is fine for pencil and paper but in python you need to use * every time you multiply. Also, you need to import the math package and then use math.sin and math.cos.

    import math
    a = 90
    b = 45
    x = (r*(math.cos(a)**2) + r*(math.sin(b))**2)**(.5)

  4. There doesn't appear to be anything wrong with the last expression except the trailing = sign which should be removed. Store the result of this expression in a variable if you want to keep it for future use:

    z = ((y**2) - (y**1))/((x**2) - (x**1))

    If you just type the expression at the command line it will print the result immediately:

    x = 3
    y = 2
    ((y**2) - (y**1))/((x**2) - (x**1))

    but if you are using this expression in a script you want to save the result in a variable so you can use it later or print it out or something: z = ((y**2) - (y**1))/((x**2) - (x**1))

    As was previously pointed out in the comments, x**1 is the same, mathematically, as x so it's not clear why you would want to write it this way, but it's not wrong.