###### tags: `chapter 3` `Python` # 3-2 數值資料型ㄋ態 # 數值資料分為兩種,分別是整數(int)以及浮點數(float),差別在於小數點的有無,這點在介紹強制轉型時以有介紹 相信前面的範例讓大家對數值已有不小的認識,這裡介紹幾個和數值有關的操作。 ## 進位制轉換 ## 如果想讓數以2進位的方式呈現,你可以使用`bin()`函數來做轉換或者在宣告變數時在值前面加上`0b`。 ```python= x = 0b1101 print(x) y = 7 print(bin(y)) ``` 想要使用16進位表示的話則是使用`hex()`函數以及`0x`來操作。 | 數值| 10 | 11 | 12 | 13 | 14 | 15 | | - | - | - | - | - | - | - | | 符號 | a | b | c | d | e | f | ```python= x = 0x5d3a print(x) y = 87 print(hex(y)) ``` ## 數值運算常用函數 ## * abs():計算絕對值。 ```python= a, b = -5, 5 print(abs(a)) print(abs(b)) ``` * pow(x , y):計算 x 的 y 次方。 ```python= x, y = 2, 5 print(pow(x, y)) ``` * round():返回五捨六入。 ```python= a, b = 3.5, 6.6 print(round(a)) print(round(b)) ```  round()函數是`五捨六入`不是`四捨五入`哦! 
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up