zoukankan      html  css  js  c++  java
  • 数据类型

    #数据类型

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

    e.g. x = 10
    print(id(x))
    x = 11
    print(id(x))

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

    e.g. y = ['a','b','c']
    print(id(y))
    y[0] = 'A' 原列表中的'a' 改为 'A'
    print(y)
    print(id(y))
    '''



    #数字类型

    #一:整型int
    '''
    =======基本使用=======

    1、用途: 记录年龄等级各种号码
    2、定义方式: age=18 # age=int(18)

    x=int('123') #只能将纯数字的字符串转换成整型
    print(type(x))
    print(int(3.7)) #int(3.7) 3 只取整数部分 不会四舍五入

    3、常用操作+内置的方法:赋值比较算术

    =======该类型总结=======

    存一个值

    不可变 == 可hash
    1、可变:值变,id不变。可变==不可hash
    2、不可变:值变,id就变。不可变==可hash

    print(hash(10))
    print(hash([1,2,3])) list可变 == 不可hash

    '''

    #二:浮点型float
    '''
    =======基本使用=======

    1、用途: 记录身高体重薪资
    2、定义方式:salary=1.3 #salary=float(1.3)

    x=float('3.1')
    print(x,type(x))

    3、常用操作+内置的方法:赋值比较算术

    =======该类型总结=======

    存一个值

    不可变 == 可hash
    1、可变:值变,id不变。可变==不可hash
    2、不可变:值变,id就变。不可变==可hash

    x=3.1
    print(id(x))
    x=3.2
    print(id(x))

    '''



    # 了解:
    # 复数
    # x=1-2j
    # print(x,type(x))
    # print(x.real)
    # print(x.imag)

    # 长整型 在python2中有long类型 python3中没有long类型


    # 其他进制=>十进制
    # 十进制: 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)) # 十进制=>二进制
    # print(oct(13)) # 十进制=>八进制
    # print(hex(13)) # 十进制=>十六进制

    #字符串
    '''
    作用:描述性信息
    定义:在 单引号、双引号、三引号 内,有一串字符组成
    e.g. name = 'egon'

    # ======该类型总结======
    存一个值

    有序

    不可变 == 可hash
    1、可变:值变,id不变。可变==不可hash
    2、不可变:值变,id就变。不可变==可hash

    s1='hello'
    print(id(s1))
    s1='world'
    print(id(s1))

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

    # 优先掌握的操作
    # 按索引取值(正向取+反向取):只能取
    msg = 'hello world'
    print(msg[0]) # h
    print(msg[5]) # ' '
    print(msg[-1]) # d 反向取
    msg[0] = 'H' # error 不能改

    # 切片(顾头,不顾尾巴,步长):想要从一个大字符串中切出一个小字符串
    msg = 'hello world'
    print(msg[0:5]) #hello
    print(msg) #hello world
    print(msg[0:5:2]) #hlo

    #了解
    print(msg[-1:-5:1]) #-1+1=0
    print(msg[-1:-5:-1]) #dlro
    print(msg[0:5:1]) #hello

    # 长度len
    msg = '你好啊a'
    print(len(msg)) # 4

    # 成员运算in和not in
    msg = 'hello world'
    print('hello' in msg)
    print('world' not in msg) 采用这种
    print(not 'world' in msg)

    1、strip,lstrip,rstrip

    移除字符串左右两边的字符strip:默认去空格
    name = '*egon**'

    print(name.strip('*')) 去除name中左右两边的*
    print(name.lstrip('*')) 去除name左边的所有*
    print(name.rstrip('*')) 去除name右边的所有*

    e.g.
    pwd=' 1 23 '
    res=pwd.strip(' ')
    print(res) #1 23

    pwd=input('>>: ').strip() #>>: 123
    pwd = '123'

    if pwd == '123':
    print('密码输入正确')

    pwd='******12*3****'
    print(pwd.strip('*')) #12*3

    pwd='****/&^**123**^*/*&'
    print(pwd.strip('*/&^')) #123
    print(pwd.strip('*/&')) #^**123**^

    2、lower,upper

    name = 'Egon'

    print(name.lower()) 大写字母小写 egon
    print(name.upper()) 小写字母大写 EGON

    3、startswith,endswith

    通常用来判断文件类型
    name = 'alex_SB'
    (str,顾头,不顾尾)
    print(name.endswith('SB')) 后缀倒数是否有SB 返回T/F
    print(name.endswith('S',3,6)) ('str',起始,结尾)
    print(name.startswith('alex')) 检查某几个字符开始

    4、format的三种玩法

    res='{} {} {}'.format('egon',18,'male') egon 18 male
    res='{1} {0} {1}'.format('egon',18,'male') 18 egon 18

    res='{name} {age} {sex}'.format(sex='male',name='egon',age=18)
    egon 18 male

    5、split,rsplit

    split()通过指定分隔符对字符串进行切片,
    如果参数num 有指定值,则仅分隔 num 个子字符串

    name='root:x:0:0::/root:/bin/bash'
    print(name.split(':')) 以:为基础分割字符串
    #['root', 'x', '0', '0', '', '/root', '/bin/bash']

    name='C:/a/b/c/d.txt' #只想拿到原始目录
    print(name.split('/',1))
    #['C:', 'a/b/c/d.txt']

    name='a|b|c'
    print(name.rsplit('|',1)) #从右开始切分
    #['a|b', 'c']

    6、join

    join()方法用于将序列中的元素以指定的字符连接生成一个新的字符串。
    #可迭代对象必须都是字符串
    (str.join())
    s1 = "-"
    s2 = ""
    seq = ("r", "u", "n", "o", "o", "b") # 字符串序列
    print (s1.join( seq ))
    print (s2.join( seq ))

    r-u-n-o-o-b
    runoob

    7、replace

    Python replace() 方法把字符串中的 old(旧字符串)
    替换成 new(新字符串),
    如果指定第三个参数max,则替换不超过 max 次
    str.replace(old, new, [max])

    name = 'alex say : i have one tesla , my name is alex'
    print(name.replace('alex','SB',1))

    SB say : i have one tesla, my name is alex

    8、isdigit

    isdigit() 方法检测字符串是否只由数字组成
    返回值True or False

    str = "123456";
    print (str.isdigit())

    str = "Runoob example....wow!!!"
    print (str.isdigit())

    True
    False

    9、循环
    msg = 'hello'

    for item in msg:
    print(item)

    # h
    e
    l
    l
    o

    10、了解

    1、find,rfind,index,rindex,count
    msg='hello worldaa'
    print(msg.index('w')) #6
    print(msg.index('wo')) #6 msg中找wo在哪个位置
    print(msg.index('wo',0,3)) #error
    print(msg.find('wo',0,3)) #error == -1

    print(msg.count('l')) #统计在msg中有几个l

    2、center,ljust,rjust,zfill
    name=input('>>: ').strip()
    print('egon'.center(50,'=')) #在 ==居中打印==
    print(('%s' %name).center(50,'-'))

    print('egon'.ljust(50,'='))
    # egon==============================================
    print('egon'.rjust(50,'='))
    # ==============================================egon
    print('egon'.zfill(50))默认用0填充
    # 0000000000000000000000000000000000000000000000egon

    3、expandtabs
    print('hello world'.expandtabs(5))
    # hello world 5个空

    4、captalize,swapcase,title
    print('hello world'.capitalize())
    # Hello world
    print('Hello world'.swapcase())
    # hELLO WORLD
    print('Hello world'.title())
    # Hello World

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

    isdigit: bytes,str
    print(num1.isdigit()) T
    print(num2.isdigit()) T
    print(num3.isdigit()) F
    print(num4.isdigit()) F

    isdecimal:str
    print(num2.isdecimal()) T
    print(num3.isdecimal()) F
    print(num4.isdecimal()) F

    isnumberic:str,中文罗马
    print(num2.isnumeric()) T
    print(num3.isnumeric()) T
    print(num4.isnumeric()) T

    6、is其他
    print('aaasdfaA'.isalpha()) # 纯字母组成的字符串
    print('aaasdfaA123'.isalnum()) # 字母或数字组成
    print(' '.isspace()) #True
    print(' 12'.isspace()) #False 纯空格
    '''

    #列表
    '''
    ======基本使用======

    1、用途:记录多个值,比如人的多个爱好
    2、定义方式: 在[]内用逗号分隔开多个任意类型的值
    li=[1,2,3] # li=list([1,2,3])

    x=list('hello')
    #['h', 'e', 'l', 'l', 'o']
    x=list({'a':1,'b':2,'c':3}) 读key
    #['a', 'b', 'c']

    3、常用操作+内置的方法
    优先掌握的操作:
    1、按索引存取值(正向存取+反向存取):即可存也可以取
    li=['a','b','c','d']
    print(li[-1]) #d
    li[-1]='D' #['a','b','c','D']

    li[4]='e' #应该用追加
    del li[0] #['b', 'c', 'D']

    2、切片(顾头不顾尾,步长)
    li=['a','b','c','d']
    print(li[0:3]) #['a', 'b', 'c']

    3、长度
    print(len(li)) #4

    4、成员运算in和not in
    users=['egon','lxx','yxx','cxxx',[1,2,3]]
    print('lxx' in users) #T
    print([1,2,3] in users) #T
    print(1 in users) #F
    print(1 not in users) #T

    5、追加
    li=['a','b','c','d']
    print(id(li))
    li.append('e')
    li.append([1,2,3])
    #['a', 'b', 'c', 'd', 'e', [1, 2, 3]]
    值改变 id不变 列表可变 == 不可以hash

    6、删除
    li=['a','b','c','d']
    按照元素值去单纯地删除某个元素
    del li[1] #['a', 'c', 'd']
    li.remove('c') #['a', 'b', 'd']

    按照元素的索引去删除某个元素并且拿到该元素作为返回值
    res=li.pop(1)
    print(li) #['a', 'c', 'd']
    print(res) #'b'

    7、循环
    li=['a','b','c','d']
    for item in li:
    print(item)
    # a
    b
    c
    d


    # ======该类型总结======

    存多个值

    有序

    可变 == 不可hash
    1、可变:值变,id不变。可变==不可hash
    2、不可变:值变,id就变。不可变==可hash

    print(hash([1,2,3])) #不可hash

    # 需要掌握的操作
    li=['a','b','c','d','c','e']
    print(li.count('c')) #2

    li.extend([1,2,3])
    #['a', 'b', 'c', 'd', 'c', 'e', 1, 2, 3]

    print(li.index('z')) #error
    print(li.index('b')) #1
    print(li.index('d',0,3)) #error 顾头不顾尾

    li.insert(1,'egon')
    #['a', 'egon', 'b', 'c', 'd', 'c', 'e', 1, 2, 3]

    li=[3,1,9,11]
    li.reverse(无参数) #[11, 9, 1, 3]
    li.sort() #[1, 3, 9, 11]
    li.sort(reverse=True)#[11, 9, 3, 1]


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

    # 堆栈: 先进后出
    q=[]
    # 入栈
    q.append('first')
    q.append('second')
    q.append('third')
    # 出栈
    print(q.pop(-1))
    print(q.pop(-1))
    print(q.pop(-1))

    '''
  • 相关阅读:
    stl rope
    vijos1574 摇钱树
    图论 Dijkstra+堆优化
    c++输入优化
    Vijos1579 宿命的PSS 最小生成树
    快速求n阶多项式乘积
    c++stl map
    C#函数式程序设计之惰性列表工具——迭代器
    C#函数式程序设计之泛型(下)
    C#函数式程序设计之泛型(上)
  • 原文地址:https://www.cnblogs.com/OutOfControl/p/9663402.html
Copyright © 2011-2022 走看看