If嵌套

  1. # 一个随机数程序
  2. import random
  3. rand = random.randint(0,10)
  4. var =input('please input a numer\n>')
  5. var = int(var)
  6. if var > 0:
  7. print('This is a postive')
  8. if ( var >= 18 ) and ( var <= 60 ):
  9. print('You are at normal age')
  10. elif ( var < 18 ):
  11. print('You are too young to work!')
  12. else:
  13. print('You should retire')
  14. elif var < 0:
  15. print('This is a nagetive')
  16. else:
  17. print('This is zero')

在C语言中

else if被简化为 elif

只要对其,就不需要大括号

但是语句一行的末尾需要引号标识

需要借助IDE来区分逻辑关系

三目运算符

  1. a = a if a > b else b

Python中诊治放在前面,否值放在后面