国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Python學習(四)數據結構——intfloat

2019-11-14 17:20:06
字體:
來源:轉載
供稿:網友

Python 數字類型 int float

 

  數字常量

    int:  一般的整數,

    long:   長整型,2.x版本需在數字后加 “L” 或 “l” ,表示長整型 如 100000000L; python3.x 版本后不分長整型,統一為int,不可加 “L” 或 “l”

    float: 浮點數,1.0 也為浮點數,float 可強制轉換為 int,取整;

 1 PRint(type(1234)) 2 print(type(-24)) 3 print(type(0)) 4 print(type(2147483647))      # 為int 5 print(type(2147483648))      # >=2^31 為long    Python2.x ; Python3.x long 和 int 合并為 int 6 i = 1l                       # Python2.x 表示long,Python3 會報錯 7 print(i,type(i)) 8 print(type(i)) 9 print(type(1e+1))            # e表示法為浮點型10 print(int(1e+20))            # 強制轉換為int11 print(int(1e+30))            # 超長精度丟失12 print(type(1.0))             # 小數表示為float13 print(int(1.999))            # int()強制轉換為int 會把float取整

 

  數字計算

    加減乘數運算

 1 a=1 2 b=2 3 c=2.0 4 print(a+b) 5 print(type(a+b))         6 print(a-c)          # 輸出 -1.0  7 print(type(a+c))    # 有浮點型加入,即自動轉換為 float 8 print(a*b) 9 print(type(a*b))    # 兩個整數相乘,仍為整數型10 print(type(a*c))    # 有浮點型加入,即自動轉換為 float11 print(a/b)12 print(type(a/b))        13 print(b/a)14 print(type(b/a))    # 除法運算,即使整除,結果仍為 float 類型

備注: print(1/0)  除數為0會報錯,而不是返回 NaN;一定要 注意 除數為0時的異常判斷;如需處理 NaN,需  from decimal import * 

Decimal numbers include special values such as NaN which stands for “Not a number”, positive and negative Infinity, and -0

 

      取余運算、指數冪運算、取絕對值、四舍五入

 1 print(3%2)                # 通常就做整數間的取余運算 2 print(type(3%2))             3 print(type(3.0%2))        # 不建議浮點型取余 4 print(2.1%2)              # 雖然float也可運算取余,但結果帶精度,此結果為0.10000000000000009 5 print(3**2)               # ** 表示做次方運算,即冪運算 6 print(type(3**2))         # 整數的整數次冪仍為整數 7 print(2**-2)             8 print(type(2**-2))         9 print(1**-2)10 print(type(1**-2))        # 負數次冪均為 float11 print(4**0.5)        12 print(type(4**0.5))       # 非整數次冪均為 float13 print(pow(2,3))           # 指數冪的另一表示法14 print(abs(-1))            # 取絕對值15 print(round(3.5))         # 四舍五入取整16 print(round(3.49))        # 四舍五入取整17 print(round(3.49,1))      # 可加一參數,表示取小數點后幾位四舍五入,如上結果為 3.518 print(round(3.04,1))

 

  數字比較

    比較運算符  ==    !=   >   >=   <   <=    

1 a = 12 b = 1.03 print(a==b)            # 數值的比較 返回 True

 

  進制數

    2進制數以 0b 開頭表示,8進制數以 0o 開頭表示(零和小寫o),16進制數以 0x 開頭表示;bin() 會以二進制輸出形式

1 a = 0b11100                # 2進制數  0b 開頭2 b = 0o34                   # 8進制數  0o 開頭3 c = 0x1c                   # 16進制數 0x開頭4 print(a,b,c)5 print(bin(2))              # bin()表示以二進制輸出

 

  位運算

    同很多語言一樣,Python的位運算符包括 <<    >>     &     |     ~     ^

1 print(bin(0b110<<2))          # 左移2位2 print(bin(0b110>>1))          # 右移1位3 print(0&0,0&1,1&0,1&1)        # & 與運算4 print(0|0,0|1,1|0,1|1)        # | 或運算5 print(0^0,0^1,1^0,1^1)        # ^ 異或運算6 print(bin(~0b11))             # ~ 非運算,有符號數的取反

 

  Math 模塊

    復雜的數學計算需導入數學模塊,即  import math ; 僅列出 math 模塊中一些常用的常量、函數等;具體要用時參閱官方幫助文檔。

 1 import math 2 print(math.e)                       # 數學常量e 3 print(math.pi)                      # 數學常量pi 4 print(math.ceil(3.00001))           # 向上取整 5 print(math.floor(3.99999))          # 向下取整 6 print(math.sqrt(9))                 # 平方根 math.sqrt(x) == x**0.5  同樣返回 float 類型 7 print(math.exp(1))                  # exp(n)  math.e的n次方 8 print(math.log(math.e))             # 即Ln運算 即以自然常數e (2.71828......)為底數的對數 9 print(math.log(16,2))               # 以2為底,16的對數10 print(math.log(1,10))               # 以10為底,1的對數11 print(math.degrees(math.pi))        # Converts angle x from radians to degrees.12 print(math.radians(60))             # Converts angle x from degrees to radians.13 print(math.sin(math.radians(30)))   # 精度丟失14 print(math.cos(math.pi/3))

 

  Random 模塊

    random 模塊可產生多種隨機數;這里僅介紹 randint:產生范圍內的隨機整數;若需其他隨機數方法,具體要用時參閱官方幫助文檔。

random.randint(a, b) 
Return a random integer N such that a <= N <= b.

1 from random import randint2 for i in range(1,11):            # 表示做十次循環3     print(randint(1,10))         # 輸出1到10內的任意數字 

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武汉市| 永川市| 思茅市| 同仁县| 乐陵市| 淮滨县| 阿图什市| 乳源| 洱源县| 根河市| 恩施市| 麦盖提县| 阿拉善盟| 广宁县| 井冈山市| 溧水县| 北宁市| 九龙城区| 玉溪市| 邻水| 吴桥县| 大悟县| 大理市| 遵化市| 淮安市| 玉树县| 平果县| 伊金霍洛旗| 江达县| 安顺市| 南漳县| 格尔木市| 高青县| 苏尼特右旗| 榆树市| 洛浦县| 太仆寺旗| 盐山县| 克东县| 万山特区| 电白县|