zoukankan      html  css  js  c++  java
  • 数字、字符串、列表

    一、可变与不可变类型

    1、可变类型:值改变,但是id不变,证明就是在改变原值,是可变类型。
    2、不可变类型:值改变,但是id也跟着变,证明是产生了新的值(也意味着创建了新的内存空间),是不可变类型

    # 总结:
    可变类型:列表,字典
    不可变类型:整型,浮点型,字符串,元组

    二、数字类型

    1、用途

    整型:记录年龄、等级等
    浮点型:记录薪资、身高、体重等

    2、定义方式

    整型:a = 3  #a = int(3)
    数据类型转换:只能将纯数字的字符串转成int

    浮点型:salary = 10.3  #salary = float(10.3)
    数据类型转换:只能将包含小数的字符串转成float

    3、总结

    存一个值
    不可变类型

    三、字符串类型

    1、作用

    记录描述性质的状态

    2、定义方式

    在单引号、双引号、三引号内包含一串字符串
    msg = 'hello'  #msg = str('hello)
    数据类型转换:所有类型都可以被str转成字符串类型

    3、常用操作+内置方法

    # 优先掌握的操作:

    1、按索引取值(正向取+反向取):只能取且取出来的字符仍然是字符串类型
    msg = 'hello'
    print(msg[0],type(msg[0]))  #h <class 'str'>
    print(msg[-1])  #o
    msg[0] = 'H'  #error 只能取

    2、切片(顾头不顾尾,步长)
    msg = 'hello world'
    res = msg[0:3:1]  #0 1 2
    print(res)  #h e l
    print(msg)  #原值并不改变
    res = msg[:]  #0 1 2 3 4 5 6 7 8 9 10
    res = msg[::2]  #0 2 4 6 8 10
    res = msg[-1:-12:-1]  #10 9 8 ... 0
    res = msg[-1::-1]  #一直执行到最后
    res = msg[::-1]  #默认从后向前依次出发

    3、长度 len
    print(len(msg))

    4、成员运算 in not in:判断一个子字符串是否存在于大字符串中,返回的值是 True False
    msg = 'hello'
    print('hel' in msg) #True
    print('hell' not in msg) #False
    print(not 'hel' in msg) #False

    5、移除空白strip:用来去除字符串左右两边的字符,不指定默认去除的是空格
    msg = '   he llo   '
    res = msg.strip() #he llo 无法去除中间的空格
    print('*****eg**on****'.strip('*')) #eg**on
    print('***%%%$eg*on&&**#'.strip('*%$&#')) #eg*on
    print('*****egon******'.lstrip('*')) #egon******
    print('*****egon******'.rstrip('*')) #*****egon
    #下面一个程序主要是针对于用户在输入信息时会输入空格
    name=input('username>>>: ').strip() # name='egon     '
    pwd=input('password>>>: ').strip()
    if name == 'egon' and pwd == '123':
        print('login successful')
    else:
        print('输错了。。。')
           
    6、切分split:针对有规律的字符串,按照某种分隔符切成列表
    info='egon:18:male'
    res=info.split(':')
    print(res,type(res))  #['egon','18','male'] <class 'list'>
    print(res[0],res[1])  #egon 18

    cmd='get|a.txt|33333'
    print(cmd.split('|',1))  #['get','a.txt|33333']
    cmd='get|a.txt|33333'
    print(cmd.split('|',2))  #['get','a.txt','33333']

    用:号作连接符号将纯字符串的列表拼接成一个字符串
    l=['egon', '18', 'male']  
    res=l[0]+':'+l[1]+':'+l[2]
    print(res) # 'egon:18:male'
    res=','.join(l)
    print(res) # 'egon,18,male'
    res = ':'.join(l)
    print(res) # 'egon:18:male'

    7、循环
    for item in 'hello':
       print(item)

    需要掌握的操作
    1、strip,lstrip,rstrip
    print('******egon***********'.strip('*')) #egon
    print('******egon***********'.lstrip('*')) #egon**********
    print('******egon***********'.rstrip('*')) #******egon

    2、lower,upper
    print('Abc123'.lower()) #abc123
    print('Abc123'.upper()) #ABC123

    3、startswith,endswith
    msg='alex is dsb'
    print(msg.startswith('alex i'))
    print(msg.endswith('dsb'))

    4、format的三种玩法
    res='my name is %s my age is %s' %('egon',18)
    print(res) #my name is egon my age is 18

    res='my name is {name} my age is {age}'.format(age=18,name='egon')
    print(res) #my name is egon my age is 18

    了解
    res='my name is {} my age is {}'.format('egon',18)
    print(res) #my name is egon my age is 18
    res='my name is {0}{1} my age is {1}{2}{1}{3}'.format('egon',18,'chen',20)
    print(res) #my name is egon18 my age is 18chen1820

    5、split,rsplit
    msg='a:b:c:d'
    print(msg.split('b',1)) #['a:',':c:d']
    print(msg.rsplit(':',2)) #['a:b','c','d']
    print(msg.rsplit(':')) #['a','b','c','d']

    6、replace
    msg='kevin is kevin is hahahah'
    res=msg.replace('kevin','sb',1)
    print(res) #sb is kevin is hahaha

    7、isdigit
    #判断字符串是否是纯数字
    print('123123'.isdigit()) # 如果字符串是由纯数字组成的,则返回True
    print('123123   '.isdigit())
    print('123123asdf'.isdigit())
    print('12312.3'.isdigit())

    score=input('>>>>: ').strip() #score='asdfasdfasfd'
    if score.isdigit():
       score=int(score)
       if score >= 90:
           print('优秀')
       else:
           print('小垃圾')
    else:
       print('必须输入纯数字')

    了解的操作
    1、find,rfind,index,rindex,count
    print('123 ke123ke'.find('ke')) #4
    print('123 ke123ke'.rfind('ke')) #9
    print('123 ke123ke'.index('ke')) #4
    print('123 ke123ke'.rindex('ke')) #4

    print('123 ke123ke'.find('xxxx')) #-1
    print('123 ke123ke'.index('xxxx')) #ValueError
    print('123 ke123ke'.count('ke')) #2
    print('123 ke123ke'.count('ke',0,5)) #0 因为只能取到0到4(顾头不顾尾)
    print('123 ke123ke'.count('ke',0,6)) #1
    print('123 ke123ke'.count('ke',0,15)) #2

    2、center,ljust,rjust,zfill
    print('egon'.center(50,'*')) # ****************egon******************
    print('egon'.ljust(50,'*')) # egon**********************************
    print('egon'.rjust(50,'*')) # **********************************egon

    print('egon'.rjust(50,'0'))
    print('egon'.zfill(50))  #z:表示zero   zfill:表示用0来填充

    3、captalize,swapcase,title
    print('abcdef dddddd'.capitalize())  #仅仅把字符串中第一个字母转换为大写,如果本来就为大写,则还是大写
    print('Abcdef dddddd'.capitalize())
    print('abcAef dddddd'.swapcase()) #大小写反转
    print('abcAef dddddd'.title())   #每个单词开头都转换为大写
    print('abcAef Dddddd'.title())

    4、is数字系列
    num1=b'4' #bytes
    num2='4' #unicode,python3中无需加u就是unicode
    num3='四' #中文数字
    num4='Ⅳ' #罗马数字

    bytes与阿拉伯数字组成的字符串
    print(num1.isdigit())
    print(num2.isdigit())
    print(num3.isdigit())
    print(num4.isdigit())

    阿拉伯数字组成的字符串
    print(num2.isdecimal())
    print(num3.isdecimal())
    print(num4.isdecimal())

    阿拉伯数字中文罗马组成的字符串
    print(num2.isnumeric())
    print(num3.isnumeric())
    print(num4.isnumeric())

    5、is其他

    4、总结

    存一个值

    有序

    不可变

    四、列表类型

    1、用途

    按照位置记录多个值,索引对应值

    2、定义方式

    在[]内用逗号分隔开多个任意类型的值
    l=['a',11,11.3,] # l=list(['a',11,11.3,])

    数据类型转换:但凡能够被for循环遍历的数据类型都可以传给list,被其转换成列表
    res=list('hello')
    # res=list(123)
    print(res)

    res=list({'a':1,'b':2,'c':3}) # []
    print(res) #['a','b','c']

    3、常用操作+内置的方法

    #优先掌握的操作:
    3.1、按索引存取值(正向存取+反向存取):即可存也可以取
    l=['a','b','c','d','e']
    print(l[0])
    print(l[-1])
    print(id(l))
    l[0]='A'
    print(id(l))

    #强调强调强调!!!:对于不存在的索引会报错
    l[5]='AAAA'  #error
    但是对于字典来说却可以添加索引
    dic={"k1":111}
    dic['k2']=2222
    print(dic) #{'k1':111,'k2':2222}

    3.2、切片(顾头不顾尾,步长)
    l=['a','b','c','d','e']
    print(l[1:4])  #['b','c','d']
    print(l[::-1])  #['e','d','c','b','a']

    3.3、长度
    l=['a','b','c','d','e']
    print(len(l)) #5

    3.4、成员运算in和not in
    l=['a','b','c','d','e']
    print('a' in l) #True

    3.5、追加与insert
    l=['a','b','c','d','e']
    l.append('xxx')
    l.append('yyy')
    print(l)

    l.insert(0,'xxxx')
    print(l)  #['xxxx','a','b','c','d','e']

    3.6、删除
    l=['a','bbb','c','d','e']
    del是一种通用的删除操作,没有返回值
    del l[0]
    print(l)  #['bbb','c','d','e']

    dic={'k1':1}
    del dic['k1']
    print(dic)  #{}

    l.remove(指定要删除的那个元素),没有返回值
    res=l.remove('bbb')  
    l.remove('bbb')
    print(l) #['a','c','d','e']
    print(res) #None

    l.pop(指定要删除的那个元素的索引),返回刚刚删掉的那个元素
    l=['a','bbb','c','d','e']
    l.pop(-1)
    res=l.pop(1)
    print(l) #['a','bbb','c','d']
    print(res) #e

    3.7、循环
    l=['a','b','c','d','e']
    for item in l:
       print(item)

    练习:
    队列:先进先出
    l=[]
    # 入队
    l.append('first')
    l.append('second')
    l.append('third')
    print(l)
    # 出队
    print(l.pop(0))
    print(l.pop(0))
    print(l.pop(0))

    堆栈:后进先出

    需要掌握的操作
    l=['aaa','bb',345]
    l.clear()
    l.append([1,2,3])
    l.append('abc')
    l.append(5)
    print(l)
    l = []
    for i in [1,2,3]:
       l.append(i)
    print(l)
    l.extend('123')
    print(l)
    l.extend('abc')
    print(l)
    l.extend(['g','e','f'])  #本质就是for循环,所以后面跟上列表或者字符串是一样的
    print(l)

    # 将列表反转
    l = ['a','b'.'c']
    l.reverse()
    print (l)  #['c','b','a']

    # 只有在类中中所有元素都是同种类型的情况下才能用sort排序
    l=[1,3,2]
    l.sort(reverse=True)
    print(l)  #[3,2,1]

    l=['z','d','a']
    l.sort()
    print(l) #['a','b','z']

    4、总结

    存多个值

    有序

    可变

     

  • 相关阅读:
    [转]Convolution Neural Network (CNN) 原理与实现
    [转]深度学习CNN研究反向
    [转]一张图看懂:Google AlphaGo的原理、弱点
    [转]前馈型神经网络与反馈型神经网络的区别
    [转]认知机和神经认知机
    [转]技术向:一文读懂卷积神经网络CNN
    PHP 日期格式化 参数参考
    PHP MAIL DEMO(程序代码直接发送邮件)
    PHP上传文件DEMO
    PDO事务管理DEMO
  • 原文地址:https://www.cnblogs.com/chenwang-23/p/10659707.html
Copyright © 2011-2022 走看看