python缩进推荐使用4个空格
if循环
if else
if expression
statement(s)
if 1<2:
print "hello"
elif ‘a‘:
print "world"
else:
print "END"
以此种格式运行,if条件成立则运行下面程序,如果不成立则不执行else
例子:
#!/usr/bin/python
a = int(raw_input("please input a number:"))
if a >= 90:
print ‘A‘
elif a >= 70:
print ‘B‘
elif a >= 60:
print ‘C‘
else:
print ‘D‘
print ‘This is you score!‘
while循环
while循环使用在有条件的循环
例子1
#!/usr/bin/python
x = ‘‘
while x != ‘q‘:
print ‘hello‘
x = raw_input("please input :")
if not x:
break
if x == ‘quit‘
continue
print ‘continue‘
else:
print ‘world‘
使用while循环遍历文件
例子2
fd = open(‘/work/python/1.txt‘)
while True:
line = fd.readline()
if not line:
break
print line,
fd.close()
本文出自 “粗粮面包” 博客,请务必保留此出处http://culiangmianbao.blog.51cto.com/10475024/1975772
原文地址:http://culiangmianbao.blog.51cto.com/10475024/1975772