Python continue, else and pass
来源:互联网 发布:逍遥模拟器清理数据 编辑:程序博客网 时间:2024/05/21 12:05
The continue Statement:
The continue statement in Python returns the control to the beginning of the while loop. The continuestatement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop.
The continue statement can be used in both while and for loops.
Example:
#!/usr/bin/pythonfor letter in 'Python': # First Example if letter == 'h': continue print 'Current Letter :', lettervar = 10 # Second Examplewhile var > 0: var = var -1 if var == 5: continue print 'Current variable value :', varprint "Good bye!"
This will produce following result:
Current Letter : PCurrent Letter : yCurrent Letter : tCurrent Letter : oCurrent Letter : nCurrent variable value : 10Current variable value : 9Current variable value : 8Current variable value : 7Current variable value : 6Current variable value : 4Current variable value : 3Current variable value : 2Current variable value : 1Good bye!
The else Statement Used with Loops
Python supports to have an else statement associated with a loop statements.
If the else statement is used with a for loop, the else statement is executed when the loop has exhausted iterating the list.
If the else statement is used with a while loop, the else statement is executed when the condition becomes false.
Example:
The following example illustrates the combination of an else statement with a for statement that searches for prime numbers from 10 through 20.
#!/usr/bin/pythonfor num in range(10,20): #to iterate between 10 to 20 for i in range(2,num): #to iterate on the factors of the number if num%i == 0: #to determine the first factor j=num/i #to calculate the second factor print '%d equals %d * %d' % (num,i,j) break #to move to the next number, the #first FOR else: # else part of the loop print num, 'is a prime number'
This will produce following result:
10 equals 2 * 511 is a prime number12 equals 2 * 613 is a prime number14 equals 2 * 715 equals 3 * 516 equals 2 * 817 is a prime number18 equals 2 * 919 is a prime number
Similar way you can use else statement with while loop.
The pass Statement:
The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.
The pass statement is a null operation; nothing happens when it executes. The pass is also useful in places where your code will eventually go, but has not been written yet (e.g., in stubs for example):
Example:
#!/usr/bin/pythonfor letter in 'Python': if letter == 'h': pass print 'This is pass block' print 'Current Letter :', letterprint "Good bye!"
This will produce following result:
Current Letter : PCurrent Letter : yCurrent Letter : tThis is pass blockCurrent Letter : hCurrent Letter : oCurrent Letter : nGood bye!
The preceding code does not execute any statement or code if the value of letter is 'h'. The passstatement is helpful when you have created a code block but it is no longer required.
You can then remove the statements inside the block but let the block remain with a pass statement so that it doesn't interfere with other parts of the code.
- Python continue, else and pass
- python difference of pass and continue
- 零基础学python-12.2 关键字pass,else,break,continue
- Python中的循环以及break/continue/else/pass
- Python中的循环以及break/continue/else/pass
- python pass continue
- Python continue pass和break
- Python中的break、continue、pass
- Python中break、continue、pass、exit()
- python中的break|continue|pass|return
- python break,pass,continue 循环语句
- python的continue和pass的区别
- [python3教程]第四章.流程控制(if;for;break;continue;else;pass)
- python之 break语句和continue语句及PASS语句
- (7)python的break,continue,pass使用
- Python中pass、continue、break、exit()的区别
- Python学习03-条件/循环/break/continue/pass语句总结
- continue和pass测试
- Spring中Bean的使用
- Hive学习资料
- leetcode:binary-tree-postorder-traversal
- 如何打造高颜值PPT——小鱼儿何艳老师ppt课-课堂笔记
- RPC 的概念模型与实现解析
- Python continue, else and pass
- redis lua 排它锁 公平信号量实践
- 课程练习三-1010-Problem J
- PostCSS一种来自未来的书写CSS方式
- XMG 中间透明。四周不透明的蒙版
- android 使用Intent传递对象 Serializable 或者 Parcelabel
- 【LeetCode】40. Combination Sum II
- 学习编程语言总结
- B00008 C++实现的大整数计算(一)