python數據類型-數字

總結:Python3.6版本

注:【進制數也屬於整數,我們說的整數為10進制整數】

數字分類:

  • 整數:-1 、1、進制數
  • 浮點數:-0.11、0.11
  • 進制數:0b1010,0B1010(二進制0b或者0B) 0o127,0O127(八進制0o或者0O) 0x12ef,0X12ef(十六進制0x或者0X)
  • 複數:1.23+1.0j,34-4j
  • 分數【不常用跳過】

運算操作:

  • 加,減,乘,除,求整,求餘,求冪、布爾運算,複數運算等[+ - * \\ // % ** ]

內置函數:

  • bin:10進制轉化為2進制
  • oct:10進制轉化為8進制
  • hex:10進制轉化為16進制
  • int:字符串或浮點數轉化為10進制整數&&其它進制轉10進制
  • float:字符串或者整數轉化為浮點數
  • complex:complex(a,b)創造複數a為實部b為虛部
  • pow:冪運算
  • abs:絕對值運算
  • round:四捨五入運算
  • ord():求ASCII碼數值
  • chr():ASCII碼值逆運算

常用模塊:

  • math
  • round

bool值:

邏輯運算返回布爾值

  • True:非0數字和非空字符串
  • False:0、None、空字符串
<code># 浮點數 

a=12.8

# 二進制數據--int型
b=0b1010

# 複數
c=12.3+5.0j

# 複數
d=-10

# 10進制數據轉換為2進制數據
print(bin(10)) # 0b1010

# 10進制數據轉換為8進制數據
print(oct(10)) # 0o12

# 10進制數據轉換為16進制數據
print(hex(10)) # 0xa

# 2進制數據轉化為10進制
print(int(0b1010)) # 10

# 8進制數據轉化為10進制
print(int(0o12)) # 10

# 16進制數據轉化為10進制
print(int(0xa)) # 10
print(bin(int(0xa))) # 0b1010

# 浮點數轉化為10進制
print(int(10.8)) # 10

# base=0把字符串'0o12'當作數字0o12,相當於int(0o12)
print(int('0o12', base=0)) # 10

# 字符串轉化為10進制數據 參數進制數必須保持一致
print(int('0o12', base=8)) # 10

# 字符串轉浮點數
print(float('123')) # 123.0

# 整型轉浮點型
print(float(123)) # 123.0

# 創造複數1為實部2為虛部
print(complex(1, 2)) # (1+2j)

# 冪運算
print(pow(2, 3)) # 8

# 絕對值運算
print(abs(-10.3)) # 10.3

# 四捨五入運算
print(round(10.3)) # 10

# 字符ASCII碼運算
print(ord('a')) # 97

# ASCII碼逆運算
print(chr(97)) # a

# 算術運算
A = 3
B = 4
print(A+B, A-B, A*B, A/B, A//B, A % B, A**B) # 7 -1 12 0.75 0 3 81

# 複數運算
print(1.2e+2, 1.2e+2j, 1.2e+2+3j) # 120.0 120j (120+3j)

# 邏輯運算返回布爾值、布爾運算
print(A > B and not A >= B, A>B or A
# 0、None、空字符串返回False
print(bool(0), bool(None), bool("")) # False False False

# 非0數值和非空字符串返回True
print(bool(1), bool(-1), bool("bool")) # True True True
/<code>

原型類或者方法:

<code>def bin(*args, **kwargs):
Return the binary representation of an integer.
#返回整數的二進制表示形式
#>>> bin(2796202)0b1010101010101010101010
pass

def oct(*args, **kwargs):
Return the octal representation of an integer.
#返回整數的八進制表示形式
#>>> oct(342391) '0o1234567'
pass

def hex(*args, **kwargs):
Return the hexadecimal representation of an integer.
#返回整數的十六進制表示形式
#>>> hex(12648430)0xc0ffee
pass

class int(object):
int(x=0) -> integer
int(x, base=10)->integer

Convert a number or string to an integer, or return 0 if no arguments
are given. If x is a number, return x.__int__(). For floating point
numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string,
bytes, or bytearray instance representing an integer literal in the
given base. The literal can be preceded by '+' or '-' and be surrounded
by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
Base 0 means to interpret the base from the string as an integer literal.
#>>> int('0b100', base=0) 4

def __init__(self, x, base=10):
int(x=0) -> integer
int(x, base=10) -> integer
pass

#1.轉化一個數字或者字符串為整型,如果沒有參數則返回0。

#2.如果為數字,則調用__init__函數,如果為浮點數則截斷小數位為0取整,沒有base參數
#3.如果參數x不是數字或者base參數被傳入,那麼參數x一定要是一個字符串,字節流,或者字節數組
# 並按照給的base去展示數據並轉化為整型,base要和前邊的進制一致
#4.base默認為10,值的範圍【0、2-36】,0意思為解釋字符串為整型數據,按照數字去處理


class float(object):
float(x) -> floating point number
Convert a string or number to a floating point number, if possible.
#轉化字符串或者數字為浮點型數字
def __init__(self, x):
pass

class complex(object):
complex(real[, imag]) -> complex number
Create a complex number from a real part and an optional imaginary part.
This is equivalent to (real + imag*1j) where imag defaults to 0.
def __int__(self, *args, **kwargs): # real signature unknown
''' int(self) '''
pass

def pow(*args, **kwargs):
Equivalent to x**y (with two arguments) or x**y % z (with three arguments)
Some types, such as ints, are able to use a more efficient algorithm when
invoked using the three argument form.
pass
#等價於x**y 或者傳入三個參數

def abs(*args, **kwargs): # real signature unknown
Return the absolute value of the argument.
#返回參數的絕對值
pass

def round(number, ndigits=None):

round(number[, ndigits]) -> number
Round a number to a given precision in decimal digits (default 0 digits).
This returns an int when called with one argument, otherwise the
same type as the number. ndigits may be negative.
return 0

def ord(*args, **kwargs):
Return the Unicode code point for a one-character string.
pass/<code>


分享到:


相關文章: