運算子
python 表示式
結果描述
支援的資料型別
+[1, 2] + [3, 4]
[1, 2, 3, 4]
合併字串、列表、元組
*['hi!'] * 4
['hi!', 'hi!', 'hi!', 'hi!']
複製字串、列表、元組
in3 in (1, 2, 3)
true
元素是否存在
字串、列表、元組、字典
not in
4 not in (1, 2, 3)
true
元素是否不存在
字串、列表、元組、字典
>>> "hello " + "itcast"
'hello itcast'
>>> [1, 2] + [3, 4]
[1, 2, 3, 4]
>>> ('a', 'b') + ('c', 'd')
('a', 'b', 'c', 'd')
in>>> 'ab' * 4
'ababab'
>>> [1, 2] * 4
[1, 2, 1, 2, 1, 2, 1, 2]
>>> ('a', 'b') * 4
('a', 'b', 'a', 'b', 'a', 'b', 'a', 'b')
注意,in在對字典操作時,判斷的是字典的鍵>>> 'itc' in 'hello itcast'
true
>>> 3 in [1, 2]
false
>>> 4 in (1, 2, 3, 4)
true
>>> "name" in
true
python包含了以下內建函式
序號方法描述1
cmp(item1, item2)
比較兩個值
2len(item)
計算容器中元素個數
3max(item)
返回容器中元素最大值
4min(item)
返回容器中元素最小值
5del(item)
刪除變數
cmp
注意:cmp在比較字典資料時,先比較鍵,再比較值。>>> cmp("hello", "itcast")
-1>>> cmp("itcast", "hello")
1>>> cmp("itcast", "itcast")
0>>> cmp([1, 2], [3, 4])
-1>>> cmp([1, 2], [1, 1])
1>>> cmp([1, 2], [1, 2, 3])
-1>>> cmp(, )
-1>>> cmp(, )
1>>> cmp(, )
-1
len
注意:len在操作字典資料時,返回的是鍵值對個數。>>> len("hello itcast")
12>>> len([1, 2, 3, 4])
4>>> len((3,4))
2>>> len()
2
max
del>>> max("hello itcast")
't'>>> max([1,4,522,3,4])
522>>> max()
'b'>>> max()
'b'>>> max()
'c'
del有兩種用法,一種是del加空格,另一種是del()
>>> a = 1
>>> a
1>>> del a
>>> a
traceback (most recent call last):
file "", line 1, in nameerror: name 'a' is not defined
>>> a = ['a', 'b']
>>> del a[0]
>>> a
['b']
>>> del(a)
>>> a
traceback (most recent call last):
file "", line 1, in nameerror: name 'a' is not defined
>>> tuple1 = [(2,3),(4,5)]
>>> tuple1[0]
(2, 3)
>>> tuple1[0][0]
2>>> tuple1[0][2]
traceback (most recent call last):
file "", line 1, in indexerror: tuple index out of range
>>> tuple1[0][1]
3>>> tuple1[2][2]
traceback (most recent call last):
file "", line 1, in indexerror: list index out of range
>>> tuple2 = tuple1+[(3)]
>>> tuple2
[(2, 3), (4, 5), 3]
>>> tuple2[2]
3>>> tuple2[2][0]
traceback (most recent call last):
file "", line 1, in typeerror: 'int' object is not subscriptable
python 公共方法
1.計算長度 value wangdianchao 計算字元個數 長度 number len value print number 2.索引取值 value wangdianchao 獲取value 0 位置的字元 number value 0 print number value wangdian...
Python的公共方法
完整的 for 迴圈語法 python 包含了以下內建函式 函式描述 備註len item 計算容器中元素個數 del item 刪除變數 del 有兩種方式 max item 返回容器中元素最大值 如果是字典,只針對 key 比較 min item 返回容器中元素最小值 如果是字典,只針對 key...
python 容器型別公共方法
1.python內建函式 函式描述 備註len item 計算容器中元素的個數 del item 刪除變數 del 有兩種方式 max item 返回容器中元素最大值 如果是字典,只針對key比較 min item 返回容器中元素最小值 如果是字典,只針對key比較 2.運算子 運算子python表...