Compared to Java, C #, these programming languages, Python is very flexible and very color. Language design is sometimes illogical, but intuitive and convenient. It's easy to get started with people who don't have a language base.
List slices in a list operation:
>>> t=[0,1,2,3,4,5,6]
>>> T[1:3]
[1, 2]
The colon two-edged number represents the position, and the number starting with the array subscript 0 is its position, and the common sense is different, the habit is good.
The right side of the colon does not represent the length. Just the location.
----------------------------------------------------------------------------------
"List Test"
def test (), int:
t=[0,1, 2, 3, 4, 5, 6]
T.append ([55,56])
Print (t)
T.extend ([7,8])
Print (t)
T.pop (7)
Print (t)
T.sort (reverse= True)
Print (t)
return 0
Test ()
return Result:
[0, 1, 2, 3, 4, 5, 6, [55, 56]]
[0, 1, 2, 3, 4, 5, 6, [55, 56], 7, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[8, 7, 6, 5, 4, 3, 2, 1, 0]
"Delete Element"
def deleteItem () int:
t = [0, 1, 23, 23, 2, 23, 2]
v = t.pop (0) #删除同时返回值, equivalent to fetching data
Print (t)
Print (v)
Del (t[0]) #删除但不返回值
Print (t)
T.remove (2) #只是删除遇到的第一个
Print (t)
return 0
DeleteItem ()
return Result:
[1, 23, 23, 2, 23, 2]
0
[23, 23, 2, 23, 2]
[23, 23, 23, 2]
Why is python so different?