Home > Backend Development > Python Tutorial > python 控制语句

python 控制语句

WBOY
Release: 2016-06-16 08:47:04
Original
1202 people have browsed it

1比如python提倡简单实用的思想,它就没有switch语句,如果要实现switch语句的效果
的话可以通过2个方法来写把
(1)通过if elif 语句来实现
if 条件:

elif 条件:

else:

(2)通过

复制代码 代码如下:

info = {}
info = {‘a':'1′,'b:2,'c:3,'default':ss'}
c = info.get(‘a',default')
2while语句有else语句
a = 2
while a > 1:
print ‘success'
else:
print ‘error'

当while循环条件变为False的时候,else块才被执行——这甚至也可能是在条件第一次被检验的时候。
3 for语句有else语句
复制代码 代码如下:

for i in xrange(5):
print i
else:
print ‘loop is end'

结果是 0 1 2 3 4 loop is end
记住,else部分是可选的。如果包含else,它总是在for循环结束后执行一次,
如果是遇到break语句,for 后面的else语句就不会执行了
复制代码 代码如下:

for i in xrange(5):
if i == 3:
print ‘success'
else:
print ‘loop is end'

结果是 success

本文来自: Rootexp
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template