• 第四章 Python——数据类型


    目录

    一、数据类型概述

    二、数字类型

    三、字符串类型

    四、列表类型

    五、元组类型

    六、字典类型

    七、集合类型

    一、数据类型概述

    什么是数据类型(what):变量才是我们存储的数据,所以数据类型指的是变量值的不同种类。

    为什么数据类型要分类(why):变量值是存现实世界的状态,针对不同状态就应该用不同类型表示。

    术语备注:

    ①可变类型:在id不变的情况下,值可以改变

    ②不可变类型:值改变,id也跟着变

    二、数字类型(str、float)

    【类型1】

    整型int

    ①应用场景:表示人的年龄,号码,等级,价格等。

    ②定义方式:

    age = 18  # age = int(18)

    ③类型type:int

    ④常用操作+内置方法:

    a)算数运算

    b)比较运算

    【类型2】

    浮点型float

    ①应用场景:表示身高,体重,工资等。

    ②定义方式:

    salary = 8928.91  # salary = float(8929.91)

    ③类型type:float

    ④常用操作+内置方法:

    a)算数运算

    b)比较运算

    【类型3*】

    长整型long与复数complex

    ①长整型long只在Python2中才有。

    ②复数complex

    x = 1 - 2j

    print( x , real )  #1.0

    print ( x , imag )   #-2,0

    【类型总结】
    
    1.只能存1个值
    
    2.没有顺序的说法
    
    3.不可变类型

    三、字符串类型(str) 

    ①应用场景:表示人的名字,爱好,性格等。

    ②定义方式:

    name = ‘xujiayu’  # name = str(xujiayu)

    ③类型type:str

    ④常用操作+内置方法:

    a)按索引取值

    #按索引取值(正向取+反向取) :只能取
    msg='hello world'
    print(type(msg[5]))
    print(msg[-1])
    msg[2]='A'

    b)切片

    #切片(顾头不顾尾,步长)
    msg='hello world'
    print(msg[1:5],type(msg[1:5]))
    print(msg[6:-1])
    print(msg[6:11])
    print(msg[6:])
    print(msg[6::2])
    #了解:
    print(msg[0:])
    print(msg[::-1])
    msg='hello world'
    print(msg[-3:-6:-1])
    print(msg[6:9:-1])

    c)统计长度len

    #长度len
    msg='hello world'
    print(len(msg))

    d)成员运算in和not in

    print('SB' in  'my name is alex,alex is SB')
    print('alex' in  'my name is alex,alex is SB')
    print('egon' not in  'my name is alex,alex is SB') # 推荐
    print(not 'egon' in  'my name is alex,alex is SB')

    e)strip、lstrip、rstrip

    name='****A*e*gon****'
    print(name.strip('*'))
    print(name.rstrip('*')) #去除左边内容
    print(name.lstrip('*')) #去除右边内容
    print('*-=egon *&^'.strip('-= *&^')) #去除特殊字符

    f)split、lsplit、rsplit

    #切分成split
    msg='egon:18:male:180:160'
    l=msg.split(':')
    print(l)
    print(l[3])
    info='egon:18:male'
    print(info.split(':',1))

    print(info.split(':',1)) #['egon','18:male']
    print(info.rsplit(':',1)) #['egon:18','male']

    g)upper、lower

    #lower,upper
    name='EoN'
    print(name.lower())
    
    name='egonN'
    print(name.upper())

    h)startwith、endwith

    #startswith,endswith
    print('alex is SB'.startswith('alex'))
    print('alex is SB'.endswith('B'))

    i)fomat

    #format的三种玩法
    print('my name is %s my age is %s' %('egon',18))
    print('my name is {name} my age is {age}'.format(age=18,name='egon')) # 可以打破位置的限制,但仍能指名道姓地为指定的参数传值
    
    print('my name is {} my age is {}'.format('egon',18))
    print('my name is {0} my age is {1} {1} {1} {1}'.format('egon',18))

    j)join

    #join:只能将元素全为字符串的列表拼成一个大的字符串
    info='egon:18:male'
    l=info.split(':')
    print(l)
    new_info='-'.join(l)
    print(new_info)
    
    num=['a','b','c']
    ':'.join(num) #'a'+':'+'b'+':'+'c'
    
    num=[1,2,'c']
    ':'.join(num) #1+':'+2+':'+'c'

    k)replace

    #replace替换
    msg='my name is wupeiqi,wupeiqi is SB'
    print(msg.replace('wupeiqi','Pig',1))
    print(msg)

    l)isdigit、isalpha、isspace

    #isdigit、isspace、isalpha
    print('111.1'.isdigit())
    print('1111'.isdigit())
    print('xu'.isalpha())
    print(' '.isspace())

    m)find,rfind,index,rindex,count

    #find,rfind,index,rindex,count
    msg='my name is alex,alex is hahaha'
    print(msg.find('alex'))
    print(msg.find('SB')) #找不到会返回-1
    
    print(msg.index('alex'))
    print(msg.index('SB')) # 找不到index会报错
    
    print(msg.find('alex',0,3))
    
    print(msg.count('alex'))
    print(msg.count('alex',0,15))

    n)center,ljust,rjust,zfill

    #center,ljust,rjust,zfill
    print('info egon'.center(50,'-'))
    print('info egon'.ljust(50,'-'))
    print('info egon'.rjust(50,'-'))
    print('info egon'.zfill(50))

    o)expandtabs

    #expandtabs
    print('a	b	c'.expandtabs(1))

    p)captalize,swapcase,title

    #captalize,swapcase,title
    print('my name is egon'.capitalize())
    print('my Name Is egon'.swapcase())
    print('my name is egon'.title())

    四、列表类型(list)

    五、元组类型(tuple)

    六、字典类型(dict)

    七、集合类型(set)

  • 相关阅读:
    AcWing 1135. 新年好 图论 枚举
    uva 10196 将军 模拟
    LeetCode 120. 三角形最小路径和 dp
    LeetCode 350. 两个数组的交集 II 哈希
    LeetCode 174. 地下城游戏 dp
    LeetCode 面试题 16.11.. 跳水板 模拟
    LeetCode 112. 路径总和 递归 树的遍历
    AcWing 1129. 热浪 spfa
    Thymeleaf Javascript 取值
    Thymeleaf Javascript 取值
  • 原文地址:https://www.cnblogs.com/neymargoal/p/9173006.html
Copyright © 2020-2023  润新知