写在前面
本系列教程针对有一定编程经验的伙伴快速入门python基础,一些涉及开发的常识问题,本文并不涉及。
if else
is_hot= False
is_cold= True
if is_hot : (可以使用 and or 连接多个条件 not 可取反)
print("It's a hot day")
elif is_cold:
print("It's a cold day")
else:
print("It's a cold day")
print("Enjoy your day")
=>
It's a cold day
Enjoy your day
while
i=1
while i<=5:
print(i)
i +=1
else:
print(2) 跳出后执行一次
=> 1 2 3 4 5 2
for
for item in 'python': print(item)
=>
p
y
t
h
o
n
for item in ['Mosh','John','Sarah']:
print(item)
=>
Mosh
John
Sarah
for item in range(10):
print(item)
=> 0 1 2 3 4 5 6 7 8 9
for item in range(5,10):
print(item) =>5~9
for item in range(5,10,2): 步长
print(item) => 5,7,9
循环嵌套
for x in range(4):
for y in range(3):
print(f'({x},{y})')
=>
(0,0)
(0,1)
(0,2)
(1,0)
(1,1)
(1,2)
(2,0)
(2,1)
(2,2)
(3,0)
(3,1)
(3,2)
数组
matrix=[ [1,2,3], [4,5,6], [7,8,9] ] print(matrix[0]) print(matrix[1][1]) print(matrix[2][2]) => [1, 2, 3] 5 9