zl程序教程

您现在的位置是:首页 >  后端

当前栏目

学习Python(二):基本语句

Python学习 语句 基本
2023-09-14 09:16:19 时间

1,条件语句

在进行逻辑判断时,需要用条件语句,Python提供了if、elif、else来进行逻辑判断。

2,for循环

for 循环可以遍历任何序列,比如:字符串。

a = 'Python'
for s in a:
    print(s)

输出结果:

P
y
t
h
o
n

3,while循环

while循环,满足条件时进行循环,不满足条件时退出循环。

sum = 0
a = 10
while a > 0:
    sum = sum + a
    m = m-1
print(sum)

输出结果:

55

4,break返回

break用在for和while循环语句中,用来终止整个循环。

m = 'Python'
for s in m:
    if s == '0':
        break
    print(s)

输出结果:

P
y
t
h

5,continue

continue用在for循环和while循环语句中,用来终止本次循环。

m = 'Python'
for s in m:
    if s == '0':
        continue
    print(s)

输出结果:

P
y
t
h
n

6,pass语句

pass是空语句,它不做任何事情,一般用做占位语句,作用是保持程序结构的完整性。

if True:
    pass