提示 以下是本篇文章正文内容 下面案例可供参考 部分案例来源互联网 版权归原作者所有。
一、条件语句 1.if语法
if boolean_expression: statement(s)
如果boolean_expression 布尔表达式 返回True 则执行 if 的语句。否则 不执行语句 并且程序继续执行 if 语句之后的语句 如果有的话 。
例 1 条件表达式 布尔表达式 为Truea 2 if a b: print(a, 是小于 , b)例 2 条件表达式 布尔表达式 为False
a 2 if a b: print(a, 是小于 , b) # 2 是小于 5
if 语句中提供的条件计算结果为 false 因此不会执行 if 语句中的语句。
例 3 条件表达式 布尔表达式 有多个条件使用逻辑运算符 and、or、not 将多个条件连接起来 并创建复合条件。
a 2 if a b and a c: print(a, 是小于 , b, 和 , c) # 2 是小于5 和 4例 4 条件表达式 布尔表达式 计算结果为数字
如果 if 语句中的表达式计算结果为数字 则如果数字为非零 则执行语句。零被认为是假的 非零 正或负 被认为是真的。
a 2 if a: print(a, is not zero ) # 2 is not zero.例 5 if 内的多行代码
a 2 if a: print(a, is not zero ) print( And this is another statement ) print( Yet another statement )
输出
2 is not zero And this is another statement Yet another statement例 6 嵌套if
a 2 if a! 0: print(a, is not zero. ) if a 0: print(a, is positive. ) if a 0: print(a, is negative. )2.if else
语法
if boolean_expression: statement(s) else: statement(s)例 1 条件表达式为True
a 2 if a b: # 2 4返回True 因此执行 if 语句 print(a, is less than , b) else: print(a, is not less than , b)例 2 条件表达式为False
a 5 if a b: # 5 4返回False 因此执行 else 语句 print(a, is less than , b) else: print(a, is not less than , b)例 3 嵌套if-else
a 2 if a b: print(a, is less than , b) if c b: print(c, is less than , b) else: print(c, is not less than , b) else: print(a, is not less than , b)3.elif
语法
if boolean_expression_1: statement(s) elif boolean_expression_2: statement(s) elif boolean_expression_3: statement(s) statement(s)例 1 if-elif-else结构
a 2 if a b: print(a, is less than , b) elif a b: print(a, is greater than , b) else: print(a, equals , b)例 2 if-elif-elif-else 带有多个 elif 语句的应用
a 2 if a 0: print(a, is negative ) elif a 0: print( its a 0 ) elif a 0 and a 5: print(a, is in (0,5) ) else: print(a, equals or greater than 5 )4.if and 例 1 带有 and 运算符的 if 条件表达式
a 5 # 嵌套if if a 5: if b 0: print( a is 5 and ,b, is greater than zero. ) # 或者 将多个条件组合成一条表达式 if a 5 and b 0: print( a is 5 and ,b, is greater than zero. )例 2 带有 and 运算符的 if-else 条件表达式
a 3 if a 5 and b 0:# 条件成立 则执行这里 print( a is 5 and ,b, is greater than zero. ) else:# 条件不成立 则执行这里 print( a is not 5 or ,b, is not greater than zero. )例 3 带有 and 运算符的 elif 条件表达式
a 8 if a 0: print( a is less than zero. ) elif a 0 and a 8: print( a is in (0,8) ) elif a 7 and a 15: print( a is in (7,15) )5.if or 例 1 带 or 运算符的 if 条件表达式
today Saturday if today Sunday or today Saturday : print( Today is off. Rest at home. )例 2 带 or 运算符的 if-else 条件表达式
today Wednesday if today Sunday or today Saturday : print( Today is off. Rest at home. ) else: print( Go to work. )例 3 带 or 运算符的 elif 条件表达式
today Sunday if today Monday : print( Your weekend is over. Go to work. ) elif today Sunday or today Saturday : print( Today is off. ) else: print( Go to work. )6.if not
语法
if not value: statement(s)value为bool类型
如果value是bool类型 则 NOT 充当否定运算符。 如果 value 为 False 则 not value 将为 True 并且 if-block 中的语句将执行。 如果 value 为 True 则 not value 将为 False 并且 if-block 中的语句将不会执行。value为string类型
如果value是 string 类型 则 if-block 中的语句将在 string 为空时执行。value为list类型
如果value是 list 类型 则 if-block 中的语句将在 list 为空时执行。 同样的解释也适用于其他集合数据类型的值 dict、set 和 tuple。例 1 value值为 bool 型的if not应用
a False if not a: print( a is false. )例 2 value值为 string 型的if not应用
string_1 if not string_1: print( String is empty. ) else: print(string_1)例 3 value值为 list 型的if not应用
a [] if not a: print( List is empty. ) else: print(a)例 4 value值为 dict 型的if not应用
a dict({})
if not a:
print( Dictionary is empty. )
else:
print(a)
例 5 value值为 set 型的if not应用
a set({})
if not a:
print( Set is empty. )
else:
print(a)
例 6 value值为 tuple 型的if not应用
a tuple() if not a: print( Tuple is empty. ) else: print(a)7.三元(目)运算符
详细查看另一个遍文章 https://blog.csdn.net/weixin_40458518/article/details/120477844
二、循环语句常用于(range、list、tuple、dictionary、set、string)序列和集合的循环迭代。
1.for循环语法
for item in iterable: statement(s)例 1 迭代对象是 range(范围) 的 for循环
for i in range(25,29): print(i)例 2 迭代对象是 string(字符串) 的 for循环
mystring python.org for x in mystring: print(x)例 3 迭代对象 list(列表) 的 for循环
mylist [ python , programming , examples , programs ] for x in mylist: print(x)例 4 迭代对象是 tuple(元组) 的 for循环
mytuple ( python , programming , examples , programs ) for x in mytuple: print(x)例 5 迭代对象是 dict(字典) 的 for循环
mydictionary { name : python , category : programming , topic : examples }
for x in mydictionary:
print(x, : , mydictionary[x])
例 6 迭代对象是 set(集合) 的 for循环
myset { python , programming , examples }
for x in myset:
print(x)
2.while循环
语法
while condition: statement(s)例 1 while循环打印 1 到 n 范围内的所有数字
n 4 while i n: print(i)3.break 中止循环 彻底中止本层循环 ----------------------------- for-break ----------------------------- 例 1 在for循环中 break 彻底中止本层循环
for x in range(2, 10): if(x 7): break print(x)例 2 在for循环中 break 彻底中止本层循环 后面的else语句不会执行 是无效代码。
for x in range(1, 11) : if x 4: break # 彻底中止本层循环 后面else语句不执行 是无效代码。 print(x) else: print( else内的代码不执行/无效 )例 3 在for循环中 break彻底中止获取列表的元素
myList [1, 5, 4, 9, 7, 2] for x in myList : if x 9: break print(x)----------------------------- while-break ----------------------------- 例 1 在while循环中 break 彻底中止本层循环
a 4 while i a: print(i) if i 1: break例 2 在while循环中 break 彻底中止本层循环 后面的else语句不执行 是无效代码。
i 1 while i 10 : if i 4 : break # 循环彻底中断 后面else语句不执行 是无效代码。 print(i) else: print( else内的代码不执行/无效 )4.continue 跳过循环 跳过本层本次循环 ----------------------------- for-continue ----------------------------- 例 1 for循环中 跳过循环 跳过本层本次循环 继续执行下次循环
for x in range(2, 10): if(x 7): continue print(x)例 2 for循环中 跳过循环 跳过本层本次循环 继续执行下次循环
for x in range(1, 11) : if x%3 0 : continue # 不会影响 for 循环后面的 else 语句 print(x) else: print( 此行代码在for循环结束前执行 )----------------------------- while-continue ----------------------------- 例 1 while循环中 跳过循环 跳过本层本次循环 继续执行下次循环
a 4 while i a: if i 2: i 1 # while循环中 如果没有条件变量不变 则会死循环、程序一直不断跑 continue print(i)例 2 while循环中 跳过循环 跳过本层本次循环 继续执行下次循环
i 1 while i 10 : if i 4 or i 7 : i 1 # 不要忘记条件变量要变哦 continue print(i)5.exit() 结束程序
凡是循环语句内有 exit() 无论 exit() 后面的代码在循环内还是循环外都不执行 并且结束程序。
例 1 for-exit()for i in range(2,100,2): if i 20: exit() # 结束程序 不是中止循环或跳过循环 print(i) else: print( 用于测试该代码执行否 else 语句内 ) # 不执行 print( 用于测试该代码执行否 for-else 语句后面 ) # 不执行例 2 while-exit()
i 0 while i 5: if i 4: exit() # 结束程序 不是中止循环或跳过循环 print(i) else: print( Printing task is done. ) # 不执行 print( hello world! ) # 不执行6.else for循环或while循环之后加入else语句 表示退出前执行else代码 ----------------------------- for-else ----------------------------- 例 1 在 for 循环之后使用 else 语句
for x in range(2, 6): print(x) else: print( Out of for loop )例 2 在for循环中 break 彻底中止本层循环 else 语句无效、不执行
for x in range(1, 11) : if x 4: break # 彻底中止本层循环 后面else语句不执行 是无效代码。 print(x) else: print( else内的代码不执行/无效 )----------------------------- while-else -----------------------------
while-else语法
while condition: statement(s) else: statement(s)例 1 在 while 循环之后使用 else 语句
i 0 while i 5: print(i) else: print( Printing task is done. )例 2 在 while 循环之后使用 else 语句来关闭文件句柄
f open( sample.txt , r ) while True: line f.readline() if not line: break print(line.strip()) else: f.close7.嵌套循环 7.1 for 嵌套
for x in range(5): for y in range(6): print(x, end ) print()7.2 while 嵌套
flag True while flag: # 第一层(外层)while循环 username input( 请输入用户名: ) password input( 请输入用户密码: ) if username admin and password 123 : while flag: # 第二层(内层)while循环 command input( 输入要执行的linux代码命令 ) if command.lower() q : flag False # 更改变化 同时控制第一层(外层)和第二层(内层)的while循环 print( 正在执行命令 %s %command) else: print( 用户名或密码输入错误 )



