• 输入输出、基本运算符、流程控制


    补充输入输出

      1.Python3版本中只有一个input

      2.Python2版本中raw_input与Python3版本中的input一摸一样

      3.Python2中的input:要求输入者必须输入一个明确的数据类型,输入什么类型就存成什么类型

    运算符

    1.算数运算符

    int,float=>数字类型  
    
        print(10 + 3.1)   ==》13.1
        print(10 / 3)     ==》3.33333333335
        print(10 // 3)    ==》3
        print(10 % 3)        ==》1
        print(10 ** 2)   ==》100
    
        +与*(了解)
    
             msg1='hello'
          msg2='world'
          print(msg1 + msg2)
          ==》 helloworld
          print(msg1*10)
          ==》hellohellohellohellohellohellohellohellohellohello
          l1=['a','b']
          l2=['c','d']
          print(l1 + l2)
                            ==》['a', 'b', 'c', 'd'] 
           print(l1*3)
    
                                ==》 ['a', 'b', 'a', 'b', 'a', 'b']

    2.赋值运算符

    增量赋值
    
        age=18
    
        age+=1  即age=age+1
    
        print(age)
    
        ==》19
    
        age=18
    
        age*=3 即age=age*3
    
        print(age)
    
        ==》54
    
      交叉运算
    
        x=11
    
        y=22
    
        temp=x
    
        x=y
    
        y=temp
    
         ||
    
        x,y=y,x
    
        print(x,y)
    
        ==》22
    
          11
    
      链式赋值
    
        1.列表的解压赋值
    
          l=['egon',18,'male',3333]
    
          a=l[0]
    
          b=l[1]
    
          c=l[2]
    
          d=l[3]
    
           ||
    
          a,b,c,d=l
    
          print(a,b,c,d)
    
          ==》egon 18 male 33333      salaries=[1.1,2.2,3.3,4.4,5.5]
    
          取前两者
          a,b,*_=salaries
          取最后两者
          *_,a,b=salaries
          取第一个和最后一个
          a,*_,b=salaries
          print(a,b,)
        2.字典的解压赋值
          dic={'aaa':1,'bbb':2,'ccc':3}
          x,y,z=dic
          print(x,y,z)
          ==》aaa bbb ccc
        3.逻辑运算符
          and: 左右两个条件必须同时成立,最终结果才为True
            print(10 < 3 and 3 == 3)
                ==》Fales
          or: 左右两个条件只要有一个成立,最终结果就为True
            print(10 < 3 or 3 == 3)
                ==》True
            print(10 < 3 or 3 < 3)
                ==》False
          not:将紧跟其后的条件结果取反
            print(not 10 > 3)
                ==》False
            print(not 10 < 3 or 3 < 3)
                ==》True
            res=(10 > 3 and 3 == 1) or ((4 < 3 and True) or (not 1 > 2 or 3 > 2))
                    print(res)
                ==》True
        
        4.比较运算符
          ==
            print(10 != 3)
              ==》True
    
    

    流程控制之if判断

    语法一:

      if 条件:
       代码1
      代码2
      代码3

    gender='female'
          age=18
          is_beautiful=True
    
          if gender == 'female' and age > 16 and age < 20 and is_beautiful:
            print('开始表白。。。。')
    
          print('其他代码')

    语法二:

      if 条件:
      代码1
      代码2
      代码3
      else:
      代码1
      代码2
      代码3
    gender='female'
      age=26
      is_beautiful=True
    
      if gender == 'female' and age > 16 and age < 20 and is_beautiful:
          print('开始表白。。。。')
      else:
          print('阿姨好')
    
      print('其他代码')

    语法三:

    if 条件1:
       if 条件2:
       代码1
      代码2
       代码3
     gender='female'
      age=18
      is_beautiful=True
      is_successfull=True
      if gender == 'female' and age > 16 and age < 20 and is_beautiful:
          print('开始表白。。。。')
          if is_successfull:
              print('在一起,,,')
          else:
              print('逗你玩呢。。。')
      else:
          print('阿姨好')
    
      print('其他代码')

    语法四:

      if 条件1:
      代码1
      代码2
      代码3
      elif 条件2:
      代码1
      代码2
      代码3
      elif 条件3:
      代码1
      代码2
      代码3
      .......
      else:
      代码1
      代码2
      代码3
     '''
      如果:成绩>=90,那么:优秀
    
      如果成绩>=80且<90,那么:良好
    
      如果成绩>=70且<80,那么:普通
    
      其他情况:很差
      '''
          score=input('your score: ')
          score=int(score)
    
          if score >= 90:
              print('优秀')
          elif score >= 80:
              print('良好')
          elif score >= 70:
              print('普通')
          else:
              print('很差')

    流程控制之while循环

      #引入:
      name='egon'
      pwd='123'
    
      inp_name=input('your name: ')
      inp_pwd=input('your password: ')
      if inp_name == name and inp_pwd == pwd:
          print('login successfull')
      else:
          print('name or password error')
      inp_name=input('your name: ')
      inp_pwd=input('your password: ')
      if inp_name == name and inp_pwd == pwd:
          print('login successfull')
      else:
          print('name or password error')
    
      inp_name=input('your name: ')
      inp_pwd=input('your password: ')
      if inp_name == name and inp_pwd == pwd:
          print('login successfull')
      else:
          print('name or password error')
    循环就是重复做某件事
      语法:
      while 条件:
      代码1
      代码2
      代码3
     n=1
      while n < 10:
          print(n)
          n+=1
    
      name='egon'
      pwd='123'
    
      tag=True
      while tag:
          inp_name=input('your name: ')
          inp_pwd=input('your password: ')
          if inp_name == name and inp_pwd == pwd:
              print('login successfull')
              tag=False
          else:
              print('name or password error')
    
      while+break:终止本层循环
      name='egon'
      pwd='123'
    
      while True:
          inp_name=input('your name: ')
          inp_pwd=input('your password: ')
          if inp_name == name and inp_pwd == pwd:
              print('login successfull')
              break
          else:
              print('name or password error')
    
    
  • 相关阅读:
    js获取长度,根据编码获取长度
    springcloud(七,多个服务消费者配置,以及zuul网关案例)
    springcloud(六,多个服务提供者)
    springcloud(五,多个服务注册中心eureka)
    jquery根据选择器进行页面赋值,封装赋值方法
    让页面元素无法选中,不能全选
    取消绑定事件
    js设置元素指定时间隐藏
    js手动抛出异常
    php 之 数据访问 查询关键字 (0506)
  • 原文地址:https://www.cnblogs.com/ShenJunHui6/p/10196322.html
Copyright © 2020-2023  润新知