python3的基本類型

1.整數:int 浮點數:float 其他語言:單精度(float),雙精度(double),在python中並無單精度與雙精度之分;而整數中也只有int類型;

>>> type(1)
<class 'int'>
>>> type(1.1)
<class 'float'>
>>> type(-1)
<class 'int'>
>>> type(1.111)
<class 'float'>
>>> 1+0.1
1.1
>>> type(1+0.1)
<class 'float'>
>>> type(1+1)
<class 'int'>
>>> type(1+1.0)
<class 'float'>
>>> 
>>> type(1*1)
<class 'int'>
>>> type(1*1.0)
<class 'float'>
>>> type(2/2)
<class 'float'>
>>> type(2//2)
<class 'int'>

除法運算,使用“/”,特別說明的是,兩個整數相除,所得結果爲浮點數,若想得到整數結果,必須使用“//”;
2.進制運算:10進制,2進制,8進制,16進制等;
如何表示:
二進制:0b10; 八進制:0o10;十六進制:0x10;

>>> 0b10
2
>>> 0b11
3
>>> 0o10
8
>>> 0o11
9
>>> 0x10
16
>>> 0x1f
31

type():用於查看某個數的類型
bin():用於進制轉換,將其他進制轉換成二進制

>>> bin(10)
'0b1010'
>>> bin(0o7)
'0b111'
>>> bin(0xE)
'0b1110'

int():用於進制轉換,將其他進制轉換成十進制

>>> int(0b111)
7
>>> int(0o77)
63
>>> 

hex():用於進制轉換,將其他進制轉換成十六進制

>>> hex(888)
'0x378'
>>> hex(0o7777)
'0xfff'
>>> 

oct():用於進制轉換,將其他進制轉換成八進制

>>> oct(0b111)
'0o7'
>>> oct(0x777)
'0o3567'

3.bool類型:表示真,假 complex:複數,如36j
在bool類型中,真爲True,假爲False

>>> True
True
>>> False
False
>>> true
Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    true
NameError: name 'true' is not defined
>>> type(True)
<class 'bool'>
>>> int(True)
1
>>> int(False)
0
>>> bool(1)
True
>>> bool(0)
False

非零實數表示True,而實數零表示False

>>> bool(2)
True
>>> bool(2.2)
True
>>> bool(0)
False
>>> 

字符串,列表,元組等也可以用於bool類型

>>> bool('abc')
True
>>> bool('')
False
>>> bool([1,2,3])
True
>>> bool([])
False
>>> bool({1,1,1})
True
>>> bool({})
False
>>> 

特殊的,bool(None)=False
4.str字符串
表示方法:單引號,雙引號,三引號

>>> 1
1
>>> '1'
'1'
>>> type(1)
<class 'int'>
>>> type('1')
<class 'str'>
>>> 
>>> 'let's go'
SyntaxError: invalid syntax
>>> "let's go"
"let's go"
>>> 'let\'s go'
"let's go"
>>> 

‘\n’:表示換行

>>> '''
hello world
hello world
hello world
'''
'\nhello world\nhello world\nhello world\n'
>>> """
hello world
hello world
hello world
"""
'\nhello world\nhello world\nhello world\n'
>>> 
>>> """hello world\nhello world\nhello world"""
'hello world\nhello world\nhello world'
>>> print("""hello world\nhello world\nhello world""")
hello world
hello world
hello world
>>> 

要注意區分三者的區別

5.轉義字符:
轉義字符是一種特殊的字符
是無法“看見”的字符
與語言本身語法有衝突的字符
如:\n:換行 , \t:橫向製表符 ,\r:回車

>>> print('hello \\n world')
	  
hello \n world
>>> print(r'c:\\nnorthw')
	  
c:\\nnorthw
>>> 

r/R:表示原始字符串
+:表示字符串的拼接

>>> "hello"+"world"
'helloworld'

*:表示倍數

>> "hello"*3
'hellohellohello'

下標操作符:
從字符串正面開始

>>> "hello world"[3]
'l'
>>> "hello world"[8]
'r'
>>> 

從字符串後面開始

>>> "hello world"[-1]
'd'
>>> "hello world"[-3]
'r'

獲取一組字符:[a:b],其中b表示所取元素的下一個字符的下標,當b爲負數時,表示從字符串末尾截取到以後的元素

'hello'
>>> "hello world"[0:-1]
'hello worl'
>>> "hello world"[0:-3]
'hello wo'
>>> 

當所取的長度大於字符串的最大長度時,表示取到字符串結尾

>>> "hello world"[6:10]
'worl'
>>> "hello world"[6:11]
'world'
>>> "hello world"[6:20]
'world'
>>> 

當b不取值時,表示取道字符串的結尾

>>> "hello world"[6:]
'world'
>>> 

當b取0時,取空

>>> "hello world"[6:0]
''
>>> "hello world"[6:-0]
''
>>> 

數字a爲負數時,b爲缺省時,從字符串末尾開始,倒着取-a個元素

>>> "hello world"[-4:]
'orld'
>>> 

當a取0時,可以省略

>>> "hello world"[0:4]
'hell'
>>> "hello world"[:4]
'hell'
>>> 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章