Python 循环与定义函数

时间:2023-11-26 13:09:32

break

for i in range(10):
if i == 2:
break
print i
0
1

continue

for i in range(10):
if i == 2:
continue
print i
0
1
3
4
5
6
7
8
9

函数

def square_sum(a,b):
c = a**2 + b**2
return c v = square_sum(2,3)
print v

计算a的平方加b的平方。

a = 1

def change_integer(a):
a = a + 1
return a print change_integer(a)
print a
2
1

段落之间留两行,显得结构清晰。

b = [1,2,3]

def change_list(b):
for i in range(len(b)):
b[i] = b[i] + 1
return b print change_list(b)
print b
[2, 3, 4]
[2, 3, 4]