• python入门006


    一:可变与不可变类型

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

    2、验证

    2.1 int是不可变类型

    2.2 float是不可变类型

    2.3 str是不可变类型


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

    2.4 list是可变类型

    2.5 dict是可变类型


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

    2.6 bool不可变
    True及True
    Flase及Flase

    二:条件

    2、什么是条件?什么可以当做条件?为何要要用条件?
    第一大类:显式布尔值
    2.1 条件可以是:比较运算符
    age = 18
    print(age > 16) # 条件判断之后会得到一个布尔值

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

    第二大类:隐式布尔值,所有的值都可以当成条件去用
    其中0、None、空(空字符串、空列表、空字典)=》代表的布尔值为False,其余都为真

    三:逻辑运算符

    一: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
    ps:
    如果单独就只是一串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)

    四:成员运算与身份运算

    1、成员运算符
    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") # 逻辑同上,但语义不明确,不推荐

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

    五:流程控制之if判断

    '''
    语法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"
    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('其他代码.............')

    作者:Joab
    本文版权归作者和博客园所有,欢迎转载,转载请标明出处。
    如果您觉得本篇博文对您有所收获,请点击右下角的 [推荐],谢谢!
  • 相关阅读:
    面试题目整理(MySQL系列-调优)
    面试题目整理(MySQL系列-事务)
    面试题目整理(MySQL系列-索引)
    MySQL遇到问题
    Gorm的高级用法
    Gorm的初步使用(使用频率排序)
    MySQL索引详解
    SSH命令行上传/下载文件
    SQLSTATE[HY000]: General error: 1267 Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8mb4_unicode_ci,COERCIBLE) for operation 'like' 。。。
    Redis 缓存穿透、缓存雪崩、缓存击穿解决方案
  • 原文地址:https://www.cnblogs.com/linqiaobao/p/12430287.html
Copyright © 2020-2023  润新知