1. 程式人生 > 其它 >一 資料型別內建方法

一 資料型別內建方法

一、數字型別

# ======================================基本使用======================================
# 整型
# 1、用途
#年齡 生日 整數型別
# 2、定義方式
# age = 18 # age = int(18)
# 3、型別轉換
# int()將括號內的資料轉成整型
# print(int(123))
# print(int('123'))
# print(int('[1,2,3,4]')) # 錯
# print(int('hello world')) # 錯
# print(int('11.11')) # 待轉換的資料內部只能是純數字
# 4 進位制轉換
# print(bin(100)) # 將十進位制的100轉換成二進位制對應的數 0b1100100
# print(oct(100)) # 將十進位制的100轉換成八進位制對應的數 0o144
# print(hex(100)) # 將十進位制的100轉換成十六進位制對應的數0x64
'''
0b開頭表示二進位制數
0o開頭表示八進位制數
0x開頭表示十六進位制
'''
# print(int('0b1100100',2))
# print(int('0o144',8))
# print(int('0x64',16))

# 浮點型
# 身高 體重 薪資
# height = 183.1 # height = float(183.1)
# print(float(183.1))
# print(float('183.1'))
# print(float('hello world')) # 錯
# print(float(183)) # 183.0
# print(int(11.11)) # 11
# print(int('11.11')) # no

'''
可變型別
值改變 記憶體地址不變
不可變型別
值改變 記憶體地址一定變
'''
# 不可變
# 整型 浮點型 字串
# 可變
# 列表