This article mainly introduces the use of while, if, and for statements in python. The editor thinks it is quite good. Now I will share it with you and make it for everyone. refer to. Let’s follow the editor and take a look.
1, if conditional statement
Basic form:
if Conditional judgment:
Execution statement
elif conditional judgment:
Execution statement
else:
Execution statement
Example:
<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>number = int(input("输入一个数字:"))<br>if number > 0: <br> print("正数")<br>elif number == 0: <br> print("零")<br>else: <br> print("负数")<br></span>
<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>输入一个数字:2<br>正数<br></span>
Analysis: As above, after running, the user enters a number 2 and starts to enter the if condition judgment, 2 > 0 is established , then enter the internal execution code block and output "positive number".
If the number entered by the user is -1, starts to enter the if condition judgment, -1 > 0 is not established, continue execution, "elif" is the condition judgment to continue execution , -1==0 is not established,
execute next, "else" is: if none of the above conditions are met, Then directly execute the else internal code block and print "negative number".
(1) while use
while Statements are used to execute programs in a loop, that is, under certain conditions, execute a certain program in a loop to handle the same tasks that need to be processed repeatedly. Its basic form is:
while Judgment condition:
循环语句...
执行语句可以是单个语句或语句块。判断条件可以是任何表达式,任何非零、或非空(null)的值均为true。
当判断条件假false时,循环结束。
例子:
<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>a = 1while a < 3: print(a)<br> a += 1<br></span>
<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>结果为:1<br> 2<br></span>
<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>解析:a 的值为1,进入while条件判断,a小于3,满足条件,则进入循环内部,<br></span>
<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>执行代码块:打印a的值“1”,然后使a+1。则此时的a为2,进入下次循环,<br></span>
<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>开始条件判断,2小于3,条件满足,进入循环内部执行代码块:打印a的值“2”,<br></span>
<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>然后a+1。a的值为3,进入下次循环。开始条件判断,此时a的值为3,条件为a<3,<br></span>
<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>可见不满足条件,所以结束循环。<br></span>
(2)continue,break 跳出循环
continue 用于跳过该次循环,break 则是用于退出循环,此外"判断条件"还可以是个常值,表示循环必定成立,具体用法如下:
<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>continue<br></span>
<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>a = 0<br>while a < 3:<br> a += 1 <br> if a == 2: <br> continue <br> print(a)<br></span>
<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>结果为:1<br> 3<br></span>
<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>循环过程:a的值为0,进行while条件判断 a<3 成立,执行代码块,a+1,此时a的<br></span>
<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>值为1,进入if条件判断,不满足条件,所以不进入if内部,继续往下执行,打印<br></span>
<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>a的值“1”。开始进入下次循环,a<3成立,进入循环,a+1,此时a的值为2,进入<br></span>
<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>if条件判断,a等于2,条件成立,进入if内部执行代码块,continue 用于跳过该<br></span>
<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>次循环,无论下面还有没有代码块都不会继续执行下去,直接将此次循环结束,<br></span>
<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>进入下次循环。开始条件判断,a<3,成立,进入循环,a+1,此时a的值为3,进入<br></span>
<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>if条件判断,不满足条件,所以不进入if内部,继续往下执行,打印a的值“3”。<br></span>
<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>开始进入下次循环,a<3不成立,结束循环。<br></span>
break a = 0 while a < 4: a += 1 if a == 2: break print(a)
结果为:1
将上面例子中的“continue”换为 “break”,循环是的流程还是一样的,只是在触发到“break”时不同,
break 为直接结束循环,所以运行的结果只有“1”被打印了。
1,使用方式
for循环可以遍历任何序列的项目,如一个列表或者一个字符串。跟while都属于循环,for循环是在序列穷尽时停止,
while循环是在条件不成立时停止。
语法格式:
for iterating_var in sequence: statements(s)
例子:遍历一个字符串的每个字符。
<span style="font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";">str1 = "Hello"<br/>for i in str1: <br/> print(i)<br/></span>
<span style="font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";">结果为:H<br/> e<br/> l<br/> l<br/> o<br/></span>
2,range()函数,生成一个整数数列,前开后闭,即后边的数字不能被打印。
如:
结果为:1 2
3,xrange() 用法与 range 完全相同,所不同的是生成的不是一个数组,而是一个生成器。
>>> list(xrange(1,5))
[1, 2, 3, 4]
4,enumerate()用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标。
如:
<span style="font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";">a = ["a","b","c"]<br/> for i in enumerate(a): <br/> print(i)<br/></span>
<span style="font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";">结果为:(0, 'a')<br/> (1, 'b')<br/> (2, 'c')<br/></span>
The above is the detailed content of How to use while, if, and for statements in python. For more information, please follow other related articles on the PHP Chinese website!