前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python中的列表

Python中的列表

作者头像
benym
发布2022-07-14 12:31:01
4.9K0
发布2022-07-14 12:31:01
举报
文章被收录于专栏:后端知识体系后端知识体系

# 列表

列表 是一种用于保存一系列有序项目的集合,也就是说,你可以利用列表保存一串项目的序 列。想象起来也不难,你可以想象你有一张购物清单,上面列出了需要购买的商品,除开在 购物清单上你可能为每件物品都单独列一行,在 Python 中你需要在它们之间多加上一个逗 号。

# 代码

代码语言:javascript
复制
# 列表
# This is my shopping list
'''
在这里要注意在调用  print  函数时我们使用  end  参数,这样就能通过一个空格来结束输出
工作,而不是通常的换行。
'''
shoplist = ['apple', 'mango', 'carrot', 'banana']

print('I have', len(shoplist), 'items to purchase')

print('These item are:', end=' ')
for item in shoplist:
    print(item, end=' ')

print('\nI also have to buy rice.')
shoplist.append('rice')
print('My shopping list is now', shoplist)

print('I will sort my list now')
shoplist.sort()
print('Sorted shopping list is', shoplist)

print('The first item I will buy is', shoplist[0])
olditem = shoplist[0]
del shoplist[0]
print('I bought the', olditem)
print('My shopping list is now', shoplist)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-07-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • # 列表
  • # 代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档