zoukankan      html  css  js  c++  java
  • Python数据类型补充1

    一、可变和不可变类型

    可变类型: 值变了,但是id没有变,证明没有生成新的值而是在改变原值,原值是可变类型

    不可变类型:值变了,id也跟着变,证明是生成了新的值而不是在改变原值,原值是不可变

    # x=10
    # print(id(x))
    # x=11
    # print(id(x))
    
    
    y=['a','b','c']
    print(id(y))
    y[0]='A'
    print(y)
    print(id(y))

    二、数字类型

    # 其他进制=>十进制
    # 十进制: 0-9
    # 11 = 1*10^1 + 1*10^0
    
    # 二进制: 0 1
    # 11 = 1*2^1 + 1*2^0
    
    # 八进制: 0-7
    # 11 = 1*8^1+1*8^0
    
    # 十六进制:0-9 A-F
    # 11 = 1*16^1+1*16^0
    
    # 十进制=>其他进制
    print(bin(13)) # 十进制=>二进制 ob1101

    print(oct(13)) # 十进制=>八进制 Oo15
    print(hex(13)) # 十进制=>十六进制 Oxd

    三、字符串

    # 可以将任意类型转换成字符串
    # str(1)
    # str(1.3)
    # x=str([1,2,3])
    # print(x,type(x))

    常用操作+内置的方法

    优先掌握的操作:

    1、按索引取值(正向取+反向取) :只能取
    msg='hello world'
    # print(msg[0])
    # print(msg[5])
    # print(msg[len(msg)-1])
    # print(msg[-1])
    # msg[0]='H'
    2、切片(顾头不顾尾,步长): 想要从一个大字符串中切出一个小字符串
    # msg='hello world'  
    # print(msg[0:5])
    # print(msg)
    # print(msg[0:5:2]) 
    3、长度len
    # msg='你好啊a'
    # print(len(msg))
    4、成员运算in和not in
    msg='yangyuanhu 老师是一个非常虎的老师'
    # print('yangyuanhu' in msg)
    # print('虎' not in msg)
    # print(not '虎' in msg)
    5、移除字符串左右两边的字符strip:默认去空格
    # pwd='       1 23        '
    # res=pwd.strip(' ')
    # print(res)
    
    # pwd=input('>>: ').strip() #pwd='123'
    #
    # if pwd == '123':
    #     print('密码输入正确')
    
    # pwd='******12*3****'
    # print(pwd.strip('*'))
    6、切分split:针对有规律字符串按照某个字符切成列表
    # info='yyhdsb|18|female'
    # li=info.split('|',1) #只按照第一个分隔符切
    # print(li)
    7、循环
    # msg='hello'
    #
    # for item in msg:
    #     print(item)

    Conclusion:

    存一个值
    
    有序
    
    不可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash)
    s1='hello'
    print(id(s1))
    s1='world'
    print(id(s1))
    
    
    需要掌握的操作(****)
    1、strip,lstrip,rstrip
    print('****egon***'.strip('*'))
    print('****egon***'.lstrip('*'))
    print('****egon***'.rstrip('*'))
    2、lower,upper
    print('AAAbbbb'.lower())
    print('AAAbbbb'.upper())
    
    3、startswith,endswith
    print('alex is sb'.startswith('alex'))
    print('alex is sb'.endswith('sb'))
    
    4、format的三种玩法
    print('my name is %s my age is %s' %('egon',18))
    print('my name is %s my age is %s' %(18,'egon'))
    
    print('my name is {name} my age is {age} '.format(age=18,name='egon'))
    
    print('my name is {} my age is {} '.format(18,'egon'))
    print('my name is {0} my age is {1} '.format(18,'egon'))
    print('my name is {1} my age is {0} '.format(18,'egon'))
    
    5、split,rsplit
    msg='a:b:c:d:e'
    print(msg.split(':',1))
    print(msg.rsplit(':',1))
    
    6、join
    msg='a:b:c:d:e'
    list1=msg.split(':')
    msg1=':'.join(list1)
    print(msg1)
    
    info='egon:123:male'
    list1=info.split(':')
    print(list1)
    
    print(':'.join(list1))
    
    7、replace
    msg='alex is alex alex is hahahaha'
    print(msg.replace('alex','SB',1))
    
    8、isdigit
    print('123'.isdigit()) # 只能判断纯数字的字符串
    print('12.3'.isdigit())
    
    age_of_db=30
    inp_age=input('>>>: ').strip()
    if inp_age.isdigit():
        inp_age=int(inp_age)
        if inp_age > age_of_db:
            print('too big')
        elif inp_age < age_of_db:
            print('too small')
        else:
            print('you got it')
    
    其他操作1、find,rfind,index,rindex,count
    msg='hello worldaa'
    print(msg.index('wo'))
    print(msg.index('wo',0,3))
    print(msg.find('wo',0,3))
    print(msg.find('xxxxxxx'))
    print(msg.index('xxxxxxx'))
    print(msg.count('l'))
    
    2、center,ljust,rjust,zfill
    name=input('>>: ').strip()
    print('egon'.center(50,'='))
    print(('%s' %name).center(50,'-'))
    
    print('egon'.ljust(50,'='))
    print('egon'.rjust(50,'='))
    print('egon'.zfill(50))
    
    3、expandtabs
    print('hello	world'.expandtabs(5))
    
    4、captalize,swapcase,title
    print('hello world'.capitalize())
    print('Hello world'.swapcase())
    print('Hello world'.title())
    
    5、is数字系列
    num1=b'4' #bytes
    num2=u'4' #unicode,python3中无需加u就是unicode
    num3='' #中文数字
    num4='' #罗马数字
    
    isdigit: bytes,str
    print(num1.isdigit())
    print(num2.isdigit())
    print(num3.isdigit())
    print(num4.isdigit())
    
    isdecimal:str
    print(num2.isdecimal())
    print(num3.isdecimal())
    print(num4.isdecimal())
    
    isnumberic:str,中文罗马
    print(num2.isnumeric())
    print(num3.isnumeric())
    print(num4.isnumeric())
    
    6、is其他
    print('aaasdfaA'.isalpha()) # 纯字母组成的字符串
    print('aaasdfaA123'.isalnum()) # 字母或数字组成
    print('aaasdfaA'.isalnum()) # 字母或数字组成
    Time the study pain is temporary,has not learned the pain is life-long.
  • 相关阅读:
    从Oracle提供两种cube产品说开
    Sql Server DWBI的几个学习资料
    Unload Oracle data into text file
    初学Java的几个tips
    我常用的Oracle知识点汇总
    benefits by using svn
    如何在windows上使用putty来显示远端linux的桌面
    building commercial website using Microsoft tech stack
    Understand Thread and Lock
    Update google calendar by sunbird
  • 原文地址:https://www.cnblogs.com/wanlei/p/9783933.html
Copyright © 2011-2022 走看看