How to cube a number

89,368

Solution 1

Actually different symbols mean different things in different programming languages. In some languages, ^ means exponent, but in Python, the exponent operator symbol is **:

>>> 3**3
27

The ^ symbol is for the bitwise 'xor' operation:

>>> 1^1
0
>>> 1^0
1

Read the documentation on the operator module to see how Python really treats these symbols.

Solution 2

Use the * key twice

def volume (v) :
    return v**3
volume(4)

Solution 3

You can use the ** operator to do exponential calculations.

def volume(v)
    return v**3
Share:
89,368
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    I just started using Python today for my class and one of my problems is cubing a number in Python. I know the way to do it is x^3, but that doesn't work in Python. I was just wondering how I would be able to do that.

    This is what I tried so far, but as you can see, I keep getting syntax errors:

    >>> def volume (v) :
        return v^3
    volume(4)
    SyntaxError: invalid syntax