基本資料型別——數字型別
一、int型別
1、定義
int是一種資料型別,在編程語言(c、c++、c#、java等)中,是用于定義整數型別變數的識別符號,
在一般的電腦中,int占用4位元組(Byte),32個二進制位(bit,二進制數字中的一位)
除了int型別之外,還有short、long、long long型別可以表示整數,即short int 等 , unsigned int 表示無符號整數
2、作用
>>> >>> age=input('please input your age:') please input your age:18 >>> print(int(age)) #強制型別轉換,將input輸入所得的字串資料轉換為int型別 18 >>> age=int(10.5) >>> print('age') age >>> print(age) 10 >>> #名字(引數)—— >>> int() >>> print() >>> input() '' >>> x=int(2) >>> name=input('your name:') your name:cc >>> res=print('ccc') ccc >>>
3、型別轉換——

>>> >>> #純數字的字串轉換成int >>> res=int('100222') >>> print(res,type(res)) 100222 <class 'int'> >>> >>> #了解知識 >>> #數字有不同進制,不同進制資料之間如何轉換 >>> #二進制—>十進制 >>> #1011---->8+2+1=11
>>> #十進制—>二進制
>>> # 11----->1011
>>> print(bin(11)) #0b1011 將11轉換成二進制 0b1011 >>> #十進制—>八進制 >>> print(oct(11)) 0o13 >>> #十進制—>十六進制 >>> print(hex(11)) 0xb >>> print(hex(123)) 0x7b >>> #其他進制轉換成十進制 >>> print(int('0b1011',2)) 11 >>> print(int('0o13',8)) 11 >>> print(int('0xb',16)) 11 >>>
二、float 型別
1、定義
float 型別,用于存盤單精度浮點數或雙精度浮點數,
float 單精度浮點數在機內占 4 個位元組,用 32 位二進制描述,
double 雙精度浮點數在機內占 8 個位元組,用 64 位二進制描述,
參考資料:
https://www.runoob.com/w3cnote/float-and-double-different.html
2、作用
eg :
salary=3.1 # salary=float(3.1)
3、型別轉換
>>> res=float('3.45') >>> print(res,type(res)) 3.45 <class 'float'> >>> s=5 >>> res=float(s) >>> print(res,type(res)) 5.0 <class 'float'> >>>
4、使用
# int與float沒有需要掌握的內置方法
# 他們的使用,就是數學運算+比較運算
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/181826.html
標籤:Python
上一篇:1.numpy入門介紹
