Python 官方文档:入门教程 => 点击学习
这篇文章主要介绍“python元组和列表的作用”,在日常操作中,相信很多人在Python元组和列表的作用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”python元组和列表的作用”的疑惑有所帮助!接下来,请跟
这篇文章主要介绍“python元组和列表的作用”,在日常操作中,相信很多人在Python元组和列表的作用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”python元组和列表的作用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
In [41]: len(a)Out[41]: 5In [42]: a+'f'Out[42]: 'abcdef'In [43]: aOut[43]: 'abcde'In [44]: print aabcdeIn [45]: a * 10Out[45]: 'abcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcdeabcde'In [46]: '#'*50Out[46]: '##################################################'In [47]: a + 1---------------------------------------------------------------------------TypeError Traceback (most recent call last)<ipython-input-47-a1bd27f4633f> in <module>()----> 1 a + 1TypeError: cannot concatenate 'str' and 'int' objectsIn [48]: ‘#’ * ‘a'File "<ipython-input-48-697a37d8b47d>", line 1‘#’ * ‘a'^SyntaxError: invalid syntaxIn [49]: 'a' in aOut[49]: TrueIn [50]: aOut[50]: 'abcde'In [51]: 'f' in aOut[51]: FalseIn [52]: 'f' not in aOut[52]: TrueIn [53]: 'f' not in a +'f'Out[53]: FalseIn [54]: max(a)Out[54]: 'e'In [55]: min(a)Out[55]: 'a'In [56]: cmp(a,'abcde')Out[56]: 0In [57]: cmp(a,'abcdef')Out[57]: -1In [58]: cmp(a+'g','abcde')Out[58]: 1In [59]: help(cmp)Help on built-in function cmp in module __builtin__:cmp(...)cmp(x, y) -> integerReturn negative if x<y, zero if x==y, positive if x>y.(END)...skipping...Help on built-in function cmp in module __builtin__:cmp(...)cmp(x, y) -> integerReturn negative if x<y, zero if x==y, positive if x>y.
元组和列表相似
元组和字符串一样是不可变的
元组可以存储一系列的值
元组常在用户定义的函数能够安全的采用一组值的时候,即被使用的元组的值不会改变。
In [60]: t=('a',1,(1,))In [61]: tOut[61]: ('a', 1, (1,))In [62]: t1=(1)In [63]: type(t1)Out[63]: intIn [64]: t1=(1,)In [65]: type(t1)Out[65]: tupleIn [66]: aOut[66]: 'abcde'In [67]: t =(a,'b','c')In [68]: tOut[68]: ('abcde', 'b', 'c')In [69]: first,second,third=tIn [70]: firstOut[70]: 'abcde'In [71]: secondOut[71]: 'b'In [72]: thirdOut[72]: 'c'In [73]: t. File "<ipython-input-73-a9f65571ee48>", line 1 t. ^SyntaxError: invalid syntaxIn [74]: t.countOut[74]: <function count>In [75]: t. File "<ipython-input-75-a9f65571ee48>", line 1 t. ^SyntaxError: invalid syntaxIn [76]: help(t.count)---------------------------------------------------------------------------KeyboardInterrupt Traceback (most recent call last)<ipython-input-76-aca5cb18b0a7> in <module>()----> 1 help(t.count)In [78]: t.count('b')Out[78]: 1In [79]: t.count('z' ...: )Out[79]: 0In [80]: tOut[80]: ('abcde', 'b', 'c')In [81]: t.count(a)Out[81]: 1In [82]: t.count('a')Out[82]: 0In [84]: t.index('b')Out[84]: 1In [85]: t.index('c')Out[85]: 2In [86]:
列表:
In [86]: list3 =['a',1,(1,),['hello','python']]In [87]: list3Out[87]: ['a', 1, (1,), ['hello', 'python']]In [88]: len(list3)Out[88]: 4In [89]: list3[0]Out[89]: 'a'In [90]: list3[0]='b'In [91]: list3Out[91]: ['b', 1, (1,), ['hello', 'python']]In [92]: list2=[]In [93]: list2.append('linux')In [94]: list2Out[94]: ['linux']In [95]: list3+list2Out[95]: ['b', 1, (1,), ['hello', 'python'], 'linux']In [96]: (list3+list2)*2Out[96]: ['b', 1, (1,), ['hello', 'python'], 'linux', 'b', 1, (1,), ['hello', 'python'], 'linux']In [97]: list3Out[97]: ['b', 1, (1,), ['hello', 'python']]In [98]: dev list3[-1] File "<ipython-input-98-a37927fbd6ca>", line 1 dev list3[-1] ^SyntaxError: invalid syntaxIn [99]: del list3[-1]In [100]: list3Out[100]: ['b', 1, (1,)]In [101]: list2Out[101]: ['linux']In [102]: del list2In [103]: list2---------------------------------------------------------------------------NameError Traceback (most recent call last)<ipython-input-103-db9b7629a516> in <module>()----> 1 list2NameError: name 'list2' is not definedIn [104]: help list.remove File "<ipython-input-104-178e53c9bdee>", line 1 help list.remove ^SyntaxError: invalid syntaxIn [105]: help list3.remove File "<ipython-input-105-b98070bef357>", line 1 help list3.remove ^SyntaxError: invalid syntaxIn [106]: helplist3.remove---------------------------------------------------------------------------NameError Traceback (most recent call last)<ipython-input-106-e9ee3419c47a> in <module>()----> 1 helplist3.removeNameError: name 'helplist3' is not definedIn [107]: list3Out[107]: ['b', 1, (1,)]In [108]: list3.append(1)In [109]: list3Out[109]: ['b', 1, (1,), 1]In [110]: list3.remove(1)In [111]: list3Out[111]: ['b', (1,), 1]In [112]: list3.remove(2)---------------------------------------------------------------------------ValueError Traceback (most recent call last)<ipython-input-112-700afa01bf12> in <module>()----> 1 list3.remove(2)ValueError: list.remove(x): x not in listIn [113]: list3.indexOut[113]: <function index>In [114]: list3.index(0)---------------------------------------------------------------------------ValueError Traceback (most recent call last)<ipython-input-114-05fab3a45675> in <module>()----> 1 list3.index(0)ValueError: 0 is not in listIn [115]: help list(3) File "<ipython-input-115-81a30b2a31f7>", line 1 help list(3) ^SyntaxError: invalid syntaxIn [116]: list3.count()---------------------------------------------------------------------------TypeError Traceback (most recent call last)<ipython-input-116-d977b332b071> in <module>()----> 1 list3.count()TypeError: count() takes exactly one argument (0 given)In [117]: list3.remove(1)In [118]: list3Out[118]: ['b', (1,)]In [119]: help(list3.remove)In [120]: pop()---------------------------------------------------------------------------NameError Traceback (most recent call last)<ipython-input-120-de840d9b1602> in <module>()----> 1 pop()NameError: name 'pop' is not definedIn [121]: 'a' in list3;In [122]: 'a' in list3Out[122]: FalseIn [123]: list3.pop(0)Out[123]: 'b'In [124]: list3.pop(1)---------------------------------------------------------------------------IndexError Traceback (most recent call last)<ipython-input-124-48a521f74801> in <module>()----> 1 list3.pop(1)IndexError: pop index out of rangeIn [125]: list3Out[125]: [(1,)]In [126]: list3.append(4,5)---------------------------------------------------------------------------TypeError Traceback (most recent call last)<ipython-input-126-cd4f2cf121c4> in <module>()----> 1 list3.append(4,5)TypeError: append() takes exactly one argument (2 given)In [127]: list3.append(4)In [128]: list3.append(5)In [129]: list3Out[129]: [(1,), 4, 5]In [130]: list3.pop(2)Out[130]: 5In [131]: help(list3.pop(0))In [132]: help(list3.pop())In [133]: help(list3.pop)In [134]: list3Out[134]: []In [136]: list3.append('a')In [137]: list3.append(3)In [138]: list3.append(4)In [139]: list3.append(5)In [140]: list3Out[140]: ['a', 3, 4, 5]In [142]: list3.insert(1,'list1')In [143]: list3Out[143]: ['a', 'list1', 3, 4, 5]In [150]: list1=[]In [151]: In [151]: list3=[]In [152]: list3.insert(1, list1)In [153]: list3Out[153]: [[]]In [154]: list3[0].append('abc')In [155]: list3Out[155]: [['abc']]
到此,关于“python元组和列表的作用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!
--结束END--
本文标题: python元组和列表的作用
本文链接: https://www.lsjlt.com/news/229881.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0