python基础教程(第三版)学习笔记(二)

第二章 列表和元组
2.1序列概述
Python内建的序列包括列表、元组、字符串、Unicode字符串、buffer对象和xrange对象等,本章仅介绍列表和元组。
列表和元组的主要不同在于,列表的元素是可以修改的,而元组不可以单独更改其中的单个元素,但可以更改元组的整体内容。
在序列中,每个元素都有编号。
2.2通用的序列操作
有几种操作适用于所有序列,包括索引、切片、相加、相乘和成员资格检查。另外,Python还提供了一些内置函数,可用于确定序列的长度以及找出序列中最大和最小的元素。
2.2.1索引
序列中的所有元素都有编号——从0开始递增。你可像下面这样使用编号来访问各个元素:
'''

grStr="hello"
print(grStr[0])


'''

h


------------------
(program exited with code: 0)

请按任意键继续. . 

.
当你使用负数索引时,Python将从右(即从最后一个元素)开始往左数。
'''

print(grStr[-1])


'''

o


------------------
(program exited with code: 0)

请按任意键继续. . .


2.2.2切片
除使用索引来访问单个元素外,还可使用切片来访问特定范围内的元素。为此,可使用两个索引,并用冒号分隔:
'''

print(grStr[1:3])


'''

el


------------------
(program exited with code: 0)

请按任意键继续. . .

1、一个参数:
a、在冒号前面将截取若是正数向后截取,负数向前。
b、在冒号后若是正数向前截取负数向后。
c、没有参数截取全部序列。
2、步长
两个冒号三个参数最后一个是步长。比如lest[n:m:k]其中k是步长。步长不能为0,否则无法向前移动,但可以为负数,即从右向左提取元素。
2.2.3序列拼接
可使用加法运算符来拼接序列
'''

print([1,2,3,4]+[4,3,2,1])


'''

[1, 2, 3, 4, 4, 3, 2, 1]


------------------
(program exited with code: 0)

请按任意键继续. . .


'''

print([1,2,3,4]+["Hello","world"])
'''
[1, 2, 3, 4, 'Hello', 'world']


------------------
(program exited with code: 0)

请按任意键继续. . .


一般而言,不能拼接不同类型的序列。

print([1,2,3,4]+"Hello world")
Traceback (most recent call last):
  File "xx.py", line 2, in <module>
    print([1,2,3,4]+"Hello world")
TypeError: can only concatenate list (not "str") to list


------------------
(program exited with code: 1)

请按任意键继续. . .


2.2.4用*建立序列
将序列与数x“相乘”时,将重复这个序列x次来创建一个新序列:
'''

print("Hello world"*4)


'''

Hello worldHello worldHello worldHello world


------------------
(program exited with code: 0)

请按任意键继续. . .


'''

print([1,2,3]*3)


'''

[1, 2, 3, 1, 2, 3, 1, 2, 3]


------------------
(program exited with code: 0)

请按任意键继续. . .


在python中None表示“空”,因此可以用None建立一个有若干元素的序列。
'''

print([None]*5)


'''

[None, None, None, None, None]


------------------
(program exited with code: 0)

请按任意键继续. . .


'''

l=[]*5
print(len(l))
print(len([None]*5))


'''

0
5


------------------
(program exited with code: 0)

请按任意键继续. . .


注:len()为python的内置函数,用来输出序列长(元素个数)。
2.2.5包含检查 in
要检查特定的值是否包含在序列中,可使用运算符in。返回相应的值:满足时返回True,不满足时返回False。
'''

print("x" in "wrold")


'''

False


------------------
(program exited with code: 0)

请按任意键继续. . .


'''

print(3 in [1,2,3,4])


'''

True


------------------
(program exited with code: 0)

请按任意键继续. . .


另外,在Python中还有两个用于序列的内置函数max()和min(),就不再演示了。
2.3 列表
2.3.1 list
list其实不是Python的内置函数,而是“类”(关于这个革命性的概念以后在详细讨论),在这里姑且把list当作函数用吧!
'''

rts="Hello wrold"
print(list(rts))


'''

['H', 'e', 'l', 'l', 'o', ' ', 'w', 'r', 'o', 'l', 'd']


------------------
(program exited with code: 0)

请按任意键继续. . .


神奇吧?
2.3.2列表的基本操作
列表除了具有序列操作之外,还有自己的特点。
1、改变元素值
'''

x=[1,2,"me",3,"you"]
x[4]=5
print(x)


'''

