Python_MathOperator

本文介绍了Python中数学运算符的使用以及math和cmath模块提供的高级数学函数,包括基本运算、math包中的函数示例、复数运算和极坐标转换等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python - 数学运算符/函数

这些是常用的数学函数(你可以将其作为快速参考表)。还有许多数学函数。math包中的一些函数几乎与原生操作符/函数具有相同的功能,但更具有准确性。你可以参阅Python官方文档的第9.2节:math — Mathematical functions以获取更多详细信息。

基本操作符 - 示例1

math包中的函数/常量 - 示例2

复数运算 - 示例3,示例4

Examples 1 > Basic Operator

>>> 2 + 5

7

 

>>> 3 - 5

-2

 

>>> 2 * 3

6

 

>>> 2 / 3

0.6666666666666666

 

>>> pow(2,3)

8

 

>>> pow(2.0,3.0)

8.0

 

>>> 2 ** 3

8

 

>>> 7 % 3

1

 

 

 

Examples 2>  math package

>>> import math

 

>>> math.exp(1)

2.718281828459045

 

>>> math.sqrt(2)

1.4142135623730951

 

 

Examples 3>  complex number 

 

>>> a = 1+2j

 

>>> b = complex(2,3)

 

>>> a

(1+2j)

 

>>> b

(2+3j)

 

>>> a+b

(3+5j)

 

>>> a*b

(-4+7j)

 

>>> a/b

(0.6153846153846154+0.07692307692307691j)

 

>>> a.real

1.0

 

>>> a.imag

2.0

 

>>> a.conjugate()

(1-2j)

 

>>> import cmath

 

>>> cmath.phase(a)

1.1071487177940904

 

>>> cmath.polar(a)

(2.23606797749979, 1.1071487177940904)

 

>>> cmath.rect(2.23606797749979,1.1071487177940904)

(1.0000000000000002+2j)

 

 

 

Example 4 >  Complex Number (Same as Example 3, but run in  *.py script)

 

import cmath

 

a = 1+2j

b = complex(2,3)

 

print('a = ',a)

print('b = ',b)

print('a+b = ',a+b)

print('a*b = ',a*b)

print('a/b = ',a/b)

print('a.real = ',a.real)

print('a.imag = ',a.imag)

print('a.conjugate() = ',a.conjugate())

print('cmath.phase(a) = ',cmath.phase(a))

print('cmath.polar(a) = ',cmath.polar(a))

print('cmath.rect(2.23606797749979,1.1071487177940904) = ',

                                 cmath.rect(2.23606797749979,1.1071487177940904))

 

 

Result :---------------------------------

 

a =  (1+2j)

b =  (2+3j)

a+b =  (3+5j)

a*b =  (-4+7j)

a/b =  (0.6153846153846154+0.07692307692307691j)

a.real =  1.0

a.imag =  2.0

a.conjugate() =  (1-2j)

cmath.phase(a) =  1.1071487177940904

cmath.polar(a) =  (2.23606797749979, 1.1071487177940904)

cmath.rect(2.23606797749979,1.1071487177940904) =  (1.0000000000000002+2j)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值