3

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
user3795416
  • 65
  • 1
  • 1
  • 2
  • 1
    " I know the way to do it is `x^3`, but that doesn't work in Python." That's a contradiction per se. If that doesn't work in Python, it's obviously not the way to do. – glglgl Jan 24 '19 at 13:43

9 Answers9

20

Python uses the ** operator for exponentiation, not the ^ operator (which is a bitwise XOR):

>>> 3*3*3
27
>>>
>>> 3**3  # This is the same as the above
27
>>>

Note however that the syntax error is being raised because there is no newline before volume(4):

>>> def volume(v):
...     return v**3
... volume(4)
  File "<stdin>", line 3
    volume(4)
         ^
SyntaxError: invalid syntax
>>>
>>> def volume(v):
...     return v**3
...                  # Newline
>>> volume(4)
64
>>>

When you are in the interactive interpreter, the newline lets Python know that the definition of function volume is finished.

tripleee
  • 175,061
  • 34
  • 275
  • 318
3

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.

kojiro
  • 74,557
  • 19
  • 143
  • 201
2

Use the * key twice

def volume (v) :
    return v**3
volume(4)
Nobi
  • 1,113
  • 4
  • 23
  • 41
1

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

def volume(v)
    return v**3
Rafael Barros
  • 2,738
  • 1
  • 21
  • 28
1

Use two asteric's between the number and the power. Ex 2^5 in math is 2**5 in python. You can also do something along the lines of math.pow(100, 2) = 10000.0.

0

The Best way to do this is

cube = lambda x: x**3

cube(3)

but one another solution be like which result in the same

cube = lambda x: x*x**2

cube(3)

one another alter solution be like

math.pow(3,3)

all will return the cube of number 3.

Ajay Sivan
  • 2,807
  • 2
  • 32
  • 57
yogesh
  • 1
  • 1
0
def volume(v):
    result = v * v * v
    return result


print("enter a number to be cubed: ")
print(volume(int(input())))

or

def volume(v):
    result = v ** 3
    return result


print("enter a number to be cubed: ")
print(volume(int(input())))
hanumanDev
  • 6,592
  • 11
  • 82
  • 146
0

Your syntax is attempting to use ^ as the exponentiation operator, but you should be using the ** operator. For example:

>>> def volume (v) :
...   return v ** 3
...
>>> volume(4)
64
user212514
  • 3,110
  • 1
  • 15
  • 11
-1

I have tried this with using a simple print function, you can calculate the cube by using ** operators

a = int(input())
b = int(input())

print(a**3)
print(b**3)
Ajay Sivan
  • 2,807
  • 2
  • 32
  • 57