Home  >  Article  >  Backend Development  >  How to use while, if, and for statements in python

How to use while, if, and for statements in python

零到壹度
零到壹度Original
2018-04-04 14:09:023701browse


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".

2. while loop statement

(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 1ff9ac361d84316a6b8502bfdf67bb36>> list(xrange(1,5))<br>[1, 2, 3, 4]<br></span><p><span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>4,enumerate()用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标。</span></p><p><span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>如:</span></p><pre style="background-color:rgb(255,255,255);font-family:'宋体';font-size:15pt;" class="brush:php;toolbar:false;"><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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn