python list AND tuple练习

list列表元素可添加修改删除

list1=[1,'a','hh']

list1.insert(0,'cccc')#在list1 0位置插入‘cccc’  

print(list1)  #['cccc',1,'a','hh']插入

list1.pop(1)

print(list1)#['cccc','a','hh']删除

list1[0]='www'

print(list1)#['www','a','hh'] 修改

tuple元组一旦初始化元素不可修改

一个元素时元素后必须加逗号

tuple1=(1,)

取值方法和list几乎一致

tuple1=('a','b',[1,2])

tuple1[2][1]='x'

print(tuple1)#('a','b',[1,'x'])

嵌套list

#coding:utf-8
#buergege
# -*- coding: utf-8 -*-

L = [
['Apple', 'Google', 'Microsoft'],
['Java', 'Python', 'Ruby', 'PHP'],
['Adam', 'Bart', 'Lisa']
]
# 打印Apple:print(L[0][0])
print(L[0][0])
# 打印Python:
print(L[1][1])
# 打印Lisa:
print(L[2][2])

猜你喜欢

转载自www.cnblogs.com/buergege520/p/9332979.html
今日推荐