逻辑判断与逻辑语句
- 对于─件事情正确与否(真假的判断) √ x
- 根据判断的结果做不同的事情,就是我们的逻辑业务
- 对于条件满足的判断语句,就是条件语句
- 一个逻辑语句是由条件语句和业务语句组合而成
if语句功能
判断一个命题的真实性,如果命题为真( true )则执行if的逻辑语句
if语句用法
用法:
1
2
|
if bool_result: # 语法块 do # 业务代码块注意缩进 |
参数︰
-
bool_result
: 判断结果的真假,布尔类型 -
do
: 如果bool_result为true时执行任意python代码
返回值: if属于关键字,没有返回值
1
2
3
4
5
|
>>> insane_status = 'hunger >>> if insane_status = = 'hunger' : print ( 'xiaomu invites insane to dinner' ) xiaomu invites insane to dinner |
实战
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# coding:utf-8 info = 'my name is insane' info_list = info.split() print (info_list) if info_list[ 0 ] = = 'insane' : print ( 1 ) info_list[ 0 ] = 'loafer' if info_list[ 1 ] = = 'insane' : print ( 2 ) info_list[ 1 ] = 'loafer' if info_list[ 2 ] = = 'insane' : print ( 3 ) info_list[ 2 ] = 'loafer' if info_list[ - 1 ] = = 'insane' : print ( 4 ) info_list[ - 1 ] = 'loafer' print (info_list) info = ' ' .join(info_list) print (info) info = 'my name is insane, i am a pythoner, i love python' info_list = info.split() if info_list[ 0 ] in [ 'python' , 'i' ]: info_list[ 0 ] = '*' if info_list[ 1 ] = = 'python' or info_list[ 1 ] = = 'i' : info_list[ 1 ] = '*' if info_list[ 2 ] = = 'python' or info_list[ 2 ] = = 'i' : info_list[ 2 ] = '*' if info_list[ 3 ] = = 'python' or info_list[ 3 ] = = 'i' : info_list[ 3 ] = '*' if info_list[ 4 ] = = 'python' or info_list[ 4 ] = = 'i' : info_list[ 4 ] = '*' if info_list[ 5 ] = = 'python' or info_list[ 5 ] = = 'i' : info_list[ 5 ] = '*' if info_list[ 6 ] = = 'python' or info_list[ 6 ] = = 'i' : info_list[ 6 ] = '*' if info_list[ 7 ] = = 'python' or info_list[ 7 ] = = 'i' : info_list[ 7 ] = '*' if info_list[ 8 ] = = 'python' or info_list[ 8 ] = = 'i' : info_list[ 8 ] = '*' if info_list[ 9 ] = = 'python' or info_list[ 9 ] = = 'i' : info_list[ 9 ] = '*' if info_list[ - 1 ] in [ 'python' , 'i' ]: info_list[ - 1 ] = '*' print (info_list) info = ' ' .join(info_list) print (info) info = 'my name is insane' print ( len (info)) if len (info) > 10 and len (info) ! = 15 : print (info.replace( 'insane' , 'loafer' )) if len (info) > 10 and len (info) ! = 17 : print (info.replace( 'name' , 'iop' )) print ( 'finish' ) |
1
2
3
4
5
6
7
8
9
10
11
|
[ 'my' , 'name' , 'is' , 'insane' ] 4 [ 'my' , 'name' , 'is' , 'loafer' ] my name is loafer [ 'my' , 'name' , 'is' , 'insane,' , '*' , 'am' , 'a' , 'pythoner,' , '*' , 'love' , '*' ] my name is insane, * am a pythoner, * love * 17 my name is loafer finish process finished with exit code 0 |
到此这篇关于python初识逻辑与if语句的文章就介绍到这了,更多相关python逻辑if语句内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/m0_48978908/article/details/119428181