python基础-列表

最基础的才是最重要的!

列表是python的一种 基本结构类型之一,类似于js的数组。

 

列表的切片操作,列表灵活操作之一:

 

#coding=utf-8

hello = ["ada","alex","shone","alex","antonio","bbb","ccc"]


print(hello)
# 倒着切片
print(hello[-1]) #ccc
print(hello[-2]) #bbb

# 正规切片,前后都有数字的时候左取右不取
print(hello[2:5]) #['shone', 'alex', 'antonio']

# 从开头切到第三个
print(hello[:3]) #['ada', 'alex', 'shone']
# 从第三个切到最后
print(hello[3:]) #['alex', 'antonio', 'bbb', 'ccc']

# 相当于直接输出
print(hello[:])
print(hello[::])

# 后面是步长
print(hello[::2])
# 返回第一次找到的下标
print(hello.index("alex")) #1

# 统计重复
print(hello.count("alex")) #2

  

列表的增删改查

 

# 列表的增删改查

# 增加元素
hello.append("append")
print(hello)
# ['ada', 'alex', 'shone', 'alex', 'antonio', 'bbb', 'ccc', 'append']

# 插入元素
hello.insert(3,"insert")
print(hello)
# ['ada', 'alex', 'shone', 'insert', 'alex', 'antonio', 'bbb', 'ccc', 'append']

# 删除元素
remove = hello.remove("ccc")
print("remove删除ccc",remove)
# remove删除ccc None,注意返回none

pop = hello.pop(5)
print(pop)
#antonio 注意被去掉的元素可以这样返回

del hello[1]
print(hello)
#['ada', 'shone', 'insert', 'alex', 'bbb', 'append'] 直接删除掉了



# ha=input("please input:")
#
# print(type(ha))

#
# number  = input("Input a number:")
# if   type(number) is int:
#     print("is int")
# else:
#     print("not int")

  

 

posted @ 2017-09-06 22:03  Adamanter  阅读(97)  评论(0编辑  收藏  举报