Python 入门简明教程
1. 1. 与 Python 的第一次亲密接触-写给初学者 2. 2. Python 语法关键字 3. 3. Python 的注释和语法 4. 4. Python 基本数据类型 5. 5. Python 输入输出 6. 6. Python 运算符 7. 7. Python 变量 8. 8. Python 命名空间和变量生存范围 9. 9. Python 逻辑判断 10. 10. Python For 循环语法 11. 11. Python While 循环语法 12. 12. Python 循环语法中 break 与 continue的使用 13. 14. Python 三目运算符 14. 15. Python 循环语法中 pass 语句的使用 15. 16. Python 循环语法示例 16. 17. Python 函数 17. 18. Python main 函数 18. 19. Python print 函数 19. 20. Python print format 输出格式化 20. 21. Python print 输出到文件 21. 22. Python 输入 22. 23. Python import 导入模块 23. 25. Python 中递归的使用 24. 26. Python 匿名函数 25. 27. Python lambda 26. 28. Python PIP 包管理机制 27. 29. Python 命令行参数 28. 30. Python Numbers 包 29. 31. Python 随机数 30. 32. Python String 转 int 31. 34. Python 自定义异常 Exception 32. 35. Python 继承 33. 36. Python 父类 34. 38. Python 操作符重载 35. 39. Python __str__ and __repr__

11. Python While 循环语法

在本教程中,我们将介绍python while循环的基础知识。在上一个教程中,我们了解到Python for循环.

Python while循环

Python while循环用于重复执行某些语句,直到条件为真。所以python while循环的基本结构是:


While condition :
	#Start of the statements
	Statement
	. . . . . . .
	Statement
	#End of the Statements
else :
	#this scope is optional
        #This statements will be executed if the condition
	#written to execute while loop is false

例如,下面的代码将为您提供有关while循环的一些想法。在这个例子中,我们在循环内部打印1到4个数字,在循环外部打印5个数字


cnt=1 #this is the initial variable
while cnt < 5 :
        #inside of while loop
        print (cnt,"This is inside of while loop")
        cnt+=1
else :
        #this statement will be printed if cnt is equals to 5
        print (cnt, "This is outside of while loop")

输出

在for循环教程的示例中,我们打印单词中的每个字母。我们可以实现代码使用while循环。下面的代码将向您展示这一点。


word="anaconda"
pos=0 #initial position is zero
while pos < len(word) :
	print (word[pos])
	#increment the position after printing the letter of that position
	pos+=1 

输出

关于循环的一个有趣的事实是,如果您使用for循环实现某些东西,您也可以在while循环中实现它。尝试在while循环中实现for循环教程中显示的示例。

Python嵌套while循环

可以在另一个while循环中编写while循环。假设您需要打印这样的图案

    112个1 2 3个1 2 3 41 2 3 4 5

下面的代码将说明如何使用嵌套while循环来实现它。


line=1 #this is the initial variable
while line <= 5 :
        pos = 1
        while pos < line:
 
                #This print will add space after printing the value
                print pos,
                
                #increment the value of pos by one
                pos += 1
        else:
                #This print will add newline after printing the value
                print pos
        
        #increment the value of line by one
        line += 1

Python while循环无限问题

因为while循环将继续运行,直到条件变为false,所以您应该确保它继续运行,否则程序将永远不会结束。有时,当你想让你的程序等待一些输入并持续检查时,它会很方便。


var = 100
while var == 100 :  # an infinite loop
   data = input("Enter something:")
   print ("You entered : ", data)

print ("Good Bye Friend!")

如果您运行上述程序,它将永远不会结束,您将不得不使用Ctrl+C键盘命令终止它。


>>> 
================= RESTART: /Users/pankaj/Desktop/infinite.py =================
Enter something:10
You entered :  10
Enter something:20
You entered :  20
Enter something:
Traceback (most recent call last):
  File "/Users/pankaj/Desktop/infinite.py", line 3, in <module>
    data = input("Enter something:")
KeyboardInterrupt
>>> 

以上是python while循环示例教程。如有任何疑问,请在下面发表意见。