-
python3中整除用//,非整初用/
-
python的数据类型有str、number、列表、字典、元组
-
列表是有序的集合,其中的元素可以是任何数据类型。
bicycles = [‘trek’, ‘cannondale’, ‘redline’, ‘specialized’]
print(bicycles[1])
4列表中值的修改:
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles[0] = 'ducati'
print(motorcycles).
5.在列表中添加元素:
5.1 在列表末尾添加元素:motorcycles.append(‘ducati’)
5.2 在列表中插入元素:motorcycles.insert(0, ‘ducati’)这种操作将列表中既有的每个元素都右移一个位置
6.从列表中删除元素:
6.1 使用del语句删除元素,如果知道要删除的元素在列表中的位置,可使用del语句del motorcycles[0]
6.2 使用方法pop()删除元素,方法pop()可删除列表末尾的元素,并让你能够接着使用它.
motorcycles = [‘honda’, ‘yamaha’, ‘suzuki’]
test= motorcycles.pop()
print(motorcycles)
print(test)
6.3 弹出列表中任何位置处的元素,可以使用pop()来删除列表中任何位置的元素,只需在括号中指定要删除的元素
的索引即可。
motorcycles = ['honda', 'yamaha', 'suzuki']
first_owned = motorcycles.pop(0)
print('The first motorcycle I owned was a ' + first_owned.title() + '.')
6.4 根据值删除元素:你不知道要从列表中删除的值所处的位置。如果你只知道要删除的元素的值,可使
用方法remove()。
motorcycles.remove('ducati')
print(motorcycles)
方法remove()只删除第一个指定的值。如果要删除的值可能在列表中出现多次,就需要
使用循环来判断是否删除了所有这样的值。
- 屏蔽单句时使用# 注释此语句,屏蔽多句时,可以使用三引号把多行语句引起来。
如下所示:
print(‘这里是屏蔽单句’)
‘’’
for i in range(1, 10):
print(‘这里是多句’,i)
‘’’
说明:
python使用#注释语句。
python使用三引号引起来的字符串保持原始样式。