Python 如何对一个数字进行立方
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24519122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to cube a number
提问by user3795416
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.
我今天刚开始在课堂上使用 Python,我的问题之一是在 Python 中对数字求立方。我知道这样做的方法是x^3
,但这在 Python 中不起作用。我只是想知道我怎么能做到这一点。
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
回答by kojiro
Actually different symbols mean different things in different programming languages. In some languages, ^
means exponent, but in Python, the exponent operator symbol is **
:
实际上,不同的符号在不同的编程语言中意味着不同的东西。在某些语言中,^
表示指数,但在 Python 中,指数运算符符号是**
:
>>> 3**3
27
The ^
symbol is for the bitwise 'xor' operation:
该^
符号用于按位“异或”运算:
>>> 1^1
0
>>> 1^0
1
Read the documentation on the operator moduleto see how Python really treats these symbols.
阅读有关operator 模块的文档以了解 Python 如何真正处理这些符号。
回答by Rafael Barros
You can use the **
operator to do exponential calculations.
您可以使用**
运算符进行指数计算。
def volume(v)
return v**3
回答by Rafael Barros
Python uses the **
operatorfor exponentiation, not the ^
operator(which is a bitwise XOR):
Python 使用**
运算符来求幂,而不是^
运算符(它是按位异或):
>>> 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)
:
但是请注意,由于之前没有换行符,因此会引发语法错误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.
当您在交互式解释器中时,换行符让 Python 知道函数定义volume
已完成。
回答by Nobi
Use the *
key twice
使用*
钥匙两次
def volume (v) :
return v**3
volume(4)
回答by Nobi
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
.
在数字和幂之间使用两个星号。2^5
数学2**5
中的Ex在 python 中。你也可以做一些类似的事情math.pow(100, 2) = 10000.0
。
回答by yogesh
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.
all 将返回数字 3 的立方体。
回答by Shavaiz Shams
I have tried this with using a simple print
function, you can calculate the cube by using **
operators
我已经用一个简单的print
函数试过了,你可以使用**
运算符来计算立方体
a = int(input())
b = int(input())
print(a**3)
print(b**3)
回答by hanumanDev
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())))
回答by user212514
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