A list in Python

Source: Internet
Author: User

  1. # 列表用中括号包围,列表中的数据项用‘,’分隔,用‘=’将列表赋值给一个标示符(请注意:Python中的变量标识符没有类型)
  2. >>> cast =["item0",‘item1‘,‘item2‘,"item3"]
  3. #print()函数用于在屏幕上输出
  4. >>>print(cast)
  5. [‘item0‘,‘item1‘,‘item2‘,‘item3‘]
  6. #python中的列表类似于其它编程语言中的数组,可以通过中括号偏移量记法来访问列表中的指定元素
  7. >>>print(cast[1])
  8. item1
  9. #可以使用len(列表)得到列表的数据项数目
  10. >>>print(len(cast))
  11. 4
Some functions of the list
  1. #append()用于往列表末尾添加数据项
  2. >>> cast.append("item4")
  3. >>>print(cast)
  4. [‘item0‘,‘item1‘,‘item2‘,‘item3‘,‘item4‘]
  5. #pop()用于返回并删除列表的最后一个数据项
  6. >>> cast.pop()
  7. ‘item4‘
  8. >>> cast
  9. [‘item0‘,‘item1‘,‘item2‘,‘item3‘]
  10. >>> cast1 =["item10","item11",‘item12‘]
  11. >>> cast1
  12. [‘item10‘,‘item11‘,‘item12‘]
  13. #可以通过extend()往列表末尾添加另一个数据项集合
  14. >>> cast.extend(cast1)
  15. #print(cast)或者cast都能够在屏幕输出
  16. >>> cast
  17. [‘item0‘,‘item1‘,‘item2‘,‘item3‘,‘item10‘,‘item11‘,‘item12‘]
  18. #insert()用于在列表的指定位置插入新的数据项
  19. >>> cast.insert(0,"item-1")
  20. >>>print(cast)
  21. [‘item-1‘,‘item0‘,‘item1‘,‘item2‘,‘item3‘,‘item10‘,‘item11‘,‘item12‘]
  22. #remove()可以用于删除列表中的指定项
  23. >>> cast.remove("item-1")
  24. >>>print(cast)
  25. [‘item0‘,‘item1‘,‘item2‘,‘item3‘,‘item10‘,‘item11‘,‘item12‘]



From for notes (Wiz)



A list in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.