python中如何进行开方运算

Python09

python中如何进行开方运算,第1张

1、python中使用pow函数求n的n方根。首先打开python的编辑器,新建一个python 3的文件:

2、pow函数的用法很简单,只要传入待开方的数,以及要开几次方就可以了。比如演示里是3开3次方:

3、然后需要编译运行,点击菜单栏上run下面的run命令,执行编译运行:

4、在下方的结果中即可看到运算的结果尾27,说明是是正确的。以上就是python中开N次方的操作方法:

先来看一下 math 模块中包含内容,如下所示:

接下来具体看一下该模块的常用函数和常量。

ceil(x)

返回 x 的上限,即大于或者等于 x 的最小整数。看下示例

floor(x)

返回 x 的向下取整,小于或等于 x 的最大整数。看下示例:

fabs(x)

返回 x 的绝对值。看下示例:

fmod(x, y)

返回 x/y 的余数,值为浮点数。看下示例:

factorial(x)

返回 x 的阶乘,如果 x 不是整数或为负数时则将引发 ValueError。看下示例:

pow(x, y)

返回 x 的 y 次幂。看下示例:

fsum(iterable)

返回迭代器中所有元素的和。看下示例:

gcd(x, y)

返回整数 x 和 y 的最大公约数。看下示例:

sqrt(x)

返回 x 的平方根。看下示例:

trunc(x)

返回 x 的整数部分。看下示例:

exp(x)

返回 e 的 x 次幂。看下示例:

log(x[, base])

返回 x 的对数,底数默认为 e。看下示例:

常量

tan(x)

返回 x 弧度的正切值。看下示例:

atan(x)

返回 x 的反正切值。看下示例:

sin(x)

返回 x 弧度的正弦值。看下示例:

asin(x)

返回 x 的反正弦值。看下示例:

cos(x)

返回 x 弧度的余弦值。看下示例:

acos(x)

返回 x 的反余弦值。看下示例:

decimal 模块为正确舍入十进制浮点运算提供了支持,相比内置的浮点类型 float,它能更加精确的控制精度,能够为精度要求较高的金融等领域提供支持。

decimal 在一个独立的 context 下工作,可以使用 getcontext() 查看当前上下文,如下所示:

从上面的结果中我们可以看到 prec=28,这就是默认的精度,我们可以使用 getcontext().prec = xxx 来重新设置精度。接下来通过具体示例看一下。

基本运算

执行结果:

上面结果是用了默认精度,我们重新设置下精度再来看一下:

执行结果:

random 模块可以生成随机数,我们来看一下其常用函数。

random()

返回 [0.0, 1.0) 范围内的一个随机浮点数。看下示例:

uniform(a, b)

返回 [a, b) 范围内的一个随机浮点数。看下示例:

randint(a, b)

返回 [a, b] 范围内的一个随机整数。看下示例:

randrange(start, stop[, step])

返回 [start, stop) 范围内步长为 step 的一个随机整数。看下示例:

choice(seq)

从非空序列 seq 返回一个随机元素。 看下示例:

shuffle(x[, random])

将序列 x 随机打乱位置。看下示例:

sample(population, k)

返回从总体序列或集合中选择的唯一元素的 k 长度列表,用于无重复的随机抽样。看下示例:

参考:

https://docs.python.org/3/library/numeric.html

>>>dir(turtle)['Canvas', 'Pen', 'RawPen', 'RawTurtle', 'Screen', 'ScrolledCanvas', 'Shape', 'TK', 'TNavigator', 'TPen', 'Tbuffer', 'Terminator', 'Turtle', 'TurtleGraphicsError', 'TurtleScreen', 'TurtleScreenBase', 'Vec2D', '_CFG', '_LANGUAGE', '_Root', '_Screen', '_TurtleImage', '__all__', '__builtins__', '__doc__', '__file__', '__forwardmethods', '__func_body', '__methodDict', '__methods', '__name__', '__package__', '__stringBody', '_alias_list', '_make_global_funcs', '_math_functions', '_screen_docrevise', '_tg_classes', '_tg_screen_functions', '_tg_turtle_functions', '_tg_utilities', '_turtle_docrevise', '_ver', 'acos', 'acosh', 'addshape', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'back', 'backward', 'begin_fill', 'begin_poly', 'bgcolor', 'bgpic', 'bk', 'bye', 'ceil', 'circle', 'clear', 'clearscreen', 'clearstamp', 'clearstamps', 'clone', 'color', 'colormode', 'config_dict', 'copysign', 'cos', 'cosh', 'deepcopy', 'degrees', 'delay', 'distance', 'done', 'dot', 'down', 'e', 'end_fill', 'end_poly', 'erf', 'erfc', 'exitonclick', 'exp', 'expm1', 'fabs', 'factorial', 'fd', 'fill', 'fillcolor', 'floor', 'fmod', 'forward', 'frexp', 'fsum', 'gamma', 'get_poly', 'getcanvas', 'getmethparlist', 'getpen', 'getscreen', 'getshapes', 'getturtle', 'goto', 'heading', 'hideturtle', 'home', 'ht', 'hypot', 'isdown', 'isfile', 'isinf', 'isnan', 'isvisible', 'join', 'ldexp', 'left', 'lgamma', 'listen', 'log', 'log10', 'log1p', 'lt', 'mainloop', 'math', 'mode', 'modf', 'onclick', 'ondrag', 'onkey', 'onrelease', 'onscreenclick', 'ontimer', 'os', 'pd', 'pen', 'pencolor', 'pendown', 'pensize', 'penup', 'pi', 'pos', 'position', 'pow', 'pu', 'radians', 'read_docstrings', 'readconfig', 'register_shape', 'reset', 'resetscreen', 'resizemode', 'right', 'rt', 'screensize', 'seth', 'setheading', 'setpos', 'setposition', 'settiltangle', 'setundobuffer', 'setup', 'setworldcoordinates', 'setx', 'sety', 'shape', 'shapesize', 'showturtle', 'sin', 'sinh', 'speed', 'split', 'sqrt', 'st', 'stamp', 'tan', 'tanh', 'tilt', 'tiltangle', 'time', 'title', 'towards', 'tracer', 'trunc', 'turtles', 'turtlesize', 'types', 'undo', 'undobufferentries', 'up', 'update', 'width', 'window_height', 'window_width', 'write', 'write_docstringdict', 'xcor', 'ycor']>>>