List 用法


list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5]
list3 = ["a", "b", "c", "d"]

print("1.访问列表中的值")
print("list1[0]: ", list1[0])  # 输出  list1[0]:  physics
print("list2[1:5]: ", list2[1:5])   # 输出 list2[1:5]:  [2, 3, 4, 5]


print("2.更新列表")
list = []          # 空列表
list.append('Google')   # 使用 append() 添加元素
list.append('Runoob')
print(list)  # 输出 ['Google', 'Runoob']


print("3.删除列表元素")
print(list1)
del list1[2]
print("After deleting value at index 2 : ")
print(list1)   # 输出 ['physics', 'chemistry', 2000]


print("4.Python列表脚本操作符")
print(len(list3)) # 输出 4
print(list2+list3) # 输出 [1, 2, 3, 4, 5, 'a', 'b', 'c', 'd']
print(list2*3)   # 输出 [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
print(3 in list2)  # 输出 True
for x in list2: print (x)
"""
输出:
1
2
3
4
5
"""

print("5.Python列表截取")
L = ['Google', 'Runoob', 'Taobao']
print(L[2])   #输出 Taobao
print(L[-2])  #输出 Runoob
print(L[1:])  #输出 ['Runoob', 'Taobao']


print("6.方法")
# 对原列表进行排序
aList = ['123', 'Google', 'Runoob', 'Taobao', 'Facebook'];
aList.sort();
print("List : ")
print(aList)

"""
输出 
List : 
['123', 'Facebook', 'Google', 'Runoob', 'Taobao']

"""
# 在列表末尾添加新的对象
aList.append( 2009 );
print ("Updated List : ", aList)  #  输出 Updated List :  ['123', 'Facebook', 'Google', 'Runoob', 'Taobao', 2009]

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

悟道行僧

鼓励是我最大的动力!

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

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

打赏作者

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

抵扣说明:

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

余额充值