python--分支语句

if语句:

  1. bool数据类型:真和假,只有两个值,就是TrueFalse
  2. if语句使用的语法:
    if False:
        print('success')
    else:
        print('fail')
    
  3. else:语句:
    if False:
        print('success')
    else:
        print('fail')
    
  4. 比较运算符:

    • a==b:a和b是否相等.
          a = 1
          b = 2
          if a == b:
              print('equal')
          else:
              print('not equal')
      
    • a!=b:a和b是否不相等.
          username = input(u'请输入用户名:')
          if username != 'zhiliao':
              print('您的用户是没有被加入到黑名单,可以正常使用')
          else:
              print('您的用户被加入到黑名单,不能正常使用')
      
    • a>b:a是否大于b.
          age = 17
          if age > 17:
              print('您可以进入到网吧了')
          else:
              print('您年龄未满18岁,不能进入网吧')
      
    • a<b:a是否小于b.
          age = 17
          if age < 18:
              print('您的年龄未满18岁,不能进入网吧')
          else:
              print('您可以进入到网吧了')
      
    • a>=b:a是否大于等于b.
          age = 18
          if age >= 18:
              print('您可以进入到网吧了')
          else:
              print('您的年龄未满18岁,不能进入网吧')
      
    • a<=b:a是否小于等于b.
          age = 18
          if age <= 17:
              print('您的年龄未满18岁,不能进入网吧')
          else:
              print('您可以进入到网吧了')
      
    • 条件a and 条件b:只有条件a和条件b都满足才成立:
          age = input('请您输入年龄:')
          age = int(age)
          if age >= 15 and age <= 24:
              print('您是一个青年人')
          else:
              print('您不是一个青年人')
      
    • 条件a or 条件b:只要条件a或者条件b中的一个满足,就成立:
          if age < 15 or age > 24:
              print('您不是一个青年人')
          else:
              print('您是一个青年人')
      
    • not 条件a:如果条件a为True,那么返回False。如果条件b为False,那么返回True:

          person1 = '中国人'
          person2 = '南非'
      
          if not person2 == '中国人':
              print('不可以上战舰')
          else:
              print('可以上战舰')
      

elif语句:

   index = input('请输入星期数字:')

   index = int(index)

   if index == 0:
       print('星期天')
   elif index == 1:
       print('星期一')
   elif index == 2:
       print('星期二')
   elif index == 3:
       print('星期三')
   elif index == 4:
       print('星期四')
   elif index == 5:
       print('星期五')
   else:
       print('星期六')

猜你喜欢

转载自www.cnblogs.com/song9998/p/11627419.html