python基礎-15數字類型內置方法

Python3 中有六個標準的數據類型:
Number(數字)
String(字符串)
List(列表)
Tuple(元組)
Set(集合)
Dictionary(字典)
Python3 的六個標準數據類型中:
不可變數據(3 個):Number(數字)、String(字符串)、Tuple(元組);
可變數據(3 個):List(列表)、Dictionary(字典)、Set(集合)python

整型和浮點型統稱爲數字類型。code

整型內置方法(int)

  • 1.用途:年齡、號碼、等級
  • 2.定義:可使用int()方法將純數字的字符串轉爲十進制的整型
age = 19  # age = int(10)
print(type(age))

<class 'int'>字符串

x = int('111')
print(type(x))

<class 'int'>io

x = int('11.1')  # 報錯
print(x)
  • 3.經常使用操做+內置方法:算術運算+比較運算
    ** 1.0.1 長整型 **
    長整型只在python2中存在,python3中不存在長整型。

x = 11111111111111111111111111111111111111111111111
print(type(x)) # longintclass

  • 4.存一個值or多個值:一個值基礎

  • 5.有序or無序:無有序or無序一說List

age = 19
print(f'first:{id(age)}')
age = 20
print(f'second:{id(age)}')

first:4384901776
second:4384901808數據類型

  • 6.可變or不可變:不可變數據類型。


浮點型內置方法(float)

  • 1.用途:薪資、身高、體重
  • 2.定義:可使用float()方法將純數字的字符串轉爲浮點型數字。
age = 3.1  # age = float(3.1)
print(type(age))

<class 'float'>float

x = float('111')
print(x)
print(type(x))

111.0
<class 'float'>方法

x = float('11.1')  # 報錯
print(type(x))

<class 'float'>

  • 3.經常使用操做+內置方法:算術運算+比較運算

  • 4.存一個值or多個值:一個值

  • 5.有序or無序:無有序or無序一說
salary = 3.1
print(f'first:{id(salary)}')
salary = 5.1
print(f'second:{id(salary)}')

first:4423173584
second:4423173800

  • 6.可變or不可變:不可變數據類型

可變or不可變

id不變值可變,即在原值的基礎上修改,則爲可變數據類型;值變id也變,即從新申請一個空間放入新值,則爲不可變數據類型。

相關文章
相關標籤/搜索