# Python 3 基础语法
● list 索引
○ -1代表最后一个
○ list可以放置各种各样的类型
# -*- coding: UTF-8 -*- names = [‘Mark‘, ‘coloe‘,1,2,3,4,5,3.14,True] print(type(names)) print(names) print(names[1]) print(names[0]) print(names[-1])
● list 嵌套使用
1 # -*- coding: UTF-8 -*- 2 names = [‘Mark‘, ‘coloe‘, [‘I‘, ‘Love‘, ‘PoEdu‘,‘!‘,],1,2,3,4,5,3.14,True, [‘I‘, ‘Love‘, ‘Mark‘,‘!‘,]] 3 print(type(names)) 4 print(names) 5 print(names[2]) #嵌套的list 6 print(names[-1]) #嵌套的list 7 print(names[-1][1], names[2][2]) #获取嵌套list 的值 并输出
● list append[]
末尾加入
1 # -*- coding: UTF-8 -*- 2 names = [‘Mark‘, ‘coloe‘, [‘I‘, ‘Love‘, ‘PoEdu‘,‘!‘,]] 3 print(names) 4 names.append(‘Google‘) 5 print(names) 6 names.append(‘Baidu‘) 7 print(names) 8 names.append(‘PoEdu‘) 9 print(names)
● list insert[]
指定位置插入
参数1:位置
参数2:插入的值
1 # -*- coding: UTF-8 -*- 2 names = [‘Mark‘, ‘coloe‘, [ ‘Love‘, ‘PoEdu‘,‘!‘,]] 3 print(names) 4 names.insert(1,‘胡萝卜‘) 5 print(names) 6 names.insert(4,‘So Much !‘) 7 print(names)
● list clear()
清空索引
1 # -*- coding: UTF-8 -*- 2 names = [‘Mark‘, ‘coloe‘, [ ‘Love‘, ‘PoEdu‘,‘!‘,]] 3 names.insert(1,‘胡萝卜‘) 4 names.insert(4,‘So Much !‘) 5 names.clear() 6 print(names)
● list copy()
复制索引
1 # -*- coding: UTF-8 -*- 2 names = [‘Mark‘, ‘coloe‘, [ ‘Love‘, ‘PoEdu‘,‘!‘,]] 3 other = names.copy() 4 print(names) 5 print(other)
● list pop()
删除索引末尾
参数1:删除指定位置的数据
感觉跟出栈一样
1 # -*- coding: UTF-8 -*- 2 names = [‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘,] 3 print(names) 4 names.pop() 5 print(names) 6 names.pop(0) 7 print(names) 8 names.pop(3) 9 print(names)
● Python 元组
Python的元组与列表类似,不同之处在于元组的元素不能修改。
元组使用小括号,列表使用方括号。
元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。
1 # -*- coding: UTF-8 -*- 2 names = (‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘) 3 print(type(names)) 4 print(names) 5 #元祖访问 6 print(names[0]) 7 print(names[1:5]) 8 print(names[6:]) 9 print(names[6:-1])
时间: 2024-10-10 21:04:38