码迷,mamicode.com
首页 > 编程语言 > 详细

Python条件控制与循环

时间:2019-12-23 00:18:10      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:while   循环语句   continue   aaa   class   sum   nis   控制   als   

条件控制语句:if

循环语句:while、for

其他语句:continue、break、pass

1、if语句

# ================================
a = 1
if a < 2:
    print(true)
else:
    print(false)
# ================================
if a:
    print(true)
else:
    print(false)
# ================================
if a == 0:
    print(a = 0)
elif a == 1:
    print(a = 1)
elif a == 2:
    print(a = 2)
else:
    print(a为其他值)

2、while语句

# ===================================
a = 1
sum = 0
while a <= 100:
    sum += sum
    a += 1
print(1到100的和为:, sum)
# ===================================
a = 0
while a < 5:
    print(a小于5)
    a += 1
else:
    print(a大于等于5)
# ===================================
a = 0
while a < 5:
    a += 1
    if a == 2:
        continue
    if a == 4:
        break
    print(a, end= )

3、for语句

# ==================================
lst = [aaa, bbb, ccc, ddd]
for s in lst:
    print(s, end= )
print()
# ==================================
lst = [aaa, bbb, ccc, ddd]
for s in lst:
    if s == aaa:
        continue
    if s == ccc:
        break
    print(s, end= )
else:
    print(finish)
print()

4、pass语句

a = 0
if a == 0:
    pass
elif a == 1:
    pass
elif a == 2:
    pass
else:
    pass

Python条件控制与循环

标签:while   循环语句   continue   aaa   class   sum   nis   控制   als   

原文地址:https://www.cnblogs.com/wbz-blogs/p/12081559.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!