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__

12. Python 循环语法中 break 与 continue的使用

今天我们将学习Python break和continue语句。在上一篇文章中,我们了解到python while循环.

Python中断并继续

Python break和continue语句只在循环中使用。它有助于修改循环行为。假设您正在执行一个循环,在某个阶段需要终止循环或跳过一些语句,那么您需要这些语句。

Python中断

此语句用于中断循环。假设您使用while循环打印奇数。但你不需要打印大于10的数字。然后可以编写以下python代码。


number = 1 #Number is initially 1
 
while True : #This means the loop will continue infinite time
        print (number) #print the number
        number+=2 #calculate next odd number
 
        # Now give the breaking condition
        if number > 10:
                break;
                #Breaks the loop if number is greater than ten
                print (number) #This statement won"t be executed

Python中断示例输出

在给定的示例中,您将看到在break之后写入的语句将不会被执行。所以在这里,11不会被打印出来。

Python break语句也可以在for循环中使用。假设您正在打印列表中的单词。如果任何与“exit”匹配的单词都不会被打印出来,循环将终止。下面的python代码说明了这个想法。


words = ["rain", "sun", "moon", "exit", "weather"]
 
for word in words:
        #checking for the breaking condition
        if word == "exit" :
                #if the condition is true, then break the loop
                break;
 
        #Otherwise, print the word
        print (word)

例如Python break

Python继续

Python continue语句用于跳过循环语句并迭代到下一步。例如,您要打印1到10号。您需要跳过步骤7中的所有语句。下面的python代码演示了这个场景。


numbers = range(1,11)
"""
the range(a,b) function creates a list of number 1 to (b-1)
So, in this case it would generate
numbers from 1 to 10
"""
for number in numbers:
        #check the skipping condition
        if number == 7:
                #this statement will be executed
                print("7 is skipped")
                continue
                #this statement won"t be executed
                print ("This won"t be printed")
 
        #print the values
        #for example:
        #2 is double of 1
        print (number*2),
        print ("is double of"),
        print (number)

Python继续示例输出

你也可以在while循环中做同样的事情。假设您要打印从1到10的所有奇数值,那么解决问题的python代码是:


numbers = [ 1, 2, 4, 3, 6, 5, 7, 10, 9 ]
pos = 0 #initial position is one
while pos < len(numbers):
        #checking skipping condition if number is divisible by two, it is even
        if numbers[pos] % 2 == 0 :
                #increment the position by one
                pos = pos + 1
                continue
        #print the odd number
        print (numbers[pos])
        #increment the position by one
        pos = pos + 1

Python继续示例while循环

这就是python break and continue语句的全部内容。希望你学得好。若你们对这个话题有任何疑问,可以在评论栏里提出。 #快乐编码