`

Python学习笔记(六): list

 
阅读更多
#!/usr/bin/python
# Filename: using_list.py

# This is my shopping list
shoplist = ['apple', 'mango', 'carrot', 'banana']

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

print('These items are:', end=' ') # Notice the comma at end of the line
[b]for item in shoplist:
    print(item, end=' ')[/b]

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

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

print('The first item I will buy is', shoplist[0])
[b]olditem = shoplist[0]
del shoplist[0][/b]
print('I bought the', olditem)
newitem = shoplist[0]
print('I bought the', newitem)
print('My shopping list is now', shoplist)


输出:

>>>
I have 4 items to purchase.
These items are: apple mango carrot banana
I also have to buy rice.
My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice']
I will sort my list now
Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice']
The first item I will buy is apple
I bought the apple
I bought the banana
My shopping list is now ['banana', 'carrot', 'mango', 'rice']
>>>

分享到:
评论

相关推荐

    python-notes:Python学习笔记:memo:

    Python笔记学习笔记 :memo: 介绍 有时候想找一个东西(写法),但当下却忘记关键字,所以整理一篇学习笔记,要找资料的时候也比较方便。 有些是网路上找的范例,然后自己再修修改改,或者去参考一些Python-Patterns...

    python学习笔记1-(廖雪峰教程,菜鸟教程)python基础.pdf

    python学习笔记1——(廖雪峰教程,菜鸟教程)python基础 python学习笔记1——(廖雪峰教程,菜鸟教程) >>> classmates = ['Michael', 'Bob', 'Tracy'] >>> len(classmates) 3 ⽤索引来访问list中每⼀个位置的元素...

    python学习笔记-王纯业

    以下是一个Python学习笔记的大纲,涵盖了从基础到进阶的内容。你可以根据自己的学习进度和理解情况,逐步填充和完善这个大纲。 Python学习笔记大纲 一、Python基础 Python简介 Python的历史 Python的特点和应用...

    Python问题解决,重复向列表中添加字典作为元素

    其他python学习笔记集合: Python基础知识详解 从入门到精通 全集索引篇 Python基础知识详解(十)小结,用python实现教师信息管理系统 上篇用python实现教师信息管理系统,这里学几个实现这个系统过程中遇到的问题 ...

    Python 难点重点学习笔记

    Python学习过程中,遇到的python难点、重点问题的学习笔记,以及一些有助于理解的好的内容。包括:list、tupe、dict、set区别、特点,用法;高级特性,包括切片、列表生成式,生成器、迭代器,函数式编程,包括高级...

    赵璐python教程答案-Python学习习题笔记-基础篇.pdf

    赵璐python教程答案_Python学习习题笔记---基础篇 基本数据类型: 1:info = 'abc' info[2] = 'd' 结果是什么,为什么会报错呢? 答: TypeError: 'str' object does not support item assignment 字符串是不可变数据...

    辛辛苦苦手打的Python学习笔记,适合想自学Python的同学

    False, 0, None, 空字符串、空列表(list)、空元组(tuple)、空字典(dict)、空集合(set) 其他对象的布尔值为True 13.分支结构:单分支结构 #以银行取款为例 如: money=1000 s=int(input(‘请输入取款金额:...

    python学习笔记:字典的使用示例详解

    如同list。 复制代码 代码如下:items=[(‘name’,’zhang’),(‘age’,42)]d=dict(items)d[‘name’] len(d):返回项的数量d[k]:返回键k上面的值。d[k]=v:将k对应的值设置为k。del d[k]:删除字典中的这一项。k in d:...

    Python学习笔记基本数据结构之序列类型list tuple range用法分析

    本文实例讲述了Python学习笔记基本数据结构之序列类型list tuple range用法。分享给大家供大家参考,具体如下: list 和 tuple list:列表,由 [] 标识; 有序;可改变列表元素 tuple:元组,由 () 标识; 有序;...

    python学习笔记(08)-案例实操:列表去重

    立即学习:https://edu.csdn.net/course/play/24797/282168?utm_source=blogtoedu 除去列表里重复元素: 新列表搜集法(可以保证元素顺序) 创建新列表 遍历源列表的所有元素,若该元素不在新列表中,将元素添加到新...

    python学习笔记之列表(list)与元组(tuple)详解

    List(列表)是Python中使用最频繁的数据...这篇文章主要给大家介绍了python学习笔记之列表(list)与元组(tuple)的相关资料,文中不仅详细介绍了两者的使用,还分享了list和tuple的相互转换的方法,需要的朋友可以参考下

    Python列表、元祖、字典、集合学习笔记文档(附Python源码)

    在 Python 编程中,我们既需要独立的变量来保存一份数据,也需要序列来保存大量数据。 列表(list)和元组(tuple)比较相似,...本资源是我在学习Python时,记录的列表、元祖、字典、集合学习笔记,供大家参考学习。

    零基础小白的python学习笔记之类和类对象(一)基本定义

    class Mylist(list):#list是父类,而Mylist是继承list的子类,它继承list的属性和方法 pass 3.Python里多态的含义:不同的类调用名字相同的函数,其实现方法不同 class A: def fun(self): print('我是A') class B:...

    python学习笔记之列表生成式

    列表生成式是python内置的非常简单且强大的可以用来创建list的生成式 如果没有列表生成式,我们得用很多行的循环或者条件代码,但是如果有了列表生成式,就能节省很多码字的时间,增加代码的简洁度。 举个例子,如果...

    Python学习笔记(七)——–Python数据结构

    一、相关 数据结构是计算机存储、组织数据的方式。数据结构是指相互之间存在一种或多种特定关系的数据元素的集合。...常用的是:列表(List)、字符串(String)、元组(Tuple)。 3、映射在Python的

    Python量化交易学习笔记(24)——策略多参数优化

    笔记(13)中介绍了在策略中对单个参数进行优化的实现方法,本文将介绍对策略中的多个参数进行优化的方案。 以笔记(14)中介绍的均线交叉策略为例,实现不同长期、短期均线参数组合的优化测试,回测股票为000001...

    Python学习笔记之Zip和Enumerate用法实例分析

    my_zip = list(zip(['a', 'b', 'c'], [1, 2, 3])) print(my_zip) # [('a', 1), ('b', 2), ('c', 3)] 正如 range() 一样,我们需要将其转换为列表或使用循环进行遍历以查看其中的元素。 letters = ['a', 'b', 'c'] ...

    python3学习笔记–python中的数据类型汇总。

    python数据类型汇总一.数值型Number1.int2.float3.complex4.数据类型转换二.字符串str对字符串进行操作,字符串的方法。1. 字符串切片2. 字符串统计3. 判断4. 拼接5. 对字符串操作后输出6. 转换7. 索引三.列表list对...

Global site tag (gtag.js) - Google Analytics