zoukankan      html  css  js  c++  java
  • Python基础 之 数据类型

    数据类型

    一、运算符

    算数运算
    a = 10 * 10
    赋值运算
    a = a + 1  a+=1

      

    布尔值:True 真
        False 假

    if  True:
        pass
                    
    while True:
        pass                  
                    
    v = not Trure #False 

    成员运算

    a = "文" in "伊文尧"

    "伊文尧"   字符串
    ""   字符
    "伊文"   建文 子字符串,子序列
    # ctrl + ?     在Pychram中使用该操作进行整体注释
    name  = "伊文尧"
    
    if "伊文" in name:
        print('OK')
    else:
        print('Error')
    
    if "伊尧" not in name:
        print('1')
    else:
        print('2')
    
    #"伊尧"字符不在name中,print输出为1
    if "伊尧" not in name:
    if 1 == 1:
    if 2 > 2:
    # 以上三条均是真假布尔值
    关于真假布尔值
    v = 1 == 2
    print(v)
    #直接对真假进行输出

    比较运算
    a = 1 > 5
    逻辑运算
    a = 1 > 6 or 1==1

    user = "root"
    pwd = "root!23"
    
    v = user == "root" and pwd == "root!23" or 1 == 1 and pwd == "99854" and 1==2
    print(v)
    
    #先计算括号内的
    #遇到如上情况,从前到后执行
    #结果True,后面还有or,直接为True
    #如果True,后面还有and,则继续运算
    #如果False,后面还有or,则继续运算
    #如果False,后面还有and,直接为False

    二、基本数据类型

    数字 int 
    a1 = 123
    a1 = 456

    将字符串转换为数字

    a = "123"
    print(type(a),a)    #输出一个值,并表示类型
    
    b = int(a)
    print(type(b),b)    #输出一个值,并表示类型
    
    num = "B" 
    v = int(num, base=16)
    print(v)
    
    #<class 'str'> 123
    #<class 'int'> 123
    #11
    转换

    capitalize

    首字母大写

    test = "evEn"
    v = test.capitalize()     
    print(v)
    #Even
    capitalize

    casefold 和 lower

    casefold使用功能更强大,很多未知的语言对应关系也可以变小写

    test = "evEn"
    v1 = test.casefold()
    print(v1)
    v2 = test.lower()
    print(v2)
    #even
    #even
    casefold 和 lower

    swapcase

    将小写转成大写,大写转成小写

    test = "eVen"
    v = test.swapcase()
    print(v)
    #EvEN
    swapcase

    center 和 just

    设置宽度 center

    test = "even"
    v = test.center(20,"=")
    # 20 代指总长度
    # = 代表填充,只能填一个字符,可有可无
    print(v)
    #========evEn========
    center

    填充功能 ljust

    test = "even"
    v = test.ljust(20,"=")  #把内容放置到左侧,右侧为填充
    print(v)
    #even================
    ljust

    填充功能 rjust

    test = "even"
    v = test.rjust(20,"=")  #把内容放置到右侧,左侧为填充
    print(v)
    #================even
    rjust

    count

    去字符串中寻找,寻找子序列的出现次数

    test = "evEnevenr"
    v = test.count('ev')
    print(v)
    #2
    
    test = "evEnevenr"
    v = test.count('ev',5,6)
    # 从5开始,直至6结束,在这之间寻找子序列的出现次数
    print(v)
    #0
    count

    encode 和 decode  暂不讨论  

    expandtabs

    断句,制表

    test = "username	email	password
    root	root@!23	123
    root	root@!23	123
    root	root@!23	123"
    v = test.expandtabs(20)  
    #test数字符20个,不足则补全20个
    print(v)
    #username            email               password
    #root                root@!23            123
    #root                root@!23            123
    #root                root@!23            123

    find

    从开始往后找,找到第一个之后,获取其位置

    test = "evenEven"
    # 未找到 -1
    v = test.find('en')
    print(v)
    #2

    format

    格式化,将一个字符串中的占位符{ }替换为指定的值

    test = 'i am {name}, age {a}'
    print(test) 
    v = test.format(name='even',a=20)
    print(v)
    #i am {name}, age {a}
    #i am even, age 20
    format

    按照位置替换为指定的值

    test = 'i am {0}, age {1}'
    print(test)
    v = test.format('even',20)
    print(v)
    #i am {name}, age {a}
    #i am even, age 20
    format

    格式化,传入的值 {"name": 'alex', "a": 19},字典相关

    test = 'i am {name}, age {a}'
    v1 = test.format(name='Q',a=10)
    v12 = test.format_map({"name": 'even', "a": 20})
    print(v1)
    print(v2)
    #i am Q, age 10
    #i am even, age 20
    format_map

    判断类型

    isalnum

    判断字符串中是否只包含 字母和数字

    test = "123"
    v = test.isalnum()
    print(v)
    #True
    isalnum

    isalpha

    判断是字母或汉字

    test1 = "as2df"
    test2 = "asdf"
    v1 = test1.isalpha()
    v2 = test2.isalpha()
    print(v1)
    print(v2)            
    #False
    #True
    isalpha

    isdecimal  isdigit  isnumeric

    判断当前输入是否是数字

    test = "" # 1,②
    v1 = test.isdecimal() #常用
    v2 = test.isdigit()
    v3 = test.isnumeric()
    print(v1,v2,v3)
    #False False True
    isdecimal

    isprintable

    判断是否存在不可显示的字符

    # 	   制表符
    # 
       换行
    test = "oiuas	dfkj"
    v = test.isprintable()
    print(v)
    #False
    isprintable

    isspace

    判断是否全部是空格

    test1 = ""
    test2 = "eve n"
    test3 = " "
    v1 = test1.isspace()
    v2 = test2.isspace()
    v3 = test3.isspace()
    print(v1,v2,v3)
    #False False True
    isspace

    istitle 和 title

    istitle判断是否是标题(每个单词首字母大写),title负责将其转换成标题

    test = "Even Is A good boy!"
    v1 = test.istitle()
    print(v1)
    v2 = test.title()
    print(v2)
    v3 = v2.istitle()
    print(v3)
    #False
    #Even Is A Good Boy!
    #True
    istitle title

    islower isupper 和 lower upper

    islower lower 判断是否全部是小写 和 全部转换小写

    test = "Even"
    v1 = test.islower()
    v2 = test.lower()
    print(v1, v2)
    #False even

    isupper upper 判断是否全部是大写 和 全部转换大写

    test = "Even"
    v1 = test.isupper()
    v2 = test.upper()
    print(v1,v2)
    #False EVEN

    startswith 和 endswith

    判断是否以xxx开头,以xxx结尾

    test = "ip 127.0.0.1"
    v1 = test.startswith('i')
    v2 = test.endswith('255')
    print(v1)
    print(v2)
    #True
    #False
    startswith endswith

    isidentifier

    判断是否是有效标识符

    a = "def"
    b = "123"
    v1 = a.isidentifier()
    v2 = b.isidentifier()
    print(v1,v2)
    #True False
    isidentifier

    join

    将字符串中的每一个元素按照指定分隔符进行拼接

    test = "伊文尧"
    print(test)
    v1 = ' '.join(test)
    v2 = "_".join(test)
    print(v1)
    print(v2)
    #伊文尧
    #伊 文 尧
    #伊_文_尧

    strip  lstrip  rstrip

    移除空白字符   (除此之外还能去除 )

    test = " even " 
    v1 = test.lstrip()    #去除左空白
    v2 = test.rstrip()    #去除右空白
    v3 = test.strip()        #去除左右空白        
    print(v1)
    print(v2)
    print(v3)
    #even 
    # even
    #even

    移除指定字符串(优先最多匹配)

    test = "even"
    v1 = test.lstrip('ev')
    v2 = test.rstrip('mnvzev')
    v3 = test.strip('ev')
    print(v1)
    print(v2)
    print(v3)
    #n
    #
    #n

    maketrans 和 translate

    构建对应关系 和 替换

    test =  "aeiou"
    test1 = "12345"
    
    v = "asidufkasd"
    m = str.maketrans("aeiou", "12345")    #构建对应关系
    new_v = v.translate(m)                #根据对应关系进行替换
    print(new_v)
    #1s3d5fk1sd
    maketrans translate

    分割

    partition  rpartition

    分割为三部分

    test = "testasdsddfg"
    v = test.partition('s')    #按照s,做分割
    print(v)
    v = test.rpartition('s')    #按照s,从右开始做分割
    print(v)
    ('te', 's', 'tasdsddfg')
    ('testasd', 's', 'ddfg')

    split  rsplit

    分割为指定个数 但匹配值会丢失 

    test = "testasdsddfg"
    v = test.split('s',2)        #按照s,做两刀分割
    print(v)
    v = test.rsplit('s',2)    #按照s,从右做两刀分割
    print(v)
    #['te', 'ta', 'dsddfg']
    #['testa', 'd', 'ddfg']

    splitlines

    只能根据True,False:选择是否保留换行

    test = "abc
    abc
    abc"
    v = test.splitlines(False)
    print(v)
    #['abc', 'abc', 'abc']

    replace

    将指定字符串替换为指定字符串

    test = "rootrootroot"
    v = test.replace("oo",'xx')    #将oo替换成xx
    print(v)
    #rxxtrxxtrxxt
    
    v = test.replace("oo",'xx',2)    #将oo替换成xx,只限于前2个
    print(v)
    #rxxtrxxtroot

    总而言之

    7个 十分重要的基本数据类型

    # join  
    # split
    # find
    # strip
    # upper
    # lower
    # replace

    三、其他补充

    1.索引

    获取字符串中的某一个字符,0为第一个字符

    test = "even"
    v = test[2]    #获取第3个字符
    print(v)
    #e

    2.切片

    获取字符串中的范围

    test = "evenyao"
    v = test[1:-1]    #获取第2个字符至倒数第2个字符中的所有字符
    print(v)
    #venya

    3.获取长度

    Python3: len获取当前字符串中由几个字符组成

    test = "even"
    v = len(test)
    print(v)
    #4

    4.for循环

    for 变量名 in 字符串:
    一样支持break 和 continue

    使用while循环对一条语句进行纵向单个输出

    test = "今天天气不错"
    index = 0
    while index < len(test):    #len获取长度
        v = test[index]        #获取索引
        print(v)
        index = index + 1
    print('=======')
    #
    #
    #
    #
    #
    #
    #=======

    使用for循环进行一样的实现

    test = "今天天气不错"
    for item in test:
        print(item)
    print('=======')
    #
    #
    #
    #
    #
    #
    #=======

    for中的break和continue

    test = "今天天气不错"
    for item in test:
        print(item)
        break
    #
    #
    break
    test = "今天天气不错"
    for item in test:
        continue
        print(item)
    #
    continue

    5.获取连续或不连续的数字

    Python2中立即创建在内容中
    Python3中只有for循环时,才一个一个创建

    r1 = range(10)
    r2 = range(1,10)
    r3 = range(1,10,2)
    #帮助创建连续的数字,通过设置步长来指定不连续

    设置步长为5,并输出0-99的数字

    v = range(0, 100, 5)    #0-100连续输出,设置步长为5
    
    for item in v:
        print(item)
    # 0
    # 5
    # 10
    # 15
    # 20
    # 25
    # 30
    # 35
    # 40
    # 45
    # 50
    # 55
    # 60
    # 65
    # 70
    # 75
    # 80
    # 85
    # 90
    # 95
    range(0,100,5)
    作者:evenyao
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    meego API
    linux的文件cache导致写文件消耗大量内存
    系统内存不断消耗 导致系统停滞(表面像死机) 但又找不到内存泄漏点
    C常用的LinuxC语言函数库
    GUI
    java 集合类结构图
    接口到底是个什么玩意
    抽象类到底是个什么玩意
    异常
    IO流
  • 原文地址:https://www.cnblogs.com/evenyao/p/9140092.html
Copyright © 2011-2022 走看看