新手到专家_1

思维导图:
在这里插入图片描述
在这里插入图片描述`四、列表 是由一系列特定顺序排列的元素组成。 在python中,使用【】表示列表

bicycles = [‘tek’,‘cannondale’,‘redine’,‘specialized’]
print(bicycles)
[‘tek’, ‘cannondale’, ‘redine’, ‘specialized’]
bicycles = [‘tek’,‘cannondale’,‘redine’,‘specialized’]
print(bicycles[1])
cannondale
bicycles = [‘tek’,‘cannondale’,‘redine’,‘specialized’]
message = f"My first bicycle is a {bicycles[0].title()}."
print(message)#在字符串中插入变量的值用f(format)
Input In [17]
print(message)/在字符串中插入变量的值用f(format)/
^
SyntaxError: invalid syntax

1.修改、添加和删除元素【创建的列表大多数是动态的,这意味着列表创建后,将随着程序的运行增删元素】

name = [‘lihua’,‘zhangsan’,‘lisi’,‘wangwu’]
print(name)
name[0] = ‘Luhan’
print(name)
[‘lihua’, ‘zhangsan’, ‘lisi’, ‘wangwu’]
[‘Luhan’, ‘zhangsan’, ‘lisi’, ‘wangwu’]
@添加元素使用append。方法append将原色添加至表的末尾,而不影响列表中的其他所有元素。

name = [‘lihua’,‘zhangsan’,‘lisi’,‘wangwu’]
name.append(‘dengwei’)
print(name)
[‘lihua’, ‘zhangsan’, ‘lisi’, ‘wangwu’, ‘dengwei’]
name=[]
name.append(‘lihua’)
name.append(‘dengwei’)
name.append(‘luhan’)
print(name)#因为经常要等程序运行后,才知道用户要在程序中存储哪些数据。为控制用户,可以创建一个空列表,用于存贮用户输入的值,然后将用户输入的值添加到列表中
[‘lihua’, ‘dengwei’, ‘luhan’]
@在列表中插入元素。insert()可以在列表的任何位置添加元素,只需指定新原色的索引和值。

name = [‘lihua’,‘zhangsan’,‘lisi’,‘wangwu’]
name.insert(0,‘liuxiaoqian’)
print(name)
[‘liuxiaoqian’, ‘lihua’, ‘zhangsan’, ‘lisi’, ‘wangwu’]
@使用del语句删除元素【当用户在你创建的web用户中注销账户时,需要将该用户从活动列表中删除】。

name = [‘lihua’,‘zhangsan’,‘lisi’,‘wangwu’]
del name[0]
print(name)
[‘zhangsan’, ‘lisi’, ‘wangwu’]
@使用pop()删除元素。【当用户在你创建的web用户中注销账户时,需要将该用户从活动列表中删除,加入到非活跃成员列表中】【删除栈尾的元素,弹出栈顶的元素】【假设是按照时间排序的,就可以使用该方法打印一条信息,指出最后一个人】

name = [‘lihua’,‘zhangsan’,‘lisi’,‘wangwu’]
name11 = name.pop()
print(name)
print(name11)
[‘lihua’, ‘zhangsan’, ‘lisi’]
wangwu
弹出列表中任意位置的元素,在pop()中加入需删除元素的索引

name = [‘lihua’,‘zhangsan’,‘lisi’,‘wangwu’]
name11 = name.pop(3)
print(name11)
wangwu
pop()和del的区别。如果从列表中删除一个元素,且不再以任何方式使用它,就是用del语句;如果你要在删除元素后还能继续使用它,就使用pop()方法

@根据值删除元素。不知道列表中删除值的所在位置,只知道要删除元素的值,可使用remove()方法

name = [‘lihua’,‘zhangsan’,‘lisi’,‘wangwu’]
name.remove(‘wangwu’)
print(name)
[‘lihua’, ‘zhangsan’, ‘lisi’]
name = [‘lihua’,‘zhangsan’,‘lisi’,‘wangwu’]
late_name = ‘wangwu’
name.remove(late_name)
print(f"{late_name.title()} is late for class")
print(name)
Wangwu is late for class
[‘lihua’, ‘zhangsan’, ‘lisi’]
注意:方法remove()只能删除第一个指定的值。如果要删除的值可能在列表中多次出现,就需要使用循环来确保每个值都删除。

2.组织列表。使用方法sort()永久排序,使用sorted()对列表临时排序

cars = [‘adu’,‘ccd’,‘dom’,‘acm’, ‘zam’]
cars.sort()
print(cars)
cars.sort(reverse = True)
print(cars)
[‘acm’, ‘adu’, ‘ccd’, ‘dom’, ‘zam’]
[‘zam’, ‘dom’, ‘ccd’, ‘adu’, ‘acm’]
cars = [‘adu’,‘ccd’,‘dom’,‘acm’, ‘zam’]
print(sorted(cars))
print(cars)
[‘acm’, ‘adu’, ‘ccd’, ‘dom’, ‘zam’]
[‘adu’, ‘ccd’, ‘dom’, ‘acm’, ‘zam’]
注意:在并非所有的值都是小写时,按照字母顺序排列列表要复杂一些。决定排列顺序时,有多种解读大写字母的方式,指定准确的排列顺序。

倒着打印列表。reverse()

cars = [‘adu’,‘ccd’,‘dom’,‘acm’, ‘zam’]
cars.reverse()
print(cars)
[‘zam’, ‘acm’, ‘dom’, ‘ccd’, ‘adu’]
确定列表的长度len()

cars = [‘adu’,‘ccd’,‘dom’,‘acm’, ‘zam’]
I = len(cars)#确定需要管理多少可视化数据,计算网站有多少注册用户等等
print(I)
5


五、操作列表

(一)列表

1.遍历整个列表 【让计算机自动完成重复工作的常见方式之一】 对每个元素执行相同的操作。如,在网站中,可能需要显示将文章列表中每个标题;对于包含数字列表,需要对每个元素进行统计运算等

#冒号告诉python,下一行是循环的第一行
names = [‘zhangsan’,‘lisi’,‘wangwu’,‘lili’]
for name in names:#冒号告诉python,下一行是循环的第一行
print(f"{name} is a great trick!")
print(names)#没有缩进
zhangsan is a great trick!
lisi is a great trick!
wangwu is a great trick!
lili is a great trick!
[‘zhangsan’, ‘lisi’, ‘wangwu’, ‘lili’]
使用for循环处理数据是对数据集执行整体操作的不错方式。例如,可能使用for来初始化游戏;遍历角色列表等。

小练习: 制作一个动物列表,再使用for循环将每种动物的名称打印出来 修改这个程序,使其针对每种动物都打印一个句子 在程序末尾添加一行代码,指出这些动物的共同之处

animinals = [‘cat’,‘dog’,‘tiger’,‘elephant’]
for animinal in animinals:
print(animinal)
print(f"{animinal} is very cute.")
print(“these animinals would make a great pet!”)
cat
cat is very cute.
dog
dog is very cute.
tiger
tiger is very cute.
elephant
elephant is very cute.
these animinals would make a great pet!
2.创建数值列表 在数据可视化中,处理的几乎是由数(如温度、距离、人口数量、精度、维度等)组成的集合 列表非常适合用于存储数字集合

使用函数range()生成一系列数

for vule in range(1,10):#在10停止(】从第一个值开始,并达到你指定到第二个值时停止
print(vule)
for num in range(4):##指定一个参数,从零开始
print(num)
1
2
3
4
5
6
7
8
9
0
1
2
3
使用range()创建数字列表,可使用List()将range()结果直接转换成为列表

for num in list(range(0,7)):
print(num)

list1 = list(range(6))
print(list1)
0
1
2
3
4
5
6
[0, 1, 2, 3, 4, 5]
指定步长,可以给这个函数指定第三个参数,python将根据这个步长来生成数。

值11
number = list(range(2,11,2))#终值11
print(number)
[2, 4, 6, 8, 10]
squares = []
for value in range(1,11):
square = value**2
squares.append(square)
print(squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
3.对数值列表进行简单的统计运算

digits = list(range(1,10))
print(min(digits))
print(max(digits))
print(sum(digits))

1
9
45
squares =[value**2 for value in range(0,11)]
print(squares)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
4.处理列表中的部分元素,python称之为切片 创建切片,可以指定使用的第一个元素和最后一个元素的索引。与range()函数一样,python在达到的第二个索引之前的元素后停止。

writers = [‘moyan’,‘yuhua’,‘shitiesheng’,‘binxin’,‘maodun’]
print(writers[0:3])
writers = [‘moyan’,‘yuhua’,‘shitiesheng’,‘binxin’,‘maodun’]
print(writers[0:3])
[‘moyan’, ‘yuhua’, ‘shitiesheng’]
writers = [‘moyan’,‘yuhua’,‘shitiesheng’,‘binxin’,‘maodun’]
print(writers[:3])#没有指定索引,python将自动从列表开头开始
writers = [‘moyan’,‘yuhua’,‘shitiesheng’,‘binxin’,‘maodun’]
print(writers[:3])#没有指定索引,python将自动从列表开头开始
[‘moyan’, ‘yuhua’, ‘shitiesheng’]
writers = [‘moyan’,‘yuhua’,‘shitiesheng’,‘binxin’,‘maodun’]
print(writers[1:])
writers = [‘moyan’,‘yuhua’,‘shitiesheng’,‘binxin’,‘maodun’]
print(writers[1:])
[‘yuhua’, ‘shitiesheng’, ‘binxin’, ‘maodun’]
可以在切片的方括号内指定第三个值。这个值告诉python在指定范围内每隔多少元素提取一个。

writers = [‘moyan’,‘yuhua’,‘shitiesheng’,‘binxin’,‘maodun’]
writers = [‘moyan’,‘yuhua’,‘shitiesheng’,‘binxin’,‘maodun’]
print(writers[0:5:2])
[‘moyan’, ‘shitiesheng’, ‘maodun’]
writers = [‘moyan’,‘yuhua’,‘shitiesheng’,‘binxin’,‘maodun’]
print(writers[-3:])
[‘shitiesheng’, ‘binxin’, ‘maodun’]
5.遍历切边 for

:
writers = [‘moyan’,‘yuhua’,‘shitiesheng’,‘binxin’,‘maodun’]
for writer in writers[:5]:
print(writer)
moyan
yuhua
shitiesheng
binxin
maodun
如,在编写游戏时,可以在玩家退出游戏时将最终得分加入一个列表中,将该列表按照降序排列获取三个最高得分。 在编写web应用程序时,可使用切片来分页显示信息,并在每页显示数量合适的信息。

6.复制列表

my_foods = [‘pizza’,‘falafel’,‘carrot cake’]
friend_foods = my_foods[:]#在不指定任何索引的情况下从列表my_foods中提取一个切片,从而创建这个列表的副本,并将该副本赋给变量friend_foods.
print(friend_foods)
my_foods = [‘pizza’,‘falafel’,‘carrot cake’]
friend_foods = my_foods[:]#在不指定任何索引的情况下从列表my_foods中提取一个切片,从而创建这个列表的副本,并将该副本赋给变量friend_foods.
print(friend_foods)
[‘pizza’, ‘falafel’, ‘carrot cake’]
my_foods.append(‘ice cream’)
friend_foods.append(‘cannoli’)
print(my_foods)
print(friend_foods)
my_foods = [‘pizza’,‘falafel’,‘carrot cake’]
friend_foods = my_foods[:]
my_foods.append(‘ice cream’)
friend_foods.append(‘cannoli’)
print(my_foods)
print(friend_foods)
[‘pizza’, ‘falafel’, ‘carrot cake’, ‘ice cream’]
[‘pizza’, ‘falafel’, ‘carrot cake’, ‘cannoli’]
my_foods = [‘pizza’,‘falafel’,‘carrot cake’]
friend_foods = my_foods
my_foods.append(‘ice cream’)
friend_foods.append(‘cannoli’)
print(my_foods)
print(friend_foods)
#python将新变量关联到已与my_foods相关联的列表
[‘pizza’, ‘falafel’, ‘carrot cake’, ‘ice cream’, ‘cannoli’]
[‘pizza’, ‘falafel’, ‘carrot cake’, ‘ice cream’, ‘cannoli’]
(二)元组 列表非常适合用于存储在程序运行期间可能变化的数据集。列表是可以修改的。如果需要存储的一组值在程序的整个生命周期内都不变,就可以使用元组。python将不能修改的值成为不可变的,而不可修改的列表被称之为元组

1.定义元组

dimensions = (20,50)
dimensions = (20,50)
#dimensions[0]= 30
print(dimensions[0])
print(dimensions[1])
20
50
注意:严格来说,元组是由逗号标识的,()只是让元组看起来更简洁、清晰。如果只创建一个元素的元组,必须在这个元素的后面加上,
dimensions = (20,)
print(dimensions[0])
20
2.遍历元组中的所有值 for

dimension = (range(0,11))
for num in dimension:
print(num)
0
1
2
3
4
5
6
7
8
9
10
3.修改元组的变量 可以给存储元组的变量重新赋值

i
dimensions = (20,50)
dimension = (20,500)
for i in dimension:
print(i)
dimension:
dimensions:


`

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨老师-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值