How do I use exponentiation in a function?


Continuing the discussion from Can you please confirm if the psuedocode I propose for adding python variables is correct?:

Please leave the following link in the post so we can find the unit module, else replace it with a link to the exact lesson:

Strings and Console Output

Can you write an example of exponentiation in a function?

the operator is **=

from math import pi

r = 1
a = pi * r ** 2


What is the psuedocode associated with this? are you importing pi from somewhere else, I assume?


pi is a constant, a reasonable approximation which is found in the math module.


does that include all the digit after the period 3.14xxxxxxxxx…

I could substitute 2 for that number, just to make it simpler?


I just used a well known formula for calculating area of a circle.

Area_of_circle = PI times radius squared

Area of a square is side times side, or side squared.

Area_of_a_square = side ** 2
>>> from math import pi
>>> pi

The actual stored value may have even greater precision out to 38 decimal places (or more?).


from math import pi

r = 1
a = pi * r ** 2

The above is yours…

The below is mine:

def circle_area(number):
   return pi*number**2

Are they the same? If I put your formula in a function, would it work?


Yes, exactly. That is how we would write it.

Mathematically speaking,

A = pi * r ** 2

is a function derived from calculus.

y = f(x)
f(x) = pi * x ** 2

Look familiar?

def f(x):
    return pi * x ** 2

y = f(x)
f(x) = pi * x ** 2
def f(x):
    return pi * x ** 2

What is the y about?


It’s the common way to write a function in maths.

y = f(x)


y is a function of x

The value of y depends upon the value of x when the relation is applied to it.

y is proportional to the square of x times a constant (pi, in this case)

Let’s take a look at classical physics as it describes gravity (Newton)

Fg = G * m1 * m2 / d ** 2

The force of gravity between two centers of mass is proportional to the product of the two masses divided by the square of the distance between them, times the gravitational constant, G.

This is vague, but nothing a little reading won’t elucidate.


I was just confused with the nomenclature. I’ve jut been taaught to make very clear variables and if you havent been in grade school for a while, you forget. but it makes sense given the explanation.


In programming, yes, giving variables names that describe what they reference is always wise. In maths we are using pencil and paper (or pen and whiteboard) and deal in concepts that have representative symbols. The language of math is not very verbose owing to its cerebral (theoretical) nature. A program doesn’t think or remember concepts. That’s why we need to spell it out so a reader (that includes us) can make sense of it.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.