python数学计算函数_Python 内置函数(数学运算类,逻辑判断类)
abs()
求絕對(duì)值
1、參數(shù)可以是整型,也可以是復(fù)數(shù)
2、若參數(shù)是復(fù)數(shù),則返回復(fù)數(shù)的模
>>> abs(-10)
10
>>> abs(10)
10
>>> abs(0)
0
>>> a = -10
>>> a.__abs__()
10
complex()
complex() 函數(shù)用于創(chuàng)建一個(gè)值為 real + imag * j 的復(fù)數(shù)或者轉(zhuǎn)化一個(gè)字符串或數(shù)為復(fù)數(shù)。如果第一個(gè)參數(shù)為字符串,則不需要指定第二個(gè)參數(shù)。
>>> complex(1,2)
(1+2j)
>>> complex(1) #數(shù)字
(1+0j)
>>> complex('1+2j') #字符串
(1+2j)
>>> complex('1 + 2j')
Traceback (most recent call last):
File "", line 1, in
complex('1 + 2j')
ValueError: complex() arg is a malformed string
注意!這個(gè)地方在"+"號(hào)兩邊不能有空格,也就是不能寫成"1 + 2j",應(yīng)該是"1+2j",否則會(huì)報(bào)錯(cuò)
divmod(a,b)
python divmod() 函數(shù)把除數(shù)和余數(shù)運(yùn)算結(jié)果結(jié)合起來,返回一個(gè)包含商和余數(shù)的元組(a // b, a % b)。
>>> divmod(7,2)
(3, 1)
>>> divmod(8,2)
(4, 0)
>>> divmod(1+2j,1+0.5j)
Traceback (most recent call last):
File "", line 1, in
divmod(1+2j,1+0.5j)
TypeError: can't take floor or mod of complex number.
注意!不能使用復(fù)數(shù)形式
float()
float() 函數(shù)用于將整數(shù)和字符串轉(zhuǎn)換成浮點(diǎn)數(shù)。
>>>float(1)
1.0
>>> float(112)
112.0
>>> float(-123.6)
-123.6
>>> float('123') # 字符串
123.0
int()
int() 函數(shù)用于將一個(gè)字符串或數(shù)字轉(zhuǎn)換為整型。
>>>int() # 不傳入?yún)?shù)時(shí),得到結(jié)果0
0
>>> int(3)
3
>>> int(3.6)
3
>>> int('12',16) # 如果是帶參數(shù)base的話,12要以字符串的形式進(jìn)行輸入,12 為 16進(jìn)制
18
>>> int('0xa',16)
10
>>> int('10',8)
8
pow()
pow() 方法返回x的y次方的值。
>>> pow(100,2)
10000
>>> pow(2,3)
8
range()
Python3 range() 函數(shù)返回的是一個(gè)可迭代對(duì)象(類型是對(duì)象),而不是列表類型, 所以打印的時(shí)候不會(huì)打印列表。
Python3 list() 函數(shù)是對(duì)象迭代器,可以把range()返回的可迭代對(duì)象轉(zhuǎn)為一個(gè)列表,返回的變量類型為列表。
>>> range(5)
range(0, 5)
>>> for i in range(5):
print(i)
0
1
2
3
4
>>> list(range(5))
[0, 1, 2, 3, 4]
round()
返回浮點(diǎn)數(shù)x的四舍五入值。
>>> round(70.2322313)
70
>>> round(1170.233234,3)
1170.233
>>> round(1170.233234,1)
1170.2
sum()
對(duì)系列進(jìn)行求和計(jì)算。
>>> sum([1,2,3])
6
>>> sum([1,2,3],22)
28
oct()
將一個(gè)整數(shù)轉(zhuǎn)換成8進(jìn)制字符串。
>>> oct(10)
'0o12'
>>> oct(12)
'0o14'
hex()
將一個(gè)整數(shù)轉(zhuǎn)換成16進(jìn)制字符串。
>>> hex(16)
'0x10'
>>> hex(20)
'0x14'
chr()
返回整數(shù)i對(duì)應(yīng)的ASCII字符
>>> chr(97)
'a'
>>> chr(0x30)
'0'
bin()
將一個(gè)整數(shù)轉(zhuǎn)換成2進(jìn)制字符串。
>>> bin(2)
'0b10'
>>> bin(8)
'0b1000'
bool()
bool是Boolean的縮寫,只有真(True)和假(False)兩種取值
bool函數(shù)只有一個(gè)參數(shù),并根據(jù)這個(gè)參數(shù)的值返回真或者假。
>>> bool()
False
>>> bool(0)
False
>>> bool(10)
True
#用bool來判斷一個(gè)值是否已經(jīng)被設(shè)置
>>> x = input('please enter a number:')
please enter a number:
>>> bool(x.strip())
False
>>> x = input('please enter a number:')
please enter a number:3
>>> bool(x.strip())
True
總結(jié)
以上是生活随笔為你收集整理的python数学计算函数_Python 内置函数(数学运算类,逻辑判断类)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 钱的本质上是什么?
- 下一篇: windows无法访问局域网共享电脑怎么