python编程基础2-条件语句与循环语句

1 条件语句

1.1 if 语句

if 2 > 1 and not 2 > 3:
    print('Correct Judgement!')

# Correct Judgement!

1.2 if - else 语句

if expression:
    expr_true_suite
else:
    expr_false_suite

1.3 if - elif - else 语句

temp = input('请输入成绩:')
source = int(temp)
if 100 >= source >= 90:
    print('A')
elif 90 > source >= 80:
    print('B')
elif 80 > source >= 60:
    print('C')
elif 60 > source >= 0:
    print('D')
else:
    print('输入错误!')

1.4 assert 关键词

assert这个关键词我们称之为“断言”,当这个关键词后边的条件为 False 时,程序自动崩溃并抛出AssertionError的异常。

my_list = ['lsgogroup']
my_list.pop(0)
assert len(my_list) > 0

# AssertionError

在进行单元测试时,可以用来在程序中置入检查点,只有条件为 True 才能让程序正常工作。

2 循环语句

2.1 while 循环

while 布尔表达式:
    代码块

2.2 while - else 循环

while 布尔表达式:
    代码块
else:
    代码块

当while循环正常执行完的情况下,执行else输出,如果while循环中执行了跳出循环的语句,比如 break,将不执行else代码块的内容。

2.3 for 循环

for 迭代变量 in 可迭代对象:
    代码块

2.4 for - else 循环

for 迭代变量 in 可迭代对象:
    代码块
else:
    代码块

当for循环正常执行完的情况下,执行else输出,如果for循环中执行了跳出循环的语句,比如 break,将不执行else代码块的内容,与while - else语句一样。

2.5 range() 函数

range([start,] stop[, step=1])

2.6 enumerate()函数

enumerate(sequence, [start=0])

例子

seasons = ['Spring', 'Summer', 'Fall', 'Winter']
lst = list(enumerate(seasons))
print(lst)
# [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
lst = list(enumerate(seasons, start=1))  # 下标从 1 开始
print(lst)
# [(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]

2.7 break语句与continue语句、pass语句

break语句可以跳出当前所在层的循环。

continue终止本轮循环并开始下一轮循环。

pass是空语句,不做任何操作,只起到占位的作用,其作用是为了保持程序结构的完整性。

2.8 推导式

2.8.1 列表推导式

[ expr for value in collection [if condition] ]

例子

x = [-4, -2, 0, 2, 4]
y = [a * 2 for a in x]
print(y)
# [-8, -4, 0, 4, 8]

2.8.2 元组推导式

( expr for value in collection [if condition] )

例子

a = (x for x in range(10))
print(a)

# <generator object <genexpr> at 0x0000025BE511CC48>

print(tuple(a))

# (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

2.8.3 字典推导式

{ key_expr: value_expr for value in collection [if condition] }

例子

b = {i: i % 2 == 0 for i in range(10) if i % 3 == 0}
print(b)
# {0: True, 3: False, 6: True, 9: False}

2.8.4 集合推导式

{ expr for value in collection [if condition] }

例子

c = {i for i in [1, 2, 3, 4, 5, 5, 6, 4, 3, 2, 1]}
print(c)
# {1, 2, 3, 4, 5, 6}

2.8.5 其他

d = 'i for i in "I Love Lsgogroup"'
print(d)
# i for i in "I Love Lsgogroup"

e = (i for i in range(10))
print(e)
# <generator object <genexpr> at 0x0000007A0B8D01B0>

print(next(e))  # 0
print(next(e))  # 1

for each in e:
    print(each, end=' ')

# 2 3 4 5 6 7 8 9

s = sum([i for i in range(101)])
print(s)  # 5050
s = sum((i for i in range(101)))
print(s)  # 5050

2.9 综合例子

passwdList = ['123', '345', '890']
valid = False
count = 3
while count > 0:
    password = input('enter password:')
    for item in passwdList:
        if password == item:
            valid = True
            break
            
    if not valid:
        print('invalid input')
        count -= 1
        continue
    else:
        break
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值