day5-运算符和if判断

一、赋值运算符

    1、赋值运算

               如: age = 18

        (1) 增量赋值

                    age += 1    <<<>>>    age = age + 1       # 增量加

                    print(age)

                    age -= 10  <<<>>>    age = age - 10     # 增量减,还有*=,/=,%=,//=

        (2) 交叉赋值

                    x = 111

                    y = 222

                    # temp=x            <=>

                    # x=y                  <=>              x, y = y, x      #  左边三行代码与右边的一行代码意思相同

                    # y=temp            <=>

                    print(x)

                    print(y)

        (3) 链式赋值

                    x=10

                    y=x

                    z=y

                    x = y = z = 10   #这一行代码就表示了上面三行所要表达的意义,言简意赅

                    print(id(x),id(y),id(z))

        (4) 解压赋值

                    salaries = [111, 222, 333, 444, 555]

                    mon0 = salaries[0]

                    mon1 = salaries[1]

                    mon2 = salaries[2]

                    mon3 = salaries[3]

                    mon4 = salaries[4]

                    mon0,mon1,mon2,mon3,mon4=salaries

                    print(mon0)

                    print(mon1)

                    print(mon2)

                    print(mon3)

                    print(mon4)

                注意1: 变量名与值的个数必须一一对应

                    mon0,mon1,mon2,mon3,mon4,mon5=salaries  # 多一个变量名不行

                    mon0,mon1,mon2,mon3=salaries  # 少一个变量名也不行

                    salaries = [111, 222, 333, 444, 555]

                    mon0,mon1,*_=salaries   #可以用*_将不需要获取的放在一起

                    print(mon0)

                    print(mon1)

                    print(_)

                                                                          

                #   取末尾的值

                   *_,x,y=salaries

                    print(x,y)

                    print(_)

                                                                          

                #  取两边的值

                    x,*_,y,z=salaries

                    print(x)

                    print(y)

                    print(z)

                                                                          

                #  取中间的值

                    _,*midlle,_=salaries

                    print(midlle)

                # 字典也可以使用解压赋值

                    dic={'k1':111,'k2':222,'k3':3333}

                    x,y,z=dic

                    print(x,y,z)

                    print(dic[x],dic[y],dic[z])

                # 字符串在使用解压赋值时要一一对应

                    x, y, z,a,b = "hello"

                    print(x)

二、逻辑运算符

        一 、条件:只要能得到True 或 False两种值的东西都能当做条件

                例如:

                1、显式的布尔值 : 表面上看就直接是True或False

                    (1)比较运算的结果

                        print(10 > 3)

                        print(10 == 3)

                    (2)变量值直接就是True或False

                        tag = True

                2、隐式的布尔值 :表面上看上去是一种值,在底层会被解释器转换成True或False

                        0、None、空对应的布尔值为False,其余值对应的布尔值均为True

                            10                             # True

                            3.1                            # True

                            ""                               # False

                            []                                 # False

                            {"k1":222}                    # True

                            None                          # False

        二:逻辑运算符:用来连接多个条件

                (1) not 条件:对条件的结果取反

                    print(not 10 > 3)

                    print(not False)

                    print(not 0)

                (2) 条件1 and 条件2:连接左右两个条件,两个条件必须都为True,最终结果才为True

                    print(True and 10 > 3)

                    print(True and 10 == 3)

                (3) 条件1 or 条件2:连接左右两个条件,两个条件只要有一个为True,最终结果就为True

                    print(False or 10 == 3)

                    print(True or 10 == 3)

                    ps:偷懒原则=》短路运算:and连接时,左边为False就不需要看右边的了;or连接时,左边为True,就不用看右边的。

                (4) 优先级:not > and > or,推荐用括号去标识优先级

                              False    or          True

                res=(3 > 4 and 4 > 3) or (not (1 == 3 and 'x' == 'x')) or 3 > 3

                print(res)

                        print(10 and 0)    # False

                        print(10 and False)    # False

                        print(10 or False)    # True

三、if语句

                """

                if判断的完整语法:

                if 条件1:

                        代码1

                        代码2

                        代码3

                        ...

                elif 条件2:

                        代码1

                        代码2

                        代码3

                        ...

                elif 条件3:

                        代码1

                        代码2

                        代码3

                        ...

                    ...

                else:

                        代码1

                        代码2

                        代码3

                    ...

        运行原理示范:

            print(1111)

            print(2222)

            if True:

                print('if内的代码1')

                print('if内的代码2')

                print('if内的代码3')

            print(3333)

            """

        语法1:

            """

            if 条件1:

                代码1

                代码2

                代码3

                ...

         语法2:

            """

            if 条件1:

                代码1

                代码2

                代码3

                ...

            else:

                代码1

                代码2

                代码3

                ...

            """

             gender = "female"

             age = 70

             is_beautiful = True

             if gender == "female" and 60 >= age >= 18 and is_beautiful:

                print('开始表白。。。')

             else:

                 print('阿姨好,开个玩笑。。。')

             print('后续代码。。。')

            """

             语法3:

            """

            if 条件1:

                代码1

                代码2

                代码3

                ...

            elif:

                代码1

                代码2

                代码3

                ...

            """

             如果:成绩 >= 90 那么:优秀

            如果成绩 >= 80 且 < 90, 那么:良好

             如果成绩 >= 70 且 < 80, 那么:普通

             其他情况:很差

             score = input("请输入你的成绩:")  # score = "93"

             score=int(score)

             if score >= 90:

                 print("优秀")

             elif score >= 80:

                print("良好")

             elif score >= 70:

                 print("普通")

             else:

                  print("小垃圾,很差")

             print('其他代码。。。。')

             语法4:

            if的嵌套

            gender = "female"

            age = 18

            is_beautiful = True

            is_successful = True

            if gender == "female" and 60 >= age >= 18 and is_beautiful:

                print('开始表白。。。')

                if is_beautiful:

                    print('在一起')

                else:

                    print('再见。。。')

            else:

                print('阿姨好,开个玩笑。。。')

            print('后续代码。。。')

你可能感兴趣的:(day5-运算符和if判断)