Python基础知识:3、操作列表

本文介绍了Python编程中常见的遍历和循环结构,如for循环和range()函数,展示了如何创建和操作列表,包括列表的切片、遍历、复制以及简单统计计算。此外,还涉及了元组的基础操作,如不可变性及遍历。
摘要由CSDN通过智能技术生成

遍历、循环。

#for循环
magicians=["alice","david","carolina"]
for magician in magicians:
    print(magician)#这句循环#
print("They are so cute !")#这句不循环#
#414
pizza=["aa","bb","cc"]
for pizza_n in pizza:
    print("I"+" "+"love"+" "+pizza_n.title())
print("I love pizza very much !")
I love Aa
I love Bb
I love Cc
I love pizza very much !
name=["cat","dog","fish"]
for name_o in name:
    print("A"+" "+name_o+" "+"would make a great pet .")
print("So cute !")
A cat would make a great pet .
A dog would make a great pet .
A fish would make a great pet .
So cute !
#函数range() 差一行为 
for value in range(1,9):
    print(value)
1
2
3
4
5
6
7
8 
#使用list()创建列表
#list将数字转换为列表
numbers=list(range(1,8))
print(numbers)
#range可指定步长
even_number=list(range(2,18,2))
print(even_number)
#如何创建一个列表,其中包含前十个整数的平方?
squares=[]
for value in range(1,11):
    square=value**2
    squares.append(square)   
print(squares)
#对数字列表进行简单的统计计算
digits=[1,2,3,4,5,6,7,8,9,0]
min(digits)
max(digits)
sum(digits)
[1, 2, 3, 4, 5, 6, 7]
[2, 4, 6, 8, 10, 12, 14, 16]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
45
#列表解析:列表名+左括号+用于生成值的表达式+给表达式提供值+右括号
squares=[value**2 for value in range(1,11)]
print(squares)
#动手414
#4-3
for i in range(1,21):
    print(i)
#4-4
number=[i*1 for i in range(1,10)]
print(number)
sum(number)
#4-6
number=[i*1 for i in range(1,20,2)]
print(number)
for i in range(1,20,2):
    print(i)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
1
3
5
7
9
11
13
15
17
19
#4-7 
number=[i*1 for i in range(3,31,3)]
print(number)
#4-8
number=[i**3 for i in range(1,11)]
print(number)
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30]
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
#切片:列表的部分元素#
lover=["zzx","yhx","wy","wjk"]
print(lover[0:3])
print(lover[:3])#自动从头开始#
print(lover[1:])#自动到末尾结束#
print(lover[-2:])#输出后几位
['zzx', 'yhx', 'wy']
['zzx', 'yhx', 'wy']
['yhx', 'wy', 'wjk']
['wy', 'wjk']
#遍历切片#
for i in lover[0:2]:
    print(i)
zzx
yhx
#复制列表#
mylover=["wjk","wy","yhx","zzx","ctr"]
herlover=mylover[:]
print("My lover:")
print(mylover)
print("\nHer lover:")
print(herlover)
My lover:
['wjk', 'wy', 'yhx', 'zzx', 'ctr']

Her lover:
['wjk', 'wy', 'yhx', 'zzx', 'ctr']
#动手414#
#4-10
yhx=["shi","qi","jin","nian","lu","feng","mang"]
print("wo hui ba ku nan dang zuo le qu")
print(yhx[0:3])
print("ji ran xuan ze zhe tiao lu")
print(yhx[2:5])
print("wo hui yi zhi zou xia qu")
print(yhx[-3:])
wo hui ba ku nan dang zuo le qu
['shi', 'qi', 'jin']
ji ran xuan ze zhe tiao lu
['jin', 'nian', 'lu']
wo hui yi zhi zou xia qu
['lu', 'feng', 'mang']
#4-11
pizza=["aa","bb","cc","dd"]
friend_pizza=pizza[:]
pizza.append("ll")
friend_pizza.append("oo")
print("My favoriate pizza are:")
for i in pizza:
    print(i)
print("My friend's favoriate pizza are:")
for o in friend_pizza:
    print(o)
My favoriate pizza are:
aa
bb
cc
dd
ll
My friend's favoriate pizza are:
aa
bb
cc
dd
oo
#元组():不可变的列表  元组中的元素不可被修改
dimen=(200,50)
print(dimen[0])
print(dimen[1])
#遍历
for i in dimen:
    print(i)
#给元组赋值
dimen=(600,900)
print(dimen[0])
200
50
200
50
600


#设置代码格式
略 书page 61

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值