zoukankan      html  css  js  c++  java
  • python基础04——运算+if判断

    可变和不可变类型

    可变类型:值改变,id不变,证明改的是原值,证明原值是可以被改变的

    不可变类型:值改变,id也变了,证明是产生新的值,压根没有改变原值,证明原值是不可以被修改的


    1 int是不可变类型
    x=10
    print(id(x))
    x=11                                                               # 产生新值
    print(id(x))

    2 float是不可变类型
    x=3.1
    print(id(x))
    x=3.2
    print(id(x))

    3 str是不可变类型
    x="abc"
    print(id(x))
    x='gggg'
    print(id(x))

    >>>>>小结:int、float、str都被设计成了不可分割的整体,不能够被改变


    4 list是可变类型
    l=['aaa','bbb','ccc']
    print(id(l))
    l[0]='AAA'
    print(l)
    print(id(l))

    5 dict

    定义:{}内用逗号分隔开多key:value,
    # 其中value可以是任意类型
    # 但是key必须是不可变类型


    dic={'k1':111,'k2':222}
    print(id(dic))
    dic['k1']=3333333333
    print(dic)
    print(id(dic))

    6 bool不可变

    条件语句

    什么是条件?什么可以当做条件?为何要要用条件?

    一、第一大类:显式布尔值
    1 条件可以是:比较运算符
    age = 18
    print(age > 16)                                                       # 条件判断之后会得到一个布尔值

    2 条件可以是:True、False
    is_beautiful=True
    print(is_beautiful)

    二、第二大类:隐式布尔值,所有的值都可以当成条件去用

    其中0、None、空(空字符串、空列表、空字典)=》代表的布尔值为False,其余都为True

     

    逻辑运算符

    一:not、and、or的基本使用

    not:就是把紧跟其后的那个条件结果取反
    ps:not与紧跟其后的那个条件是一个不可分割的整体
    #print(not 16 > 13)
    # print(not True)
    # print(not False)
    # print(not 10)
    # print(not 0)
    # print(not None)
    # print(not '')

    and:逻辑与,and用来链接左右两个条件,两个条件同时为True,最终结果才为真(一假即假)
    print(True and 10 > 3)

    print(True and 10 > 3 and 10 and 0) # 条件全为真,最终结果才为True
    print( 10 > 3 and 10 and 0 and 1 > 3 and 4 == 4 and 3 != 3)                              # 偷懒原则

    or:逻辑或,or用来链接左右两个条件,两个条件但凡有一个为True,最终结果就为True(一真即真)
    两个条件都为False的情况下,最终结果才为False
    print(3 > 2 or 0)
    print(3 > 4 or False or 3 != 2 or 3 > 2 or True)                                                     # 偷懒原则

    二:优先级not>and>or

    如果单独就只是一串and链接,或者说单独就只是一串or链接,按照从左到右的顺讯依次运算即可(偷懒原则)
    如果是混用,则需要考虑优先级了

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

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

     

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

    成员运算符 in
    print("egon" in "hello egon")                                        # 判断一个字符串是否存在于一个大字符串中

    print("e" in "hello egon")                                               # 判断一个字符串是否存在于一个大字符串中

    print(111 in [111,222,33])                                              # 判断元素是否存在于列表

    # 判断key是否存在于字典
    print(111 in {"k1":111,'k2':222})
    print("k1" in {"k1":111,'k2':222})

    not in
    print("egon" not in "hello egon")                                    # 推荐使用
    print(not "egon" in "hello egon")                                    # 逻辑同上,但语义不明确,不推荐

    身份运算符 is
    is                    判断的是id是否相等

     

    流程控制之if判断

    print(1)
    print(2)
    print(3)
    if 条件:
      代码1
      代码2
      代码3
    print(4)
    print(5)


    '''
    语法1:
    if 条件:
      代码1
      代码2
      代码3

    '''
    age = 60
    is_beautiful = True
    star = '水平座'

    if age > 16 and age < 20 and is_beautiful and star == '水平座':
    print('我喜欢,我们在一起吧。。。')

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

    '''
    语法2:
    if 条件:
      代码1
      代码2
      代码3
    else:
      代码1
      代码2
      代码3
    '''

    age = 60
    is_beautiful = True
    star = '水平座'

    if age > 16 and age < 20 and is_beautiful and star == '水平座':
    print('我喜欢,我们在一起吧。。。')
    else:
    print('阿姨好,我逗你玩呢,深藏功与名')

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


    '''
    语法3:
    if 条件1:
      代码1
      代码2
      代码3
    elif 条件2:
      代码1
      代码2
      代码3
    elif 条件2:
      代码1
      代码2
      代码3
    '''
    score=73
    if score >= 90:
    print('优秀')
    elif score >= 80 and score < 90:
    print('良好')
    elif score >= 70 and score < 80:
    print('普通')

    # 改进

    score = input('请输入您的成绩:')                            # score="18",输出str

    score=int(score)                                                       #字符串化成整型

    if score >= 90:
    print('优秀')
    elif score >= 80:
    print('良好')
    elif score >= 70:
    print('普通')

    '''
    语法3:
    if 条件1:
      代码1
      代码2
      代码3
    elif 条件2:
      代码1
      代码2
      代码3
    elif 条件2:
      代码1
      代码2
      代码3
    ...
    else:
      代码1
      代码2
      代码3
    '''
    score = input('请输入您的成绩:') # score="18"
    score=int(score)

    if score >= 90:
    print('优秀')
    elif score >= 80:
    print('良好')
    elif score >= 70:
    print('普通')
    else:
    print('很差,小垃圾')
    print('=====>')



    if嵌套if (双重条件语句)

    age = 17
    is_beautiful = True
    star = '水平座'

    if 16 < age < 20 and is_beautiful and star == '水平座':
    print('开始表白。。。。。')
    is_successful = True
    if is_successful:
    print('两个从此过上没羞没臊的生活。。。')
    else:
    print('阿姨好,我逗你玩呢,深藏功与名')

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

  • 相关阅读:
    Openstack API 开发 快速入门
    virtualBox虚拟机到vmware虚拟机转换
    使用Blogilo 发布博客到cnblogs
    Openstack Troubleshooting
    hdoj 1051 Wooden Sticks(上升子序列个数问题)
    sdut 2430 pillars (dp)
    hdoj 1058 Humble Numbers(dp)
    uva 10815 Andy's First Dictionary(快排、字符串)
    sdut 2317 Homogeneous squares
    hdoj 1025 Constructing Roads In JGShining's Kingdom(最长上升子序列+二分)
  • 原文地址:https://www.cnblogs.com/lucky-cat233/p/12430972.html
Copyright © 2011-2022 走看看