search
HomeBackend DevelopmentPython TutorialHow to use while, if, and for statements in python
How to use while, if, and for statements in pythonApr 04, 2018 pm 02:09 PM
pythonwhilestatement


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     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</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     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</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</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</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</span>
break
a = 0
while a < 4:
    a += 1   
     if a == 2:      
       break    
 print(a)


结果为:1

将上面例子中的“continue”换为 “break”,循环是的流程还是一样的,只是在触发到“break”时不同,

break 为直接结束循环,所以运行的结果只有“1”被打印了。

三,for 循环语句

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()函数,生成一个整数数列,前开后闭,即后边的数字不能被打印。

如:

   print

结果为: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!

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
详细讲解Python之Seaborn(数据可视化)详细讲解Python之Seaborn(数据可视化)Apr 21, 2022 pm 06:08 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

详细了解Python进程池与进程锁详细了解Python进程池与进程锁May 10, 2022 pm 06:11 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

Python自动化实践之筛选简历Python自动化实践之筛选简历Jun 07, 2022 pm 06:59 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

归纳总结Python标准库归纳总结Python标准库May 03, 2022 am 09:00 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于标准库总结的相关问题,下面一起来看一下,希望对大家有帮助。

Python数据类型详解之字符串、数字Python数据类型详解之字符串、数字Apr 27, 2022 pm 07:27 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

分享10款高效的VSCode插件,总有一款能够惊艳到你!!分享10款高效的VSCode插件,总有一款能够惊艳到你!!Mar 09, 2021 am 10:15 AM

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

详细介绍python的numpy模块详细介绍python的numpy模块May 19, 2022 am 11:43 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

python中文是什么意思python中文是什么意思Jun 24, 2019 pm 02:22 PM

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),