Python int

Python 教學 6 – Python Numeric Types – int, float

Python 中的 numeric type,除了常用的 int 和 float 外,還有 complex (複數),不過因為複數的應用場景比較少一點,所以就先暫時忽略。

1. 宣告 Python int, float object

1.1 int

可以直接用數字宣告變數,也可以用 int class 來 new 出 int object:

my_int = 10
print(my_int)
# 10

my_int = int(10)
print(my_int)
# 10
class int(x=0)
class int(xbase=10)

int class 有幾個特性

  • 第二個 argument – base 用來表示這個 int 是幾進位,default value 是 10
  • 沒有第二個 argument 時,第一個 argument 的 default value 是 0
  • 有第二個 argument 時,第一個 argument 一定要是用 string, bytes 或 bytearray object 來表示
my_int = int()
print(my_int)
# 0

my_int = int('10')
print(my_int)
# 10

my_int = int('10', 2)
print(my_int)
# 2

my_int = int('0b10', 2)
print(my_int)
# 2

my_int = int('010', 8)
print(my_int)
# 8

my_int = int('0x10', 16)
print(my_int)
# 16

my_int = int(10, 16)
print(my_int)
TypeError: int() can't convert non-string with explicit base

特殊表示法:當數字較大時,可以用底線將數字分開,但不能用 ‘,’ ,否則會變成 tuple:

my_int = 100_000_000
print(my_int)
# 100000000
print(my_int.__class__)
# <class 'int'>

my_int = 100,000,000
print(my_int)
# (100, 0, 0)
print(my_int.__class__)
# <class 'tuple'>

1.2 float

float 一樣可以直接賦值,或是用 float class 來 new 出一個 float object:

class float(x=0.0)
my_float = 25.3
print(my_float)
# 25.3

my_float = float('25.3')
print(my_float)
# 25.3

my_float = float()
print(my_float)
# 0.0

特殊表示法:除了用剛剛的底線也可以表示 float 之外,科學記號表示法預設也是 float:

my_float = 100_000.2
print(my_float)
# 100000.2

my_float = 1e6
print(my_float)
# 1000000.0

my_float = 1e-3
print(my_float)
# 0.001

2. Operators

名詞解釋:operator 左右兩側的 object 稱為 operands (運算數)

2.1 + (Addition Operator)

將 operands 相加,只要有其中一個 operand 是 float 的話,結果就會是 float:

1 + 2
# 3

1 + 2.0
# 3.0

1.0 + 2.0
# 3.0

2.2 – (Subtraction Operator)

將 operands 相減,只要有其中一個 operand 是 float 的話,結果就會是 float:

3 - 1
# 2

3 - 1.0
# 2.0

3.0 - 1.0
# 2.0

2.3 * (Multiplication Operator)

將 operands 相乘,只要有其中一個 operand 是 float 的話,結果就會是 float:

2 * 3
# 6

2 * 3.0
# 6.0

2.0 * 3.0
# 6.0

2.4 / (Division Operator)

將 operands 相除,division operator 比較特別,無論 operands 是 float 或 int,運算後的結果都是 float,就算整除也是一樣:

9 / 3
# 3.0

9 / 3.0
# 3.0

2 / 3
# 0.6666666666666666

如果希望相除後的結果是 int 的話,可以用 int class 包起來,結果會是無條件捨去:

int(9 / 3)
# 3

int(10 / 4)
# 2

int(10 / -4)
# -2

2.5 % (Modulus Operator)

取餘數,只要有其中一個 operand 是 float 的話,結果就會是 float:

5 % 3
# 2

5 % 3.0
# 2.0

5.0 % 3
# 2.0

5.0 % 3.0
# 2.0

2.6 ** (Exponentiation Operator)

取次方,只要有其中一個 operand 是 float 的話,結果就會是 float:

# 2 的 3 次方
2 ** 3
# 8

2 ** 3.0
# 8.0

# 3 的 4 次方
3 ** 4
# 81

2.7 // (Floor Division Operator)

也叫做 integer division operator,會計算小於相除結果的最大整數,只要有其中一個 operand 是 float 的話,結果就會是 float:

13 // 4
# 3
# 13 / 4 = 3.25
# 小於 3.25 的最大整數是 3

13 // 4.0
# 3.0

13 // -4
# -4
# 13 / -4 = -3.25
# 小於 -3.25 的最大整數是 -4

和 math.floor 的結果一樣:

import math
math.floor(13 / 4)
# 3

math.floor(13 / -4)
# -4

3. Relative Methods

介紹一些常用在 int 和 float 上的相關 functions

3.1 math.floor

回傳 <= 該數的最大整數,使用前要先 import math module

import math

math.floor(2.34)
# 2

math.floor(2.74)
# 2

math.floor(-2.74)
# -3

3.2 math.ceil

回傳 >= 該數的最小整數,使用前要先 import math module

import math

math.ceil(2.34)
# 3

math.ceil(2.74)
# 3

math.ceil(-2.34)
# -2

3.3 round

round 通常用來將 float 做四捨五入,但在 Python 中可以發現當要取位的數字為 5 時,會有一些不預期的結果:

round(1.5)
# 2

round(2.5)
# 2

2.5 四捨五入後照理說應該是 3,卻出現了 2,可以看到 Python document 中的解釋:

values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice

也就是說,當要作取位的數字,和左右兩邊距離一樣時(ex: 2.5 和 2 還有 3 的距離一樣),則會保留偶數的一邊,所以 round(2.5) 才會是 2,可以舉多一點例子看:

round(2.65, 1)
# 2.6

round(2.6754, 2)
# 2.68

3.4 math.trunc

無條件捨去,使用前要先 import math module

import math

math.trunc(2.34)
# 2

math.trunc(2.74)
# 2

math.trunc(-2.34)
# -2

3.5 abs

取絕對值,argument 是 int 就回傳 int,是 float 就回傳 float:

abs(5)
# 5

abs(-5)
# 5

abs(-5.32)
# 5.32

abs(5.0)
# 5

4. f-string 中 的 number style

4.1 取小數位

這個方式會用 round() 的 function 來做四捨五入:

my_float = 7.125
print(f'The value of my_float is {my_float:.2f}')
# The value of my_float is 7.12

4.2 加上 ‘,’

my_fortune = 3000000
print(f'The value of my_fortune is {my_fortune:,}')
# The value of my_fortune is 3,000,000

my_fortune = 10020.56
print(f'The value of my_fortune is {my_fortune:,.2f}')
# The value of my_fortune is 10,020.56

4.3 百分比

ratio = 0.9
print(f'{ratio:.1%} use iphone in Taiwan')
# 90.0% use iphone in Taiwan

ratio = 0.875
print(f'{ratio:.2%} use iphone in Taiwan')
# 87.50% use iphone in Taiwan

參考資料

Built-in Types — Python 3.11.4 documentation
Numbers in Python
Python Operators – W3Schools
Python Double Slash (//) Operator: Floor Division – LearnDataSci
Python 中關於round 函數的小坑
Built-in Functions — Python 3.11.4 documentation
math — Mathematical functions — Python 3.11.4 documentation

如果覺得我的文章有幫助的話,歡迎幫我的粉專按讚哦~謝謝你!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top