提示:python版本为2.7,windows系统

1.列表(List)

  List,是一个有序的集合,可以添加、删除其中的元素。

 >>> colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'purple']
>>> colors
['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'purple']

  colors就被成为List,而 【'red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'purple'】则被称为元素。

 >>> type(colors)
<type 'list'>

  用len()方法可以获取到colors的长度

 >>> len(colors)
7

  获取元素,则用索引0,1,2,3,4....不能超过colors的长度。从0开始,最后则是len(colors) - 1。

 >>> colors[0]
'red'
>>> colors[1]
'orange'
>>> colors[5]
'indigo'
>>> colors[6]
'purple'

  超出长度则会报错。

 >>> colors[7]

 Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
colors[7]
IndexError: list index out of range

 当索引为负数时,则从后往前取,-1开始到-len(colors)

 >>> colors[-1]
'purple'
>>> colors[-7]
'red'
 >>> colors[-8]

 Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
colors[-8]
IndexError: list index out of range

 取索引为2与-2开始的所有

 >>> colors[2:]
['yellow', 'green', 'blue', 'indigo', 'purple']
>>> colors[-2:]
['indigo', 'purple']

 取从索引为3的数据到第5个,第一个索引为0开始,第二个索引为1开始的。

 >>> colors[3:5]
['green', 'blue']

 添加元素,在末尾

 >>> colors.append('pink')
>>> colors
['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'purple', 'pink']

 指定位置添加元素

 >>> colors.insert(1, 'white')
>>> colors
['red', 'white', 'orange', 'yellow', 'green', 'blue', 'indigo', 'purple', 'pink']

 删除元素,在末尾

 >>> colors.pop()
'pink'
>>> colors
['red', 'white', 'orange', 'yellow', 'green', 'blue', 'indigo', 'purple']

 删除指定元素,则为pop(索引)

 >>> colors.pop(1)
'white'
>>> colors
['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'purple']

 修改元素,直接赋值修改

 >>> colors[0] = 'white'
>>> colors
['white', 'orange', 'yellow', 'green', 'blue', 'indigo', 'purple']

 一个List中的类型不一定都是相同的,也可以有数字,列表等在里面

 >>> colors.append(['red', 'black'])
>>> colors
['white', 'orange', 'yellow', 'green', 'blue', 'indigo', 'purple', ['red', 'black']]
>>> type(colors[7])
<type 'list'>
>>> colors[7][0]
'red'
>>> colors[7][1]
'black'

 pop是删除元素并返回这个元素,如果不需要返回还可以用del

 >>> del colors[]
>>> colors
['orange', 'yellow', 'green', 'blue', 'indigo', 'purple', ['red', 'black']]

 List的操作符

 >>> # 加号
>>> colors + ['white']
['orange', 'yellow', 'green', 'blue', 'indigo', 'purple', ['red', 'black'], 'white']
 >>> # *号
>>> colors * 2
['orange', 'yellow', 'green', 'blue', 'indigo', 'purple', ['red', 'black'], 'orange', 'yellow', 'green', 'blue', 'indigo', 'purple', ['red', 'black']]
 >>> # in
>>> 'orange' in colors
True
>>> 'red' in colors
False

提示:中文输出的是unicode,元素则是中文

 >>> money = ['人民币', '美元']
>>> money
['\xc8\xcb\xc3\xf1\xb1\xd2', '\xc3\xc0\xd4\xaa']
 >>> print money[0]
人民币
>>> print money[1]
美元

Python基础:1.数据类型(列表)的更多相关文章

  1. Python基础:数据类型-列表与元组(6)

    Python中数据结构主要有序列.集合和字典. 1. 通用序列操作 Python内置了多种序列,其中最常用的两种:列表和元组.另一种重要的序列是字符串. 列表和元组的主要不同在于:列表是可以修改的,而 ...

  2. 第三节 Python基础之数据类型(列表,元组,字典)

    列表,简单说就是用[]括起来的一大堆数据,这些数据我们叫做元素,元素与元素之间用","隔开,这些元素可以是数字,布尔值,字符串,列表等等,基本所有的数据类型都可以放在列表里边,同时 ...

  3. Python基础之数据类型

    Python基础之数据类型 变量赋值 Python中的变量不需要声明,变量的赋值操作既是变量声明和定义的过程. 每个变量在内存中创建,都包括变量的标识,名称和数据这些信息. 每个变量在使用前都必须赋值 ...

  4. 第二章:python基础,数据类型

    """第二章:python基础,数据类型2.1 变量及身份运算补充2.2 二进制数2.3 字符编码每8位所占的空间位一个比特,这是计算机中最小的表示单位.每8个比特组成一 ...

  5. python基础一数据类型之字典

    摘要: python基础一数据类型之一字典,这篇主要讲字典. 1,定义字典 2,字典的基础知识 3,字典的方法 1,定义字典 1,定义1个空字典 dict1 = {} 2,定义字典 dict1 = d ...

  6. python学习第八讲,python中的数据类型,列表,元祖,字典,之字典使用与介绍

    目录 python学习第八讲,python中的数据类型,列表,元祖,字典,之字典使用与介绍.md 一丶字典 1.字典的定义 2.字典的使用. 3.字典的常用方法. python学习第八讲,python ...

  7. python学习第七讲,python中的数据类型,列表,元祖,字典,之元祖使用与介绍

    目录 python学习第七讲,python中的数据类型,列表,元祖,字典,之元祖使用与介绍 一丶元祖 1.元祖简介 2.元祖变量的定义 3.元祖变量的常用操作. 4.元祖的遍历 5.元祖的应用场景 p ...

  8. python学习第六讲,python中的数据类型,列表,元祖,字典,之列表使用与介绍

    目录 python学习第六讲,python中的数据类型,列表,元祖,字典,之列表使用与介绍. 二丶列表,其它语言称为数组 1.列表的定义,以及语法 2.列表的使用,以及常用方法. 3.列表的常用操作 ...

  9. 『Python基础-8』列表

    『Python基础-8』列表 1. 列表的基本概念 列表让你能够在一个地方存储成组的信息,其中可以只包含几个 元素,也可以包含数百万个元素. 列表由一系列按特定顺序排列的元素组成.你可以创建包含字母表 ...

  10. python基础一数据类型之列表

    摘要: python基础一中写到列表,那么这篇主要讲列表. 1,定义列表 2,列表.元祖.字符串都属于序列,都可以用用索引和切片. 3,列表的方法 1,定义列表 list1 = ['a','b',1, ...

随机推荐

  1. lightoj 1015

    水题,统计大于0的和. #include<cstdio> int main(){ int t, n, tmp; scanf("%d", &t); for(int ...

  2. 020自动化测试 PK 手动测试

    一.手工测试为什么不可替代 手工测试是不可替代的,因为人是具有很强只能判断能力的,而工具是相对机械缺乏思维能力的东西 工具是人开发出来的 二.手工测试不可替代的表现 测试用例的设计:需要tester有 ...

  3. 018如何建立自动化框架 how to bulid the framwork

    本讲包括: 一. objective 二. How to bulid 三. Keyview of frawork (关键视图) 四. conclusion automation framwork:自动 ...

  4. python bisect模块

    转发:http://www.cnblogs.com/skydesign/archive/2011/09/02/2163592.html 先看看模块的结构: 前面五个属性大家感兴趣可以打出来看看数值,这 ...

  5. leetcode—Palindrome 解题报告

    1.题目描述 Given a string s, partition s such that every substring of the partition is a palindrome. Ret ...

  6. 【Hadoop学习】CDH5.2安装部署

    [时间]2014年11月19日 [平台]Centos 6.5 [工具]scp [软件]jdk-7u67-linux-x64.rpm CDH5.2.0-hadoop2.5.0 [步骤] 1. 准备条件 ...

  7. Django中的Model(字段)

    Model Django中的model是用来操作数据库的,Model是一个ORM框架,我们只需要关心model的操作,而不需要关心到底是哪一种数据库. 一.基本知识: 数据库引擎: Django中自带 ...

  8. 配置nginx,支持php的pathinfo路径模式

    nginx模式默认是不支持pathinfo模式的,类似index.php/index形式的url会被提示找不到页面.下面的通过正则找出实际文件路径和pathinfo部分的方法,让nginx支持path ...

  9. A Tour of Go Methods

    Go does not have classes. However, you can define methods on struct types. The method receiver appea ...

  10. 第十三章、学习 Shell Scripts 条件判断式

    利用 if .... then 单层.简单条件判断式 if [ 条件判断式 ]; then 当条件判断式成立时,可以进行的命令工作内容: fi <==将 if 反过来写,就成为 fi !结束 i ...