python基础(变量,字符串,列表,元组)

#列表的操作
list1 = ['wuqiang','lichang','changhao'] #列表的定义
print(list1)

#操作列表
print(list1[-1]) #操作列表的最后一位
list1[0] = 'guozhuang' #修改列表元素
print(list1[0])
list1.append('mengshibin') #在列表的最后面追加元素
print(list1)
list1.insert(0,'helll') #在列表的任意位置追加元素
print(list1)


#使用语句删除列表元素,上面的是方法,都是根据索引来删除值的
del list1[0] #删除列表中知道索引的元素
print(list1)
#使用方法删除列表元素,这是方法
move = list1.pop(0) #删除列表中的元素,并拿来用,方法返回来是一个值,
print(move)

#根据值来删除列表中的元素
number = [1,2,3,4]
print(number.remove(1))
print(number)


#对列表元素排序
cars_color = ['red','whitle','black','blue','green']
print(cars_color.sort())#方法返回来是一个状态,
print(cars_color) #永久排序
print(cars_color.sort(reverse=True)) #反序排列

#使用函数sorted()临时排序 语句 函数 方法
print(sorted(cars_color,reverse=True))

print(cars_color.reverse()) #反转排列列表
cars_color.len() #确定列表长度




 

猜你喜欢

转载自www.cnblogs.com/wqs5/p/10291179.html
今日推荐