python快速⼊门第3版电⼦书_python编程:从⼊门到实践第3
章
线性代数不会坑的,这两天编辑器t有点抽风,不太好打字,只能先发python了
3.1列表([ ]表⽰)数字or字母or姓名ect.任何元素都可以加⼊列表中,元素之间可以没有任何关系
bicycles = ['trek','cannondale','redline']
print(bicycles)
['trek', 'cannondale', 'redline']
访问列表元素(列表名称[元素索引])
bicycles = ['trek','cannondale','redline']
print(bicycles[0])
trek
输出的结果更加整洁可使⽤title()函数(列表名称[元素索引].title())
print(bicycles[0].title())
Trek
元素索引从0开始⽽不是1,元素的最后⼀个指定为-1,倒数第⼆个元素-2.以此类推,在不知道列表长度的情况下可以访问最后的元素
bicycles = ['trek','cannondale','redline']
print(bicycles[-1].title())vba高级编程宝典
print(bicycles[0].title())
print(bicycles[1].title())
Redline
Trek
Cannondale
使⽤列表的值各个值
bicycles = ['trek','cannondale','redline']
message = "this is a new "+bicycles[0].title() + "."
print(message)
this is a new Trek.
作业:
3-1 姓名:将⼀些朋友的姓名存储在⼀个列表中,并将其命名为names 。依次访问该列表中的每个元素,从⽽将每个朋友的姓名都打印出来。
names = ["Bell","Peter","Alice","Ben"]
print(names[0])
print(names[1])
print(names[2])
print(names[3])
Bell
Peterpython入门教程(非常详细)书
Alice
Ben
3-2 问候语:继续使⽤练习3-1中的列表,但不打印每个朋友的姓名,⽽为每⼈打印⼀条消息。每条消息都包含相同的问候语,但抬头为相应朋友的姓名。
names = ["Bell","Peter","Alice","Ben"]
message = names[0]+ ", nice to meet you! "
print(message)
message = names[1]+ ", nice to meet you! "
print(message)
message = names[2]+ ", nice to meet you! "
print(message)
message = names[3]+ ", nice to meet you! "
print(message)
Peter, nice to meet you!
Alice, nice to meet you!
Ben, nice to meet you!
3-3 ⾃⼰的列表:想想你喜欢的通勤⽅式,如骑摩托车或开汽车,并创建⼀个包含多种通勤⽅式的列表。根据该列表打印⼀系列有关这些通勤⽅式的宣⾔,如“I would ways = ["bike","on foot","train"]
ways = ["bike","on foot","train"]
sayings = "I would like to own a Honda "+ ways[0]
print(sayings)
javascript中setintervalsayings = "I like "+ ways[1].title()
print(sayings)
sayings = "The "+ ways[-1] + " is fast!"
print(sayings)
I would like to own a Honda bike
I like On Foot
The train is fast!
3.2 修改,添加和删除元素
修改列表元素
ways = ["bike","on foot","train"]
print(ways)
java安装教程及环境配置图片ways[0]="plane"
print(ways)
['bike', 'on foot', 'train']
sql条件查询['plane', 'on foot', 'train']
添加元素
列表末尾添加
ways = ["bike","on foot","train"]
ways.append('plane')
print(ways)
['bike', 'on foot', 'train', 'plane']
ways = []
ways.append('plane')
ways.append('on foot')
ways.append('bike')
ways.append('train')
print(ways)
['plane', 'on foot', 'bike', 'train']
列表中插⼊元素
insert(元素索引,元素)
ways = ["bike","on foot","train"]
print(ways)
ways.insert(0,'plane')
print(ways)
['bike', 'on foot', 'train']
['plane', 'bike', 'on foot', 'train']
从列表中删除元素
del语句
ways = ["bike","on foot","train"]
print(ways)
del ways[0]
print(ways)
['bike', 'on foot', 'train']
['on foot', 'train']
pop()函数 pop(弹出):列表就像⼀个栈,⽽删除列表末尾的元素相当于弹出栈顶元素,按时间存储pop()打印最后的
ways = ["bike","on foot","train"]
print(ways)
poped_ways = ways.pop()
print(ways)
print(ways)
print(poped_ways)
['bike', 'on foot', 'train']
['bike', 'on foot']
train
根据值删除元素
remove() 这个函数有啥⼦⽤呢?当你不知道要删除的元素的位置,只知道要删除的元素的值ways = ["bike","on foot","train"]
print(ways)
['bike', 'on foot']
amazing当我们将值从列表中删除时,还可以接着使⽤值
ways = ["bike","on foot","train"]
print(ways)
too_fast = "train"
print(ways)
print("The " + too_fast +" is fast.")
['bike', 'on foot', 'train']
['bike', 'on foot']
The train is fast.
元素从列表中删除时,但是它还储存在too_fast 中
注意啦:::remove()只能删除第⼀个指定的值
ways = ["bike","on foot","train","train"]
print(ways)
print(ways)
['bike', 'on foot', 'train', 'train']
html下拉列表如何设置['bike', 'on foot', 'train']
组织列表
永久性排序
sort(),按字母顺序排序,永久性修改,⽆法恢复
ways = ["bike","on foot","train","car"]
ways.sort()
print(ways)
['bike', 'car', 'on foot', 'train']
与字母顺序相反的排序,久性修改,⽆法恢复
ways.sort(reverse=True) 向sort()传递reverse=True
ways = ["bike","on foot","train","car"]
ways.sort(reverse=True)
print(ways)
临时排序
sorted()函数能按特定的顺序显⽰元素,不影响它们在列表中的原始排序
ways = ["bike","on foot","train","car"]
print("Here are the ways:")
print(ways)
print("nHere are the ways:")
print(sorted(ways))
print("nHere are the ways:")
print(ways)
Here are the ways:
['bike', 'on foot', 'train', 'car']
['bike', 'on foot', 'train', 'car']
Here are the ways:
['bike', 'car', 'on foot', 'train']
Here are the ways:
['bike', 'on foot', 'train', 'car']
与字母顺序相反,向sorted()传递reverse=True
ways = ["bike","on foot","train","car"]
print("Here are the ways:")
print(ways)
print("nHere are the ways:")
print(sorted(ways,reverse=True))
print("nHere are the ways:")
print(ways)
Here are the ways:
['bike', 'on foot', 'train', 'car']
Here are the ways:
['train', 'on foot', 'car', 'bike']
Here are the ways:
['bike', 'on foot', 'train', 'car']
倒着打印列表
reverse()只是反转列表元素序列,永久改变了元素顺序,但可恢复,需对列表再次调⽤reverse()ways = ["bike","on foot","train","car"]
print(ways)
print(ways)
['bike', 'on foot', 'train', 'car']
['car', 'train', 'on foot', 'bike']
确定列表的长度
len()函数,列表的长度
ways = ["bike","on foot","train","car"]
print(len(ways))
4
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论