Python 条件语句
flag = False
name = 'lixin'
if name == 'python': # 判断变量是否为 python
flag = True # 条件成立时设置标志为真
print 'welcome boss' # 并输出欢迎信息
else:
print name # 条件不成立时输出变量名称
num = 5
if num == 3: # 判断num的值
print 'it is 3'
elif num == 2:
print 'it is 2'
elif num == 1:
print 'it is 1'
elif num < 0: # 值小于零时输出
print 'it is 0'
else:
print 'default' # 条件均不成立时输出
if num >= 0 and num <= 10: # 判断值是否在0~10之间
print 'hello'
if ( var == 100 ) : print "变量 var 的值为100"
Python 循环语句
count = 0
while (count < 9):
print 'The count is:', count
count = count + 1
print "Good bye!"
count = 0
while count < 5:
print count, " is less than 5"
count = count + 1
else:
print count, " is not less than 5"
for letter in 'Python': # 第一个实例
print("当前字母: %s" % letter)
fruits = ['banana', 'apple', 'mango']
for fruit in fruits: # 第二个实例
print ('当前水果: %s'% fruit)
print ("Good bye!")
Python break 语句
for letter in 'Python': # 第一个实例
if letter == 'h':
break
print '当前字母 :', letter
for letter in 'Python': # 第一个实例
if letter == 'h':
continue
print '当前字母 :', letter
Python pass 语句
pass 不做任何事情,一般用做占位语句。
for letter in 'Python':
if letter == 'h':
pass
print '这是 pass 块'
print '当前字母 :', letter