zoukankan      html  css  js  c++  java
  • python 数据类型及内置方法

    今日目录:

      零、解压赋值+for循环

      一、 可变/不可变和有序/无序

      二、基本数据类型及内置方法

        1.整型 int

        2.浮点型float

        3.字符串类型

        4.列表类型

      三、后期补充内容

    零、解压赋值+for循环

    # for循环 + 解压赋值
    info = [
        ["zhagnsan","18","male"],
        ["lisi","19","male"],
    ]
    
    for name,age,gender in info:
        print("姓名:%s , 年龄:%s , 性别:%s"%(name,age,gender))
    View Code

    一、 可变/不可变和有序/无序

    1.可变、不可变:

      可变类型:在值变的情况下,id不变,证明就是在改原值,是可变类型。
      不可变类型:在值变的情况下,id也跟着变,证明产生了新值,是不可变类型。

    2.有序、无序:

      有序:能通过索引取值的数据类型都是有序的
      无序:则相反,例子:dict

    二、数字类型

    1.整型:int

    1.1 用途
      数字,QQ号等
    1.2 定义方式
     age = 10  # age = int(10)
    1.3 数据类型转换
      只能将纯数字字符串转成int
     x = int ('123')
     print(x,type(x))

    2.浮点型:float

    2.1 用途
      薪资、身高
    2.2 定义方式:
     salary = 10.1  # salary = float(10.1)
    2.3 数据类型转换
      将包含小数的字符串转成 float
    x = float('10.1')
    print(x,type(x)) 

      总结:

      1.变量只存一个值
      2.都是不可变类型

    3.字符串类型

    3.1 用途
      记录描述性质的状态
    3.2 定义方式
      在单引号、双引号、三引号内包含一串字符串
    msg = 'hello' # msg = str("hello") 
    3.3 数据类型转换
      所有类型都可以被str转换成字符串类型
    res = str([1,2,3])
    print(res,type(res)) 
    3.4 优先掌握的操作
    1、按索引取值(正向取 + 反向取):只能取
     msg = 'hello'
     print(msg[0])
    View Code
    
    2、切片(顾头不顾尾,步长)
    	
    msg = "zhangsan hello world"
    print(msg[0:3:1])
    print(msg[::-1])
    View Code
    
    3、长度len
    msg = "zhangsan hello world"
    print(len(msg))
    View Code
    
    4、成员运算 in 和 not in
    msg = "zhangsan hello world"
    print("zhangsan" in msg)
    View Code
    
    5、移除空白 strip
    res = "  hello  ".strip()
    print(res)
    
    print("*&*^^%zhangsan&*$#@".strip("!@#$%^&*"))
    View Code
    
    6、切分split
    	split:针对有规律字符串,按照某种分隔符切成列表
    info = 'zhangsan_18_male'
    res = info.split('_')
    print(res)
    View Code
    
    7、循环
    for i in 'hello':
        print(i)
    View Code

    8、join   有字符串、数字两种类型,报错
    # 用 :号作为连接符将春字符串的列表拼接成一个字符串
    info = 'zhangsan_18_male'
    res = info.split('_')
    res1 = res[0] + ":" + res[1] + ":" + res[2]
    print(res1)
    res2 = ":".join(res)
    print(res2)
    View Code 
    3.5 需要掌握的操作
    1、strip,lstrip,rstrip
    msg = "*****zhangsan*********"
    print(msg.strip("*"))
    print(msg.rstrip("*"))
    print(msg.lstrip("*"))
    View Code
    
    2、lower,upper
    print("zhangsan".upper())
    print("zhangSAN".lower())
    View Code
    3、startswith,endswith
    info = "zhangsan is 18"
    print(info.startswith("zha"))
    print(info.endswith("18"))
    View Code
    4、format的三种玩法
    res = 'my name is {name},age is {age}'.format(name = 'zzw',age = 18)
    res1 = 'my name is {0},age is {1}'.format('zzw',19)
    res2 = 'my name is {},age is {}'.format('zzw',20)  # 对应位置填值
    
    print(res)
    print(res1)
    print(res2)
    View Code
    5、split,rsplit
      没有指定切分次数的情况下,没有区别
    info = 'zhang:san:is:18'
    print(info.split(":"))
    print(info.rsplit(":"))
    View Code
    6、replace
    msg = "zhangsan is zhangsan ,zhangsan is 18"
    print(msg.replace("zhangsan","lisi"))
    print(msg.replace("zhangsan","lisi",2))
    View Code
    7、isdigit
      判断字符串是否是纯数字组成
    print('123'.isdigit())
    View Code
    3.6 其他操作(了解即可)
      1、find:找不到不报错,返回-1
        rfind:从右边开始找子字符串,找到后返回正向数的位置
        index:找不到会报错
        rindex:从右边开始找
        count:统计一个子字符串在大字符串中的个数
    
      2、center:有两个参数,可以设定宽度,和空余位置填充的符号
        ljust:左对齐,其余位置填充符号
        rjust:右对齐,其余位置填充符号
        zfill:右对齐,其余位置填充0
    
      3、expandtabs
      设置一个制表符的宽度(自行设置几个空格)
      4、captalize:首字母大写
        swapcase:大小写反转
        title:每个单词首字母大写
      5、is数字系列
        
      6、is其他

    4.列表类型

    4.1 数据类型转换
      但凡能够被for循环遍历的数据类型都可以传给list
    res = list('hello')
    View Code 
    4.2 优先掌握的操作
      1、按索引取值(正向取 + 反向取):既可存也可以取
    	强调:对于不存在的索引会报错
    
      2、切片(顾头不顾尾,步长):l[0,5,2]
      3、长度len
      4、成员运算 in 和 not in
    
      5、追加:append 
         插入:insert:往指定索引前面插入值
    l.append("a") : 在末尾加
    l.insert('0','b'):插到第0位置
    View Code
      6、删除:
        6.1 del l[1] :是一种通用删除操作,没有返回值
    
        6.2 remove  
          l.remove("xxx") #指定要删除的值,没有返回结果(None)
    	
        6.3 pop:从列表中拿走一个值 
          l.pop(-1)  #按照索引删除值(默认从末尾删除),返回删除的那个数
          l.pop()  #从前面取出来
      7、循环

    三、后期补充内容

    字符串操作及内置方法:菜鸟教程

    列表操作及内置方法:菜鸟教程

     
  • 相关阅读:
    jsp第七次作业
    jsp第二次作业
    第四次JSP作业
    软件测试练习第一次
    JSP第一次课后作业
    读信息
    购物商城
    页面跳转
    安卓第7周作业
    安卓第六周作业
  • 原文地址:https://www.cnblogs.com/xt12321/p/10578417.html
Copyright © 2011-2022 走看看