python數字常量

來源:互聯網
上載者:User

標籤:index   border   小數點   包括   bsp   html   base   cin   pac   

數字常量

本節內容如下:

  • 整數
  • 浮點數
  • 複數
  • 分數
整數

整數包括:正數和負數和零。查看原文

顯示方式:十進位(預設)、二進位(0b)、八進位(0o)、十六進位(0x) 轉換函式:bin(I) / oct(I) / hex(I) / int(str,base)

a = 100print(bin(a))   # 0b1100100print(oct(a))   # 0o144print(hex(a))   # 0x64
浮點數

帶小數點的數字,可以使用科學計數法3.14e-10,例如 :

x = 314print(‘%e‘ %x)  # 3.140000e+02
複數

複數包括實部和虛部,例如:

c = 3+4jprint(c.real) # 3.0print(c.imag) # 4.0 c = complex(3,4)print(c)    # (3+4j)
分數

使用分數需要使用Python中的Fraction模組,需要匯入包:

from fractions import Fraction f1 = Fraction(3,4)print(f1)   # 3/4f2 = Fraction(5,16) print(f1+f2)    # 17/16print(f1*f2)    # 15/64

python數字常量

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.