我容易忘的python知识点:控制结构(含random库)

目录

分支结构

循环结构

while(无限循环):

for(遍历循环):

类型:

break&continue: 

random库

  PPT中链接为参考文献,黑色实例为jupyter notebook运行截图

分支结构

用于返回特定值,使用较少

循环结构

while(无限循环):

# Exception Handling
# asks the user for input until a valid integer has been entered, 
# but allows the user to interrupt the program 
# (using Control-C or I for jupyter notebook or whatever the operating system supports)
# note that a user-generated interruption is signaled by raising the KeyboardInterrupt exception.

while True:
    try:
        x = int(input("Please enter a integer: "))
        break
    except ValueError:
        print("Oops! Invalid literal for int(). Try again...")

for(遍历循环):

类型:

# 循环N次
for i in range(N):
    <Blocks>

# 遍历文件file的每一行
for line in file:
    <Blocks>

# 遍历字符串string每个字符
for c in string:
    <Blocks>

# 遍历列表list每个元素
for item in lst:
    <Blocks>

 range(N, M, K) : 起点(可忽略):终点(不含):步长(可忽略)

break&continue: 

break退出内层循环,“打破”循环结构

 continue结束当前循环,“继续”下一次循环

random库

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值