a = ['a','b','c','d']
for an in a: #冒号必不可少
print(an)
a
b
c
d
a = ['a','b','c','d']
for an in a: #冒号必不可少
print(f"{an.title()} is uppercase of {an}")
print("next char is:\n")
A is uppercase of a
next char is:
B is uppercase of b
next char is:
C is uppercase of c
next char is:
D is uppercase of d
next char is:
结果说明: 缩进的都属于for循环 an相当于计数器变量作索引的a[i]。
a = ['a','b','c','d']
for an in a: #冒号必不可少
print(f"{an.title()} is uppercase of {an}")
print("test end.") #当缩进没有的时候,默认为在for循环之外。
A is uppercase of a
B is uppercase of b
C is uppercase of c
D is uppercase of d
test end.
a = ['a','b','c','d']
for an in a:
print(an)
File "/var/folders/1j/k13fttf13b5d6j8_m4q12rgr0000gn/T/ipykernel_49995/1502029580.py", line 3
print(an)
^
IndentationError: expected an indented block
当没有缩进时,for循环找不到循环体,会报错。
range()的三个用法:
range(x) 和 range(0,x)一样 range(x,y) range(x,y,z)
list(range(6)) #相当于默认从0开始数6个
[0, 1, 2, 3, 4, 5]
list(range(0,6)) #指定从0开始数6个
[0, 1, 2, 3, 4, 5]
list(range(3,12)) #相当于从3到12,步长为1,注意右括号12实际数到11
[3, 4, 5, 6, 7, 8, 9, 10, 11]
list(range(3,12,2))#步长为2
[3, 5, 7, 9, 11]
even_num = list(range(2,11,2)) #注意,左边从2开始,右边界限为11(左面取得到,右面够不到)
print(even_num)
[2, 4, 6, 8, 10]
square_list = [] #先创建一个空列表
for value in range(1,11):
square = value ** 2 #每一次线计算一下平方的值,再append到list里面
square_list.append(square)
#上面两行可以写成: spuare_list.append(value ** 2)
print(square_list)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
digits = [1,2,3,4,5,6,7,8,9,0]
print(max(digits))
print(min(digits))
print(sum(digits))
9
0
45
#上面输入平方数的例子还可以用两行代码解决:
square_list = [value ** 2 for value in range (1,11)]
#本质是把一个列表转换成另一个列表
#格式:列表1 = [表达式 for 变量 in 列表2]
print(square_list)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
digits = [1,2,3,4,5,6,7,8,9,0]
print(digits[0:3])
[1, 2, 3]
如果写成 列表[:终点],起点默认索引为0; 如果写成 列表[起点:],则默认重点到列表最后一个
digits = [1,2,3,4,5,6,7,8,9,0]
print(digits[-3:])
[8, 9, 0]
列表[-倒数几个:]
digits = [1,2,3,4,5,6,7,8,9,0]
print(digits[0:10:2])
[1, 3, 5, 7, 9]
[起点:重点:步长],步长不输入(没有第二个冒号)默认为1.
digits = [1,2,3,4,5,6,7,8,9,0]
print("No 5 to No 9 number in my digits are:\n")
for digit in digits[4:8]: #利用单复数区别 列表 和 an
print(digit)#注意这里打印的是变量
No 5 to No 9 number in my digits are:
5
6
7
8
digits = [1,2,3,4,5,6,7,8,9,0]
copy_digits = digits[:]
digits.append(10)
copy_digits.append(11)
print(digits)
print(copy_digits)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 11]
这样才是两个列表
如果没有切片直接复制
则两个列表指向同一个储存空间,本质上是一个列表
digits = [1,2,3,4,5,6,7,8,9,0]
copy_digits = digits
digits.append(10)
copy_digits.append(11)
print(digits)
print(copy_digits)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 10, 11]
元组名 = (元素1,元素2,…)
访问和列表相同
a = (30,40)
print(a[0])
print(a[1])
30
40
不能改变:指不能单独给其内元素赋值。(报错)
a = (30,40)
a[0] = 1
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/var/folders/1j/k13fttf13b5d6j8_m4q12rgr0000gn/T/ipykernel_53119/1136073212.py in <module>
1 a = (30,40)
----> 2 a[0] = 1
TypeError: 'tuple' object does not support item assignment
a = (30,40,50)
for an in a:
print(an)
30
40
50
a = (30,40)
print(a)
a = (40,50)
print(a)
(30, 40)
(40, 50)