python:第三十章:pass语句

发布时间 2023-11-20 08:08:48作者: 刘宏缔的架构森林

一,pass语句:

1,用途:

pass语句是一种空操作语句,它不执行任何操作。

2,应用场景:

它作为一个占位符,

应用于当某个代码块暂时没有实现,

以及在语法上需要一个语句但不需要执行任何操作的情况

说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/11/17/python-di-san-shi-zhang-pass-yu-ju/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 在if语句中使用pass
x = 8
if x > 5:
    pass  # do nothing or todo
 
# 在函数中使用pass
def myFunction():
    pass  # do nothing or todo
 
# 类中使用pass
class MyClass:
    pass  # do nothing or todo
 
# 循环中使用pass
# 打印从0到19,中间跳过3的倍数
for i in range(20):
    if (i % 3 == 0):
        pass  # do nothing or todo
    else:
        print(i, end=" ")

运行结果:

1 2 4 5 7 8 10 11 13 14 16 17 19