[1, 2, 'me', 3, 5]


------------------
(program exited with code: 0)

请按任意键继续. . .


2、删除元素
从列表中删除元素也很容易,只需使用del语句即可。
'''

del x[3]
print(x)


'''

[1, 2, 'me', 5]


------------------
(program exited with code: 0)

请按任意键继续. . .


注意:del没有返回值,例如:

d=(del x[3])


会报如下作错误:

python -m py_compile "xx.py" (在目录 E:\pythonProjects 中)
  File "xx.py", line 6
    d=(del x[3])
         ^
SyntaxError: invalid syntax
编译失败。


3、给切片赋值
'''

x[:2]=["you","he"]
print(x)


'''

['you', 'he', 'me', 5]


------------------
(program exited with code: 0)

请按任意键继续. . .


使用切片赋值还可在不替换原有元素的情况下插入新元素。
'''

x[4:1]=[6,7,8,9,10]
print(x)


'''

['you', 'he', 'me', 5, 6, 7, 8, 9, 10]


------------------
(program exited with code: 0)

请按任意键继续. . .


也可以用空序列[]来删除列表中相应元素。
'''

x[2:6]=[]
print(x)


'''

['you', 'he', 8, 9, 10]


------------------
(program exited with code: 0)

请按任意键继续. . .


2.3.3列表方法:
方法,一般指某对象的方法,和函数不同,使用方法要用英语句点显示指定其所在的对象,其格式为:
object.metthod(parameter)
1、append
用于将一个对象附加到列表末尾。
'''

x.append("she")
print(x)


'''

['you', 'he', 8, 9, 10, 'she']


------------------
(program exited with code: 0)

请按任意键继续. . .


2、clear
用于清空列表的内容。
'''

s=x.clear()
print(s)
print(x)


'''

None
[]


------------------
(program exited with code: 0)

请按任意键继续. . 

.
3、copy
复制列表。
'''

a=[1,2,3,4]
print(a)
b=a.copy()
print(b)
a.append(6)
print(a)
print(b)


'''

[1, 2, 3, 4]
[1, 2, 3, 4]
[1, 2, 3, 4, 6]
[1, 2, 3, 4]


------------------
(program exited with code: 0)

请按任意键继续. . .


copy是建立了一个和原列表不同的列表,他和a=b不同,a=b是赋值,当b发生变化时a同时发生变化。
这类似于使用a[:]或list(a),它们也都复制a。
4、count
计算指定的元素在列表中出现了多少次。
5、extend
能够同时将多个值附加到列表末尾,为此可将这些值组成的序列作为参数提供给方法extend。换而言之,你可使用一个列表来扩展另一个列表。
'''

a.extend(b)
print(a)


'''

[1, 2, 3, 4, 6, 1, 2, 3, 4]


------------------
(program exited with code: 0)

请按任意键继续. . .


用+拼接出来的列表与前一个示例扩展得到的列表完全相同,但在这里用+拼接a并没有被修改,而是产生出新的列表,而用extend却改变了a。但是拼接的效率要比extend低。
6、index
在列表中查找指定值第一次出现的索引。
7、insert
用于将一个对象插入列表。
格式为:对象.insert(插入位置,要插入的对象)。
8、pop
从列表中删除末尾的那个元素。
9、remove
用于删除第一个指定值的元素。
'''

str1=["a","b","a","b","c"]
print(str1)
str2=str1.remove("b")
print(str2)
print(str1)


'''

['a', 'b', 'a', 'b', 'c']
None
['a', 'a', 'b', 'c']


------------------
(program exited with code: 0)

请按任意键继续. . .


10、reverse
按相反的顺序排列列表中的元素。
11、sort
用于对列表就地排序。
2.4 元组
与列表一样,元组也是序列,唯一的差别在于元组是不能修改的(你可能注意到了,字符串也不能修改)。元组语法很简单,只要将一些值用逗号分隔,就能自动创建一个元组,或者用括号()定义。。
'''

tup=1,2,3,4
print(tup)
tupl=(1,2,3,4,5)
print(tupl)


'''

(1, 2, 3, 4)
(1, 2, 3, 4, 5)


------------------
(program exited with code: 0)

请按任意键继续. . .


第三章 使用字符串(待续)

猜你喜欢

转载自blog.csdn.net/micorjun/article/details/83310860