• 可变不可变类型、数据类型int,float,str基本使用及内置方法


    可变不可变类型:

    不可变类型:值改变,但是id也变,证明是产生了新值,原值是不可变类型

     x=1						
     x = 10
     print(id(x))
    
     x = 11
     print(id(x))
    

    可变类型: 值改变,但是id不变,证明就是在改原值,原值是可变类型

     l = [111,222,333]
      print(id(l))
    
     l[0] = 6666666
      print(id(l))
    

    数据类型的基本使用

    一、整型int的基本使用

    1、作用:用来记录年龄、个数、号码、等整数相关的状态
    2、定义方式:
     age = 18     # 本质age = int(18)
    
     print(type(age))      # <class 'int'>
    
    # int数据类型转换: 可以把纯数字组成的字符串转成整型
     res = int("18")
    
     print(res)    #  18
    
    3、常用操作+内置的方法

    数学运算+比较运算

    • 该类型总结:

    ​ 只能存一个值

    ​ 不可变类型

    二、浮点型float的基本使用

    1、作用:用来记录身高、体重、薪资等小数相关的状态
    2、定义方式:
     salary = 3.1  # 本质 salary = float(3.1)
    
    # float数据类型转换: 可以把小数组成的字符串转成浮点型
    
     res=float(111)
    
     print(res,type(res))    # 111.0 <class 'float'>
    
     res=float("    3.3    ")
    
     print(res,type(res))     #  3.3 <class 'float'>
    
    3、常用操作+内置的方法

    数学运算+比较运算

    • 该类型总结:

    ​ 只能存一个值

    ​ 不可变类型

    # 补充了解:
    
    # LONG类型 在python2中(python3中没有长整形的概念)
    
    # 进制转换:
     print(bin(11))    # bin十进制转二进制  # 0b1011
    
     print(oct(11))    # oct十进制转八进制  # 0o13
    
     print(hex(11))    # hex十进制转十六进制  # 0xb  
        
        
     x=1-2j    
    
     print(type(x))    # <class 'complex'> complex复数
    
     print(x.real)      # 1.0  实步
    
     print(x.imag)    # -2.0 虚步
    

    三、字符串str的基本使用

    1、用途:用来记录姓名、性别、国籍、家庭地址等描述性质的状态
    2、定义方式:在"",'',"""""",''''''内包含一串字符
     msg = "abc"  # 本质 msg = str("abc")
    
     print(type(msg))    # <class 'str'>
    
    • 注意:

      1、上述引号定义出来的都是str类型,没有区别

      2、三引号可以存放多行字符串

      3、引号的嵌套:外层双引号,内层只能用单引号

    # 数据类型转换:str可以把任意类型都转成字符串类型
    
     res = str([1,2,3])       # 列表转字符串类型
    
     print(res,type(res))     # [1, 2, 3] <class 'str'>
    

    <<< 补充知识点>>>

    右斜杠的概念:  
    
    
     print("abc
    ddd")
    
     print(r"abc
    ddd")   # √
    
     print("abc\nddd")   # √
    
     print('abc
    ddd')  
    
     file_path=r'D:
    ewa.py'  # 加r前缀
    
     file_path='D:\new\a.py' # 双斜杠转意
    
     print(file_path)    # D:
    ewa.py
    
    3、常用操作+内置的方法
    • 优先掌握的操作:(*****)

    1、按索引取值(正向取+反向取):只能取

     msg = "hello world"
    
     print(msg[0],type(msg[0]))  # h <class 'str'>
    
     print(msg[-1])        # d
    
     msg[0]="H"    # 报错 只能取不能改
    

    2、切片 (顾头不顾尾,步长)

     msg = "hello world"
    
     res = msg[1:7]
    
     res = msg[1:7:2]  # 1 3 5
    
     print(res)     # el 
    
     print(msg)
    
     res = msg[:]
    
     print(res)    # hello world
    
     res = msg[::-1]
    
     print(res)    #  dlrow olleh
    

    3、长度len:

     msg = "hello world"
    
     print(len(msg))        #  11
    

    4、成员运算in和not in:

     msg = "hello world"
    
     print("he" in msg)       # True
    
     print("he" not in msg)   # False       # 推荐使用
    
     print(not "he" in msg)   # False
    

    5、移除空白strip:

     msg = "    hello    "
    
     res = msg.strip()
    
     print(res)
    
     print(msg)
    
    # strip移除*号
    
     msg = "********hello**********"
    
     print(msg)
    
     print(msg.strip("*"))
    
    # strip移除符号
    
     msg = "+*-he*llo***%/*"
    
     print(msg.strip("*+-/%"))
    
    # strip应用用户登录验证案例:
    
     inp_name = input("username: ").strip()  # inp_name = "egon "
    
     inp_pwd = input("password: ").strip()
    
     if inp_name == "egon" and inp_pwd == "123":
    
         print('ok')
    
     else:
    
         print('no')
    

    6、切分split:

     msg="egon:123:3000"
    
     print(msg[0:4])
    
     res=msg.split(":",1) # 指定切分次数
    
     print(res)
    

    7、循环:

     for x in msg:
    
         print(x)
    
    需要掌握的操作(****)

    1、strip(去除左右两边)lstrip(去除左边)rstrip(去除右边)

     msg = "****hello****"
    
     print(msg.strip("*"))
    
     print(msg.lstrip("*"))
    
     print(msg.rstrip("*"))
    

    2、lower(所有变小写)、 upper(所有变大写)

     msg = "aAbB"
    
     print(msg.lower())
    
     print(msg.upper())
    

    3、startswith(判断开头) 、 endswith(判断结尾)

     msg = "egon is ok"
    
     print(msg.startswith("egon"))     #  True
    
     print(msg.startswith("eg"))       #  True
    
     print(msg.endswith("ok"))         #  True
    

    4、format的三种玩法

     msg = "my name is %s my age is %s" %("egon",18)
    
     msg = "my name is {name} my age is {age}".format(age=18,name="egon")
    
     print(msg)
    
    ​ 虽打乱顺序,但仍然能指名道姓为某个位置进行传值,
    
    ​ 字符串格式化推荐用format,相比%s效率更高
    
     msg = "my name is {1} my age is {0}{0} {0}".format(18,"egon")
    
     print(msg)
    
    
    
    # 补充(传字典需要加特殊符号**)
    
     msg = "my name is {name} my age is {age}".format(**{"age":18,"name":"egon"})
    
     msg = "my name is %(name)s my age is %(age)s" % {"age":18,"name":"egon"}
    
     print(msg)
    
     name = "egon"
    
     age = 18
    
     print(f"my name is {name} my age is {age}")
    
    ​  实现字符串格式化
    

    5、split切片(把一个字符串按照某一个分隔符切割成列表,指定切几刀从 左往右切)

    rsplit(指定切几刀从右往左切)

     msg = "egon:18:3000"
    
     print(msg.split(":"))
    
     print(msg.split(":",1))
    
     print(msg.rsplit(":",1))
    

    6、join(把列表用某一个连接符重新再拼接成字符串)

     msg = "egon:18:3000"
    
     l = msg.split(":")
    
       print(l)
    
     l=["egon","18","3000","44444"]
    
     print(l[0]+":"+l[1]+":"+l[2]+":"+l[3])
    
     print(":".join(l))
    

    7、replace(新值替换旧值)

     msg = 'egon xxxx egon yyy egon'
    
     print(msg.replace('egon',"EGON",-1))   # 写几次换几次 -1全换
    
     print(msg.replace('egon',"EGON"))     # 默认全换
    
     msg = "**_+_***he llo***+-**"
    
     print(msg.strip("*_+-").replace(" ",''))
    

    8、isdigit(判断是否为数字)

    num = input(">>>: ")  # num = "asfasdf"
    
    if num.isdigit():
    
        num = int(num)
    
        print(num > 10)
    
    else:
    
        print("必须输入数字,小垃圾")
    
    ###(了解即可)***
    
    # 1、find,rfind,index,rindex,count
    
     msg = 'hello el abc'
    
     res=msg.find("el")
    
     res=msg.rfind("el")
    
     res=msg.index("el")
    
     res=msg.rindex("el")
    
     res=msg.find("xxx")
    
     res=msg.index("xxx")  # 找不到则报错
    
     print(res)
    
    # 2、center,ljust,rjust,zfill
    
     print('hello'.center(50,'*'))
    
     print('hello'.ljust(50,'*'))
    
     print('hello'.rjust(50,'*'))
    
     print('hello'.zfill(50))  # 'hello'.rjust(50,'0')
    
    # 3、captalize,swapcase,title
    
     print("hello world".capitalize())
    
     print("aAbB".swapcase())
    
     print("hello world".title())
    
    # 4、is数字系列
    
    # 在python3中
    
     num1=b'4'       # bytes
    
     num2=u'4'     # unicode,python3中无需加u就是unicode
    
     num3='四'     # 中文数字
    
     num4='Ⅳ'     # 罗马数字
    
    # bytes、unicode
    
     print(num1.isdigit())    # True
    
     print(num2.isdigit())    # True
    
     print(num3.isdigit())    # False
    
     print(num4.isdigit())    # False
    
    #  unicode
    
     print(num2.isdecimal())    # True
    
     print(num3.isdecimal())    # False
    
     print(num4.isdecimal())    # False
    
     unicode、中文数字、罗马数字
    
     print(num2.isnumeric())    # True
    
     print(num3.isnumeric())    # True
    
     print(num4.isnumeric())    # True
    
    # 5、is其他
    
     name="EGON123"
    
     print(name.isalpha())  # 只能由字母组成
    
     print(name.isalnum())  # 字母或数字组成
    
     print(name.islower())
    
     print(name.isupper())
    
     name="               "
    
     print(name.isspace())
    
     name="Hello world"
    
     print(name.istitle())
    
    • 该类型总结

    ​ 存一个值

    ​ 有序

    ​ 不可变

  • 相关阅读:
    HTML表格布局例子
    WCF分布式开发必备知识(2):.Net Remoting (转)
    WCF分布式开发必备知识(1):MSMQ消息队列(转)
    WCF数据契约与序列化(转)
    Asp.net中图片存储数据库以及页面读取显示通用方法详解附源码下载
    2010年初的一点随想
    Windows7旗舰版磁盘分区详解—附分区步骤截图
    AjaxControltoolkit(工具包)安装步骤说明
    Windows7安装IIS中关于Windows 系列于谷歌Chrome系统争议一点联想
    Oracle 10G中关于约束在表和列中使用详解.
  • 原文地址:https://www.cnblogs.com/gfeng/p/14176454.html
Copyright © 2020-2023  润新知