【python学习笔记】6.list和tuple的操作

1.建立一个list:classmate=[‘a’,‘b’,‘c’]

2.读取classmate,输出[‘a’,‘b’,‘c’]

3.输出某一个数classmate[1]

4.扩充list classmate.append(‘d’)

5.去掉一个数据classmate.pop(1)

6.插入一个数据classmate.insert(1,'d')

7.读长度len(classmate)

tuple一旦初始化后就不能修改了,没有append(),或者insert()的方法.

不可变的tuple有什么意义?因为tuple不可变,所以代码更安全。如果可能,能用tuple代替list就尽量用tuple。

tuple指向的list是可以改的。

有1个元素的tuple定义时必须加一个逗号,,来消除歧义

练习

请用索引取出下面list的指定元素:

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

L = [
    ['Apple', 'Google', 'Microsoft'],
    ['Java', 'Python', 'Ruby', 'PHP'],
    ['Adam', 'Bart', 'Lisa']
]

# 打印Apple:
print(L[0][0])
# 打印Python:
print(L[1][1])
# 打印Lisa:
print(L[2][2])

猜你喜欢

转载自blog.csdn.net/qq_37519186/article/details/73391940
今日推荐