# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# list是一种有序的集合,可以随时添加和删除其中的元素。
classmates = [
'Michael',
'Bob',
'Tracy']
print(classmates)
# ['Michael', 'Bob', 'Tracy']
print(len(classmates))
# 3
print(classmates[0])
# Michael
print(classmates[-1])
# 可以用-1做索引,直接获取最后一个元素:Tracy
# -2 -3 -4 以此类推取倒数第二 第三 第四个元素
# insert
classmates.insert(1,
'Jack')
# 下表是从0 开始
print(classmates)
# ['Michael', 'Jack', 'Bob', 'Tracy']
print(classmates.pop())
# pop删除末尾的元素
print(classmates)
# ['Michael', 'Jack', 'Bob']
print(classmates.pop(1))
# 删除指定位置的元素 Jack
# 替换
classmates[1] =
'Sarah'
print(classmates)
# ['Michael', 'Sarah']
# append 添加
classmates.append(
'Adam')
print(classmates)
# ['Michael', 'Sarah', 'Adam']
# 元素类型可以不同
L = [
'Apple', 123
, True]
# list 的元素也可以是list
s = [
'python',
'java', [
'asp',
'php'],
'scheme']
print(len(s))
# 4
print(s[2][1])
# php
# L =[] len=0
# ----------------------------------------------------------------------------------------------------------
# tuple一旦初始化就不能修改,没有增删改的api,其他和list差不多
t =
()
print(t)
t = (1)
# 1 所以这个变量不是tuple
print(t)
t = (1
,)
print(t)
# (1,) #Python在显示只有1个元素的tuple时,也会加一个逗号,,以免你误解成数学计算意义上的括号。
转载于:https://www.cnblogs.com/shaozhiqi/p/11543473.html
转载请注明原文地址: https://mac.8miu.com/read-73619.html