天天看點

python資料類型分類以及運算類型

一、python資料類型

目錄:

1.數字(整數、小數)

2.字元串(單引号、雙引号、三引号)

3.元組  #元素确定之後不能修改

4.清單 #元素可以修改

5.集合  #不講順序,得到的結果沒有重複元素、可以用于去重

6.字典

二、python運算類型

1.數學運算(+ - * /  ** // % )

2.指派運算

3.邏輯運算(and or not)

4.比較運算(> < != >= <= ==)

5.關系運算(in not in is not is)

6.位運算(&(與)   |(或)  ^(異或)  ~(求反)  >>(左移)  <<(右移) )

一、python資料類型(數字、字元串、元組、清單、集合、字典)

代碼如下:

1 #數字
 2 print("整數",1)
 3 print("小數",1.2)
 4 print("整數的類型:",type(1))
 5 print("小數的類型:",type(1.2))
 6 #字元串
 7 print("單引号字元串:",type('aa'))
 8 print("雙引号字元串:",type("aa"))
 9 print("三引号字元串:",type('''aa'''))
10 #元組
11 print("元組:",(1,2,3))
12 print("元組的類型:",type((1,2,3)))
13 #清單
14 print("清單:",[1,2,3])
15 print("清單的類型:",type([1,2,3]))
16 #集合
17 print("集合:",{1,2,3})
18 print("集合的類型:",type({1,2,3}))
19 #字典
20 print("字典:",{1:"a",2:"b",3:"c"})
21 print("字典的類型:",type({1:"a",2:"b",3:"c"}))      

運作如下:

二、運算類型

1.數學運算(+ - * /  ** // % )

1 # + - * / ** // %
 2 #加
 3 print("1+2 = ",1+2)
 4 #減
 5 print("3-2 = ",3-2)
 6 #乘
 7 print("3*2 = ",3*2)
 8 #除
 9 print("3/2 = ",3/2)  #不管是否能夠整除,結果都為小數
10 print("6/3 =",6/3)
11 #幂
12 print("3**2 = ",3**2)
13 #整除
14 print("3//2 = ",3//2)   #結果為1
15 #取餘
16 print("5%3 = ",5%3)     #結果為3      

運作效果如下:

1 #+= -= *= /= //= %= **=
 2 #+=
 3 a=2
 4 print("a的值:",a)
 5 print("a+=3 ")
 6 a+=3
 7 print(a)
 8 #-=
 9 b=4
10 print("b的值:",b)
11 print("b-=1 ")
12 b-=1
13 print(b)
14 #*=
15 c=2
16 print("c的值:",c)
17 print("c*=2 ")
18 c*=2
19 print(c)
20 #/=
21 d=5
22 print("d的值:",d)
23 print("d/=2 ")
24 d/=2
25 print(d)
26 #//=
27 e=7
28 print("e的值:",e)
29 print("e//=2 ")
30 e//=2
31 print(e)
32 #%=
33 f=7
34 print("f的值:",f)
35 print("f%=2 ")
36 f%=2
37 print(f)
38 #**=
39 g=3
40 print("g的值:",g)
41 print("g**=2 ")
42 g**=2
43 print(g)      
1 # and or not
 2 #and
 3 print("and運算:")
 4 print("true and false:")
 5 print(True and False)
 6 print("true and true:")
 7 print(True and True)
 8 print("false and false:")
 9 print(False and False)
10 #and 先判斷第一個數是否為0,若為0,則結果為0;否則看第二個數,不管第二個數是多少,輸出結果都是第二個數
11 print("1 and 2 的結果是:",1 and 2)
12 print("0 and 2 的結果是:",0 and 2)
13 #or
14 print("or 運算:")
15 print("true or false:")
16 print(True or False)
17 print("true or true:")
18 print(True or True)
19 print("false or false:")
20 print(False or False)
21 #or 先判斷第一個數是否為0,不為0,則輸出第一個數;若為0,則看第二個數,不管第二個數是多少,輸出結果都是第二個數
22 print("1 or 2 的結果是:",1 or 2)
23 print("0 or 3 的結果是:",0 or 3)
24 print("2 or 0 的結果是:",2 or 0)
25 #not 
26 print("not 運算:")
27 print("not false:")
28 print(not False)
29 print("not true:")
30 print(not True)      

4.比較運算(> < >= <= == !=)

1 #>  < ==  >=  <=  !=
 2 #>
 3 print("2>1:")
 4 print(2>1)
 5 #<
 6 print("2<3:")
 7 print(2>3)
 8 #==
 9 a=2
10 b=2
11 print("a的值:",a)
12 print("b的值:",b)
13 print("a==b:")
14 print(a==b)
15 #>=
16 print("a>=b")
17 print(a>=b)
18 #<=
19 print("a<=b")
20 print(a<=b)
21 #!=
22 print("2!=3")
23 print(2!=3)      
python資料類型分類以及運算類型
1 #in not in   is not is
 2 print("in not in的用法:")
 3 a=(1,2,3)
 4 print(a)
 5 print(1 in a)
 6 print(4 not in a)
 7 print("is not is在數字類型的用法:")
 8 a=2
 9 b=2
10 print("a的值為:",a)
11 print("b的值為:",b)
12 print(a is b)
13 print("is not is在清單類型的用法:")
14 c=[1,2,3]
15 d=[1,2,3]
16 print("c為:",c)
17 print("d為:",d)
18 print(c is d )
19 print(c is not d )      
python資料類型分類以及運算類型

注:

技巧:~求反(加負号再減1,也可以用補碼計算)   

a=10    二進制為:00001010

b=8      二進制為:00001000

&兩個數的二進制位相"與"運算,都為1,結果為1

| 兩個數的二進制位相"與"運算,隻要有一個為1,結果為1

^兩個數的二進制位相"與"運算,不同時為1,結果為1,同時為1或者0,結果為0

1 #&  |  ^  ~ >> <<
 2 a=10
 3 print("請輸一個數:",a)
 4 b=8
 5 print("請輸一個數:",b)
 6 c=a&b  #結果為8
 7 d=a|b  #結果為10
 8 e=a^b  #結果為2
 9 f=~a  #結果為-11 
10 g=a>>2  #右移2位,結果為2
11 h=a<<1  #左移1位,結果為20
12 print("a與b按位與運算:",c)
13 print("a與b按位或運算:",d)
14 print("a與b按位異或運算:",e)
15 print("a按位取反運算:",f)
16 print("a按位右移2位運算:",g)
17 print("a按位左移1位運算:",h)      

效果如下:

python資料類型分類以及運算類型

繼續閱讀