The syntax for accessing the elements of a list is the same as for accessing the characters of a string – the bracket operator ([ ]). The expression inside the brackets specifies the index. Unlike strings, lists are mutable. When the bracket operator appears on the left side of an assignment, it identifies the element of the list that will be assigned. You can think of a list as a relationship between indices and elements. This relationship is called a mapping; each index ‘maps to’ one of the elements.

Lists are represented by boxes with the word ‘list’ outside and the elements of the list inside. test refers to a list with three elements indexed 0, 1 and 2.

The bracket operator can appear anywhere in an expression. When it appears on the left side of an assignment, it changes one of the elements in the list.

List indices work the same way as string indices:

  • Any integer expression can be used as an index
  • If you try to read or write an element that does not exist, you get an IndexError
  • If an index has a negative value, it counts backward from the end of the list

The in operator also works in lists.

from Thinking in Python

Lists are mutable的更多相关文章

  1. Think Python - Chapter 10 - Lists

    10.1 A list is a sequenceLike a string, a list is a sequence of values. In a string, the values are ...

  2. Python学习笔记(迭代、模块扩展、GUI 、编码处理等)

    PythonIDLE中的编码处理 http://www.tuicool.com/articles/NbyEBr 原文标题:Python中实际上已经得到了正确的Unicode或某种编码的字符,但是看起来 ...

  3. 关于Python中数据对象的可变性

    先贴上Python官网中对数据模型描述的几段话.(在python官网的 语言参考>>数据模型 那部分) Every object has an identity, a type and a ...

  4. Python Tutorial 学习(五)--Data Structures

    5. Data Structures 这一章来说说Python的数据结构 5.1. More on Lists 之前的文字里面简单的介绍了一些基本的东西,其中就涉及到了list的一点点的使用.当然,它 ...

  5. (python数据分析)第03章 Python的数据结构、函数和文件

    本章讨论Python的内置功能,这些功能本书会用到很多.虽然扩展库,比如pandas和Numpy,使处理大数据集很方便,但它们是和Python的内置数据处理工具一同使用的. 我们会从Python最基础 ...

  6. Python - 2. Built-in Collection Data Types

    From: http://interactivepython.org/courselib/static/pythonds/Introduction/GettingStartedwithData.htm ...

  7. A Complete Tutorial to Learn Data Science with Python from Scratch

    A Complete Tutorial to Learn Data Science with Python from Scratch Introduction It happened few year ...

  8. objects & values & types

    [objects & values & types] 1.Every object has an identity, a type and a value. An object’s i ...

  9. [python数据结构] hashable, list, tuple, set, frozenset

    学习 cs212 unit4 时遇到了 tuple, list, set 同时使用的问题,并且进行了拼接.合并操作.于是我就被弄混了.所以在这里进行一下总结. hashable and unhasha ...

随机推荐

  1. 在MAC下怎样用SSH连接远程LINUXserver

    首页,打开MAC的命令终端,检查是不是用root用的登录的,假设不是的话请输入命令:sudo -i切换到root. 然后,输入:ssh ip地址,假设主机存在的话会提示你输入password,输入正确 ...

  2. Linux内核编译測试

    内核编译: Step 1:配置内核编译选项. make menuconfig Optional Step :排除编译结果文件(.o)等之间的依赖性. make mrproper Optional St ...

  3. php设计模式适配器模式

    php设计模式适配器模式 简介 适配器模式(有时候也称包装样式或者包装)将一个类的接口适配成用户所期待的.一个适配允许通常因为接口不兼容而不能在一起工作的类工作在一起. 其实就是通过一个转换类,这个转 ...

  4. TensorFlow训练MNIST报错ResourceExhaustedError

    title: TensorFlow训练MNIST报错ResourceExhaustedError date: 2018-04-01 12:35:44 categories: deep learning ...

  5. ie浏览器下get方式获取数据无效问题

    在ie浏览器用get方式获取数据时因为发送得到参数地址都是一样的,所以浏览器会优先从缓存获取数据,而不去服务器请求数据,post由于参数不同所以不会影响. 解决方法: 1.  Internet选项-- ...

  6. 转js调优

    随着网络的发展,网速和机器速度的提高,越来越多的网站用到了丰富客户端技术.而现在Ajax则是最为流行的一种方式.javascript是一种解释型 语言,所以能无法达到和C/Java之类的水平,限制了它 ...

  7. cg基础

    1. row-major or column-major 2.orthogonal matrices 3.why cameras always point along the world coordi ...

  8. Optional是以enum和泛型为基础的高阶类型

    结论:1.optionals使用时需要检查:2.可以通过!+赋值语句转化为非optionals. Optional-Generic Enumeration enum Optional<T> ...

  9. ZBrush中Z球(ZSphere和ZSphereⅡ)

    ZSphere可以让用户使用干净的拓扑结构快速建立一个基础网格,然后将其塑造成任何形状.ZSphere的强大在于它非常简单,用户可以从一个单一的ZSphere开始,然后轻松地在其上面增加新的ZSphe ...

  10. FCC编程题之中级算法篇(中)

    介绍 接着上次的中级算法题 目录 1. Missing letters 2. Boo who 3. Sorted Union 4. Convert HTML Entities 5. Spinal Ta ...