py--list

Python 3.7.5 (tags/v3.7.5:5c02a39a0b, Oct 15 2019, 00:11:34) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> lsit =[]
>>> list =[]
>>> type(list)
<class 'list'>
>>> list=[1,2,3,4]
>>> list
[1, 2, 3, 4]
>>> list=['str',1,2]
>>> list
['str', 1, 2]
>>> list=[1,'str',['list',123]]
>>> list
[1, 'str', ['list', 123]]
>>> len(list}
SyntaxError: invalid syntax
>>> len(list)
3
>>> list[2][1]
123
>>> #可以放任何類型
>>> list=list([1,2,3])
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    list=list([1,2,3])
TypeError: 'list' object is not callable
>>> #操作
>>> a=[1,2,3,4]
>>> b=[2,3,4,5]
>>> a+b
[1, 2, 3, 4, 2, 3, 4, 5]
>>> type(a+b)
<class 'list'>
>>> a*2
[1, 2, 3, 4, 1, 2, 3, 4]
>>> a
[1, 2, 3, 4]
>>> del a[0]
>>> a
[2, 3, 4]
>>> 
>>> a[0:]
[2, 3, 4]
>>> a[0]=123
>>> a
[123, 3, 4]
>>> 3 in a
True
>>> 7 not in a
True
>>> a=[1,2,3,4,5]
>>> a[1:]
[2, 3, 4, 5]
>>> a
[1, 2, 3, 4, 5]
>>> del a[3:]
>>> a
[1, 2, 3]
>>> str='hello python'
>>> 'hello' in str
True
>>> list=['apple','orange','apple','orange']
>>> list.count('apple')
2
>>> list.index('apple')
0
>>> list.append('pear')
>>> list
['apple', 'orange', 'apple', 'orange', 'pear']
>>> list.append([12,34])
>>> list
['apple', 'orange', 'apple', 'orange', 'pear', [12, 34]]
>>> list.insert(1,'hh')
>>> list
['apple', 'hh', 'orange', 'apple', 'orange', 'pear', [12, 34]]
>>> list.remove ('hh')
>>> list
['apple', 'orange', 'apple', 'orange', 'pear', [12, 34]]
>>> list.remove('apple')
>>> list
['orange', 'apple', 'orange', 'pear', [12, 34]]
>>> list.pop()
[12, 34]
>>> list
['orange', 'apple', 'orange', 'pear']
>>> list=[1,2,5,4,3,2]
>>> list.sort()
>>> list
[1, 2, 2, 3, 4, 5]
>>> list=[3,2,11,1,5]
>>> list1= sorted(list)
>>> list
[3, 2, 11, 1, 5]
>>> list1
[1, 2, 3, 5, 11]
>>> list=['liu', 'yong']
>>> list.reverse()
>>> list
['yong', 'liu']
>>> 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章