source = int (raw_input("please input a num: ")) #raw_input输入的是字符串,需要转化才能if
if source >= 90:
print 'A'
print 'very good'
elif source >= 70:
print 'B'
print 'good'
else:
print 'C'
print 'It's bad'
print "ending"
while 判断条件:
执行语句……
#!/usr/bin/python
count = 0
while count < 5:
print count, " is less than 5"
count = count + 1
else:
print count, " is not less than 5"
>>>>>>
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5
注:while..else和for...else相同,会在循环正常执行完的情况下执行(并非遇到break等跳出中断)
Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。
for iterating_var in sequence:
statements(s)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
fruits = ['banana', 'apple', 'mango']
for index in range(len(fruits)):
print '当前水果 :', fruits[index]
print "Good bye!"
>>>>>>>>
当前水果 : banana
当前水果 : apple
当前水果 : mango
Good bye! 随机函数:
range(5) >>> 0,1,2,3,4
range(1,6) >>> 1,2,3,4,5
range(1,10,2) >>> 1,3,5,7,9
xrange(5) >>> 0,1,2,3,4
xrange(1,6) >>> 1,2,3,4,5
xrange不占内存,当用到时才会调用到,测试:
a = xrange(5); print a 举例:嵌套循环输出2-30之间的素数?
#!/usr/bin/python
# -*- coding:UFT-8 -*-