students = ['zhangsan','lisi','wangxiaohua','songjiang','linchong']
print(students)
#访问列表元素
print(students[0])
print(students[3])
print(students[-1].title())
输出如下:
2、修改、添加、删除元素
students = ['zhouyue','zhangsan','lisi','wangxiaohua','songjiang','linchong','zhangshun','hexin','Monika']
print(students)
#修改元素
students[0] = 'likui'
print("\nstudents: "+str(students))
#添加元素
names = []
names.append('aaa')
names.append('bbb') #末尾追加
names.insert(1,'ccc') #中间插入
print("names: "+ str(names)+"\n")
#删除元素
del students[0] #知道索引位置
print("after del students: "+ str(students))
students.pop() #删除末尾元素,类似d出栈顶元素
print("pop末尾元素: "+ str(students))
students.pop(0)
print("pop第一个元素: "+ str(students)+"\n")
msg = students.pop(2) #d出指定元素,该元素之后仍能使用,但del后不能再使用
print("My name is " + str(msg))
print("after pop students: "+ str(students))
# 根据值删除元素
students.remove('lisi')
print("after remove students: "+ str(students))
输出如下:
注:remove()方法只删除匹配值中的第一个,若列表中有多个值匹配,需要使用循环来判断是否删除了所有值。
3、其他
people1 = ['zhouyue','zhangsan','lisi']
people2 = ['songjinag','likui','wusong']
# 列表组合
people = people2 + people1
print(people)
# 计算列表长度
print(len(people))
输出如下:
4、数字列表
numbers1 = list(range(1,6))
print(numbers1)
numbers2 = list(range(1,10,2)) #指定步长为2
print(numbers2)
squares = []
numbers3 = list(range(1,10))
for value in numbers3:
squares.append(value**2)
print(squares)
print("min:"+str(min(squares))+"\n"
"max:"+str(max(squares))+"\n"
"sum:"+str(sum(squares))+"\n")
或
numbers1 = list(range(1,6))
print(numbers1)
numbers2 = list(range(1,10,2)) #指定步长为2
print(numbers2)
squares = [value**2 for value in range(1,10)]
print(squares)
print("min:"+str(min(squares))+"\n"
"max:"+str(max(squares))+"\n"
"sum:"+str(sum(squares))+"\n")
输出如下:
5、切片
players = ['zhangsan','lisi','wangwu','zhengliu','haoqi','zhengba']
# 指定起始、终止index
print(players[:])
print("切片,指定起始1、终止3:"+str(players[1:3]))
print("切片,未指定则从第0个index开始:"+str(players[:3]))
print("切片,终止于末尾:"+str(players[3:]))
print("切片,切末尾最后2位:"+str(players[-2:]))
#遍历切片
print("\n遍历切片:")
for player in players[:2]:
print(player.title())
输出如下:
6、练习题
1、宾客名单:创建一个列表,其中包含至少3个你想邀请的人,打印消息,邀请这些人来与你共进晚餐。
(使用了列表的遍历)
for i in ['zhouyue','zhangsan','lisi']:
print("Hello "+str(i.title())+" ,Would you like to dinner with me?")
或
people = ['zhouyue','zhangsan','lisi']
for name in people:
print("Hello "+str(name.title())+" ,Would you like to dinner with me?")
输出如下:
2、 宾客名单:从名单中选出你最喜欢的人,邀请她与你共进晚餐。
(使用了循环判断)
people = ['zhouyue','zhangsan','lisi']
for name in people:
if name == 'zhangsan':
print("Hello "+str(name.title())+" ,Would you like to dinner with me?")
或
people = ['zhouyue','zhangsan','lisi']
name = 'zhangsan'
if name in people:
print("Hello "+str(name.title())+" ,Would you like to dinner with me?")
输出如下:
3、宾客名单:创建一个列表,随机邀请1人来与你共进晚餐。
import random
people = ['zhouyue','zhangsan','lisi','songjinag','likui','wusong']
i = random.randint(1, len(people))
print(str(i)+": Hello "+str(people[i].title())+" ,Would you like to dinner with me?")
输出如下:
4、使用for循环打印1~20。
5、使用for循环打印1~20的奇数。
6、创建一个1~1000的列表,计算1~1000的总和sum,求该列表的max、min。
7、创建一个3~30的列表,打印出其中能被3整除的数。
8、创建一个1~10的立方的列表
# 打印3~30能被3整除的数
numbers1 = list(range(3,31))
for i in numbers1:
if i%3 == 0:
print(i)
# 1~10的立方列表
numbers2 = [value**3 for value in range(1,11)]
print("\n"+str(numbers2))
输出如下:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)