Code
= ["China",'USA',"UK","France","German","Japan"]
Country print(Country)
['China', 'USA', 'UK', 'France', 'German', 'Japan']
在Python中,用方括号[]
表示列表,用逗号分隔其中的元素。
= ["China",'USA',"UK","France","German","Japan"]
Country print(Country)
['China', 'USA', 'UK', 'France', 'German', 'Japan']
索引从0开始,而不是从1开始。
print(Country[0])
print(Country[4])
China
German
也可以从倒数第一个数向前访问
print(Country[-1])
print(Country[-2])
print(Country[-3])
Japan
German
France
使用列表中的值
= f"Hello,I'm from {Country[0]}!"
intro print(intro)
Hello,I'm from China!
= [1,2,3,4,5,6,7,8,9]
num print(num)
0] = 0
num[print(num)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 2, 3, 4, 5, 6, 7, 8, 9]
在列表末尾添加元素
追加(append()
)到列表末尾
= [1,2,3,4,5,6,7,8,9]
num print(num)
10)
num.append(print(num)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
可以先创建空列表,再用append
逐个添加元素。
= []
fruit "apple")
fruit.append("pear")
fruit.append(
print(fruit)
['apple', 'pear']
在列表中插入元素
使用insert()
方法可在列表的任意位置添加元素
= [1,2,3,4,5,6,7,8,9]
num print(num)
0,0)
num.insert( num
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
使用del 语句删除元素
= [1,2,3,4,5,6,7,8,9]
num
del num[0]
del num[2]
num
[2, 3, 5, 6, 7, 8, 9]
使用pop()方法删除元素
弹出pop()
方法删除列表末尾的元素
= [1,2,3,4,5,6,7,8,9]
num = num.pop()
popped_num
num popped_num
9
删除任意位置的元素
也可以使用pop()
方法删除列表中任意位置的元素,指定索引即可
= [1,2,3,4,5,6,7,8,9]
num = num.pop(4)
fifth_num num
[1, 2, 3, 4, 6, 7, 8, 9]
根据值删除元素
= [1,2,3,4,5,6,7,8,9]
num = 4
hate_num
num.remove(hate_num) num
[1, 2, 3, 5, 6, 7, 8, 9]
使用sort()
方法永久排序
### 字母顺序
= ["a","r","k","n","v","z"]
letters
letters.sort()print(letters)
### 字母逆序
= ["a","r","k","n","v","z"]
letters = True)
letters.sort(reverse print(letters)
['a', 'k', 'n', 'r', 'v', 'z']
['z', 'v', 'r', 'n', 'k', 'a']
使用sorted()
函数临时排序
= ["a","r","k","n","v","z"]
letters sorted(letters)
sorted(letters,reverse = True)
print(letters)
['a', 'r', 'k', 'n', 'v', 'z']
使用reverse()
方法反向打印列表
= ["a","r","k","n","v","z"]
letters
letters.reverse()print(letters) #反向打印
letters.reverse()print(letters) #恢复
['z', 'v', 'n', 'k', 'r', 'a']
['a', 'r', 'k', 'n', 'v', 'z']
使用len()
函数确定列表长度
= ["a","r","k","n","v","z"]
letters len(letters)
6
for
循环
注意避免缩进错误,如忘记缩进、忘记缩进额外的代码行、不必要的缩进、循环后不必要的缩进、遗漏冒号、
= ["Chinese","Mathematics","English","Physics","Chemistry","Biology"]
subjects for subject in subjects:
print(f"{subject}:")
print(f"{subject} is very important for students.\n")
print("day day up!")
Chinese:
Chinese is very important for students.
Mathematics:
Mathematics is very important for students.
English:
English is very important for students.
Physics:
Physics is very important for students.
Chemistry:
Chemistry is very important for students.
Biology:
Biology is very important for students.
day day up!
range()
函数创建列表for value in range(-3,3):
print(value)
-3
-2
-1
0
1
2
= list(range(-5,5,2))
numbers
numbers
= []
squares for value in range(1,11):
=value**2
square
squares.append(square)print(squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
= list(range(0,10))
digits min(digits)
max(digits)
sum(digits)
45
= [i**2 for i in range(1,10)]
squares squares
[1, 4, 9, 16, 25, 36, 49, 64, 81]
列表的部分称为slice。
= ["a","r","k","n","v","z"]
letters # 左包含,右不包含
0:3]
letters[3:5]
letters[3] # 0,1,2
letters[:-2] # 从头到倒数第2之前1位
letters[:2:] # 从第3到末尾
letters[-3:] # 从倒数第3到末尾
letters[
2] # 从头到尾每间隔2打印
letters[::1::3]
letters[
# 遍历slice
for i in letters[:3]:
i.upper()
# 复制slice
= letters[:] # 从头到尾
字母 "X")
字母.append(
字母"Y")
letters.append(
letters
#不使用slice
= ["a","r","k","n","v","z"]
letters
letters= letters # 两种指向同一个列表
LETTERS "X")
LETTERS.append(
LETTERS
letters"Y")
letters.append(
LETTERS letters
['a', 'r', 'k', 'n', 'v', 'z', 'X', 'Y']
Python将不能修改的值称为不可变的。而不可变的列表称为元组(tuple)。元组使用圆括号()
+逗号,
标识。严格地说,元组是用逗号标识的,圆括号只是让元组看起来更整洁、更清晰,并不是必须的。
= (200,50,4)
dimensions
dimensions# dimensions[0] = 333 # 'tuple' object does not support item assignment
= 9,
single # single[0] = 3
虽然元组的元素不能修改,但可以重新给表示元组的变量赋值
= (200,50,4)
dimensions
dimensions= 2,3,4,5
dimensions
dimensionsfor i in dimensions:
i