zoukankan      html  css  js  c++  java
  • python的第三天(数据类型:int/str/bool/lisrt(列表)/tuple(元祖)/dict(字典))

    算数运算符
    +---------------------加号
    - --------------------减号
    *----------------------乘号
    /-----------------------除号,对于2.7和3.7的版本对于除号有所不同对至2.7的版本不会精确比如9/2=4.0而对于3.7的版本就会等于4.5那当然2.7也有办法就是加入模块
    %----------------------取余
    **----------------------几次方
    //----------------------除法取整
    比较运算符
    >------------------大于号
    <-------------------小于号
    ==------------------等于号
    !=-------------------不等于号
    <>------------------不等于号和!=是一样的只是写法不一样
    <=------------------小于等于号
    >=------------------大于等于号
    赋值运算符
    =--------------------赋值
    +=-------------------相当于加上某个数例如 a +=1 (a =a+1)
    -=
    *=
    /=
    %=
    //=
    **=
    逻辑运算符
    and--------------------并且的意思两过的表达式都满足才是真
    or----------------------或的意思再过只要有一个满足就是真
    not---------------------非的意思
    成员运算付
    in----------------------在指定的序列中查找如果有就返回真
    not in------------------在指定的序列中查找没有就返回真
    数据类型
    1数字 int
    2字符串 str
    3布尔值 bool
    4列表 lisrt
    5元祖 tuple
    6字典 dict

    类与对象

    对象是在类的下面的,比如向上面的int,str,bool,字典等等就是类,而我们所创建一些变量他们就是对象。类里面有很多的功能,当我们在这个类的正面创建了对象那么这个对象就会拥有这个类的所有的功能。

    可以这么抽象的理解:比如说我是一个工人,那么我是一个木工,那么我是属于木工这一类的。对于木工他单独有一个房间,这个房间时都是木工使用的工具。那我每次出去干活不可能把所有的工具都带上对吧,只带我这次工作所用到的对吧,如果我到了地方我还需要别的工具那我在回来拿就好了。这里的我就是对象,房间就相当于类

    查看对象的类,或对象所具备的功能
    1、通过type来看对象属于那个类
    temp = "cui"
    temp_new = type(temp)
    2、通过dir可以查看类那些功能
    print (dir(temp))
    3、能过help也可以查看类那些功能,详细些
    help(type(temp))
    4、可以通过Ctrl+左键可以定位到使用的某些功能,把鼠标放到使用的参数上

    一些常用的类的功能
    1、整形:int
    eg.
    n1 = 123
    n2 = 456
    print (n1 + n2)
    print (n1.__add__(n2) )


    eg.获取可表示的二进制最短的位数
    n1 = 4
    bin(n1)-----------------自动转换面二进制
    n1.bit_length()--------可以查看可以最少几位来表示这个数

    2、字符型

    eg            :capitalize() ------------------------------------------返回时把首字母改为大写

    举例:
    temp = "cui"
    temp_new = temp.capitalize()
    print(temp_new)


    输出结果:Cui yan cheng

    eg          :def title(self):--------------------------------------把字符串中所有单词的首字母转为大写

    举例:
    temp = "cui yan cheng"
    temp_new = temp.capitalize()
    temp_12 = temp.title()
    print(temp_new)
    print(temp_12)

    输出结果:Cui yan cheng
                      Cui Yan Cheng

    eg             casefold ()           ---------------------------将字符串中的所有的大写字母转换成小写字母返回

    举例:
    temp = "CUI"
    temp_new = temp.casefold ()
    print(temp_new)


    输出结果:cui

    eg:              center(self, width, fillchar=None)-------------------------------返回指定宽度并居中

    举例:
    temp = "cui"
    print(temp.center(20,"*"))


    输出结果:********cui*********

    eg:     def count(self, sub, start=None, end=None)----------------------------查找字符,并返回个数,对于end这个值代表小于而不等于,对于start代表大于等于#

    举例:
    temp = "cui yan cheng cui"
    temp_new = temp.count("ccc")
    temp_12 = temp.count("cui",0,3)
    print(temp_12)
    print(temp_new)


    输出结果:0
                     1


    eg:           def encode(self, encoding='utf-8', errors='strict'):------------------用来对定程序的内容进行解码,对于GBK与utf-8来说utf-8用的更多一些

    举例:

    temp = "不开心"
    print(temp)
    temp_new = temp.encode(encoding='utf-8')
    temp_gbk = temp.encode(encoding='gbk')
    print(temp_new)
    print(temp_gbk)


    输出结果:不开心
    b'xe4xb8x8dxe5xbcx80xe5xbfx83'
    b'xb2xbbxbfxaaxd0xc4'

    eg:        def endswith(self, suffix, start=None, end=None):---------------指定查找字符串的后缀如果查找相同返回True,否则返回False

    举例:
    temp = "cui yan cheng"
    temp_new = temp.endswith("enge")
    print(temp_new)
    temp_12 = temp.endswith("i",0,3)
    print(temp_12)


    输出结果:False
                      True

    eg :           def startswith(self, prefix, start=None, end=None):----------------------------与endswith的用法相同只不过是查找前缀

    举例:
    temp = "cui yan cheng"
    temp_new = temp.startswith("cui")
    print(temp_new)
    temp_12 = temp.startswith("i",0,3)
    print(temp_12)


    输出结果:True
                      False

    eg :              def expandtabs(self, tabsize=8)--------------------------------将字符串的tab键默认转换为8个空格,当然这个数也可以自行设定

    举例:
    temp = "cui yang"
    print(temp)
    temp_new = temp.expandtabs(20)
    print(temp_new)


    输出结果:cui yang
                      cui yang

    eg:             def find(self, sub, start=None, end=None)----从字符串中查看字符的或者字符串的位置并且返回并且只返回第一个查到的,如果没有返回—1

    举例
    temp = "cui yan cheng"
    temp_new = temp.find("cef")
    print(temp_new)
    temp_12 = temp.find("y",3,6)------------------当然也可以先指定范围
    print(temp_12)


    输出结果:-1
                      4

    eg              :def format(self, *args, **kwargs)------------------      可以用来替换{and}内的值

    举例
    temp = "cui,{0},yan,{1}"
    print(temp)
    temp_new = temp.format("cheng","abc")
    print(temp_new)


    输出结果:cui,{0},yan,{1}
                      cui,cheng,yan,abc

    eg:     def format_map(self, mapping)---------到字典的时候在看


    eg:       def index(self, sub, start=None, end=None)--------与find的功能是一样的,不一样的是如果他查不到的话他会报错用法也是一样的


    eg:       def isalnum(self):----------------------------查找字符串中是否有数字或者字母如果只有数字和字母那么返回Ture否则返回False

    举例:
    temp = "adfe123"
    temp_new = temp.isalnum()
    print(temp_new)


    输出结果:True

    eg:        def isalpha(self):-----------------------------查找字符串中是否只有字母如果只有字母则返回True否则返回False

    举例:
    temp = "rainsir123"
    temp_new = temp.isalpha()
    print(temp_new)


    输出结果:False

    eg:         def isdecimal(self)------------------------------查找字符串中是否只有十进制的字符如果只有则返回True,否则返回False

    举例:

    temp = "123"
    temp_new = temp.isdecimal()
    print(temp_new)


    输出结果:True

    eg:def isdigit(self):--------------------------------查找字符串中是否只有数字如果只有则返回True,否则返回False

    举例:
    temp = "0619"
    temp_new = temp.isdigit()
    print(temp_new)


    输出结果:True

    eg;def isidentifier(self)-------------------没弄清楚

    eg:def islower(self)-------------------------------------判断字符串中的字母是否都是小写,如果都是小写返回True否则返回False

    举例:
    temp = "Rain.sir"
    temp_new = temp.islower()
    print(temp_new)


    输出结果:False

    eg:def isnumeric(self):----------------------------------这个的用法与isdigit的用法是一样的功能也是一样

    eg:def isprintable(self):--------------------------------------如果字符串中有不可打印的字符则返回False,否则返回True

    eg: def isspace(self):--------------------------------如果字符串中只有空格则返回True,否则返回False

    举例:
    temp = " "
    temp_new = temp.isspace()
    print(temp_new)


    输出结果:True

    eg:def istitle(self):------------------------------判断字符串中所有的单词的首字母是否都是大写如果都是大写则返回Ture否则返回Flase

    举例:
    temp = "Cui Yan Ch123Eng"
    top = "cui yan Cheng"
    print(temp.istitle())
    print(top.istitle())


    输出结果:True
                      False

    eg:def isupper(self)--------------------------------判断字符串所有字母是否都是大写如果是返回True,否则返回False

    举例:
    temp = "CYC3"
    top = "CYC3c"
    print(temp.isupper())
    print(top.isupper())


    输出结果:True
                     False

    eg:def join(self, iterable)-------------------------------连接把元素之间用某种字符连接起来

    举例:
    temp = ["cui","yan","cheng"]
    temp_new = "+".join(temp)
    print(temp_new)


    输出结果:cui+yan+cheng

    eg:def ljust(self, width, fillchar=None)-------------------------指定字符串的宽度,左对齐,默认填充为空格

    举例:
    temp = "cuiyancheng"
    temp_new = temp.ljust(30,'*')
    print(temp_new)


    输出结果:cuiyancheng*******************

    eg:def lower(self):-------------------------------------------------把字符串中所有的字母都转换为小写子母

    举例:
    temp = "CuiYanCheng123"
    temp_new = temp.lower()
    print(temp_new)


    输出结果:cuiyancheng123

    eg:def lstrip(self, chars=None)----------------------------默认移除字符中左边的空客,当然也可以指定移除的字符

    举例:
    rstrip-------------------------移除右边
    strip--------------------------移除两边
    temp = " cui yan* "
    temp_new = temp.lstrip()
    print(temp_new)


    eg:def maketrans(self, *args, **kwargs)----------------------------没弄清楚

    eg:def partition(self, sep):---------------------------------------------------给字符串进行分段,给一个判断符号,但不会把判断符替换掉

    举例:
    temp = "http://www.baidu.com"
    temp_new = temp.partition('/')
    print(temp_new)


    输出结果:('http:', '/', '/www.baidu.com')

    eg:def replace(self, old, new, count=None)------------------------------替换字符串某些字符,默认是都从左住右依次替换也可以指定替换的次数

    举例:
    temp = "www.baidu.cwom"
    temp_new = temp.replace('w','s')
    temp_12 = temp.replace('w','s',1)
    print(temp_new)
    print(temp_12)


    输出结果:sss.baidu.csom
                      sww.baidu.cwom


    eg:def rfind(self, sub, start=None, end=None):-------------------------从字符串中查找字符的位置,并且返回。如果没有返回-1与find的用法一样区别是他是从右往左

    eg:def rindex(self, sub, start=None, end=None):-----------------------与index的用法一样,为一的区别是从右往左查找,一样如果查不到返回报错

    eg:def rjust(self, width, fillchar=None)--------------------------------指定宽度,默认右对齐,默认填充为空格

    eg:def rpartition(self, sep):--------------------------------------------------与Rpartition一样不过就是从右往左开始

    举例:
    temp = "http://www.baidu.com"
    temp_new = temp.rpartition('/')
    print(temp_new)


    输出结果:('http:/', '/', 'www.baidu.com')


    eg:def rsplit(self, sep=None, maxsplit=-1):---------------------把字符串分段,与partition不同的是他会把判断符给去掉rsplit从右往左开始,默认是只要查到就分段,也可以指定次数

    举例:
    temp = "http://www.baidu.com"
    temp_new = temp.rsplit("/")
    print(temp_new)
    print(temp_new[2])


    输出结果:['http:', '', 'www.baidu.com']
                      www.baidu.com


    eg;def split(self, sep=None, maxsplit=-1):-----------------------与rsplit一样只不过是从左住右

    eg:def splitlines(self, keepends=None):------------------------------会在分行符那时进行分段并且会去掉分行符

    举例:

    temp = "http:/ www.baidu.com cv"
    temp_new = temp.splitlines()
    print(temp_new)


    输出结果:['http:/', 'www.baidu.com', 'cv']

    eg:def swapcase(self): -----------------字符串中大写改小写,小写改大写

    举例:
    temp = 'cui YAN chenG'
    temp_new = temp.swapcase()
    print(temp_new)


    输出结果:CUI yan CHENg


    eg:def translate(self, table):------------------------------没有看懂

    eg:def upper(self):-----------------------------------------------将所有字符改为大写字母

    举例:
    temp = "cui Yan cheng"
    temp_new = temp.upper()
    print(temp_new)


    输出结果:CUI YAN CHENG

    eg:   def zfill(self, width):--------------------------------------指定宽度,右对齐,只能用0来填充

    举例:
    temp = "rain.sir"
    temp_new = temp.zfill(20)
    print(temp_new)


    输出结果:000000000000rain.sir


    索引与切片
    temp = "cuiyancheng"
    print(temp[0])
    print(temp[0:2])----------切片输出temp[0],temp[1]
    for i in temp:-------------一个for的循环
    print(i)
    print(len(temp))------------查看字符串的长度


    变量名.upper()--------------------将字符串中的所有的小写自动转换成大写
    temp = "key"
    print (key)
    temp_new = temp.upper()
    print (temp_new)

    3、列表
    temp_list = ['cui','yang','cheng']-----------------这就是一个列表
    一些常用的功能:
    eg:append(self, p_object)------------------------------可以在列表后面追加字符串
    举例:
    temp_list = ['cui','yan','cheng']
    temp_list.append('cui')
    print(temp_list)
    输出结果:['cui', 'yan', 'cheng', 'cui']

    eg:clear(self):------------------------------------------------清空列表里的所有的内容
    举例:
    temp_list = ['cui','yan','cheng']
    temp_list.clear()
    print(temp_list)
    输出结果:[]

    eg:copy(self)------------------------------------------------------------不是很明白我感觉就是一个复制

    eg:count(self, value)-----------------------------------------------------查找列表中某个元素的个数
    举例:
    temp_list = ['cui','yang','cui']
    temp_12 = temp_list.count('cui')
    print(temp_12)
    输出结果:2

    eg:extend(self, iterable)----------------------------------------------在列表时添加列表元素与append是不同以下面为列
    举例:
    temp_list = ['cui','yang','cui']
    temp_12 = ['cheng','rou']
    temp_list.extend(temp_12)
    printt(emp_list)
    输出结果:['cui', 'yang', 'cui', 'cheng', 'rou']
    举例:
    temp_list = ['cui','yang','cui']
    temp_12 = ['cheng','rou']
    temp_list.append(temp_12)
    print(temp_list)
    输出结果:['cui', 'yang', 'cui', ['cheng', 'rou']]

    eg:index(self, value, start=None, stop=None)-----------------------------与之前的字符串的用法是一样的,查找某个值并返回位置,如果没有返回0
    举例:
    temp_list = ['cui','yang','cui']
    print(temp_list.index('cui',1,3))
    输出结果:2

    eg:insert(self, index, p_object)----------------------------------------------------在列表中插入某个元素
    举例:
    temp_list = ['cui','yang','cui']
    temp_list.insert(1,'cheng')
    print(temp_list)
    输出结果:['cui', 'cheng', 'yang', 'cui']


    eg:pop(self, index=None)-------------------------------------------------------------------移除元素,并且返回移除的元素,默认是最后一个
    举例:
    temp_list = ['cui','yang','cui']
    temp_list.pop(1)
    print(temp_list)
    输出结果:['cui', 'cui']


    eg:remove(self, value)-----------------------------------------------------------------------移除元素,如果没的刚返回错误
    举例:
    temp_list = ['cui','yang','cui']
    temp_list.remove('cui')
    print(temp_list)
    输出结果:['yang', 'cui']

    eg:reverse(self)-------------------------------------------------------------------------------将整个列表反转
    举例:
    temp = ['cui','yan','cheng','123','345']
    temp.reverse()
    print(temp)
    输出结果:['345', '123', 'cheng', 'yan', 'cui']


    eg:sort(self, key=None, reverse=False)----------------------------------------------将整个列表进行排序,目前还不怎么用
    举例:
    temp = [1,34,534,56,234,15]
    temp.sort()
    print(temp)
    输出结果:[1, 15, 34, 56, 234, 534]


    eg:del------------------------------------------------------------------------------------------------------删除指定索引元素
    举例:
    temp = ['cui','yan','cheng','123','345']
    del temp[0]
    print(temp)
    del temp[0:2]
    print(temp)
    输出结果:['yan', 'cheng', '123', '345']
    ['123', '345']


    4、元组(list)
    元组和列表基本是一样的,区别是列表可以修改,而元组是不可以修改的。
    由于元组不支持删除和修改,没有像列表那么多的特性
    支持:索引,切片,,len,for,counts,index(指定元素的索引位置)等等


    5、字典(dict)
    字典的每一个元素:键值对
    比如说:像下面的举例,cui---相当于key也就是键,abc--相当于value也就是值,他们两个是一对
    temp_dir = {
    'cui': 'abc',
    'rain':'bbb',
    'sir':'ccc'
    }
    print(temp_dir)
    print(temp_dir['rain'])----------------------------------------索引的时候我们是根据key来索引的
    输出结果:{'cui': 'abc', 'rain': 'bbb', 'sir': 'ccc'}
    bbb


    可以基于key,Value,对的值单独输出
    eg:
    temp_dir = {
    'cui': 'abc',
    'rain':'bbb',
    'sir':'ccc'
    }
    print(temp_dir.keys())----------------------------------基于key
    print(temp_dir.values())-------------------------------基于Value
    print(temp_dir.items())---------------------------------基于对
    for i in temp_dir:--------------------------------------默认情况下是输出Key值
    print(i)
    输出结果:dict_keys(['cui', 'rain', 'sir'])
    dict_values(['abc', 'bbb', 'ccc'])
    dict_items([('cui', 'abc'), ('rain', 'bbb'), ('sir', 'ccc')])
    cui
    rain
    sir
    对于键值对的循环输出
    eg: for i,k in temp_dir.items():--------------------------这里为什么需要i和k,因为有两个值一个键一个值所以需要两个变量
    print(i,k)
    输出结果:cui abc
    rain bbb
    sir ccc

    一些常用类的功能

    eg:clear(self)----------------------------------------------------用来清空字典里的所有的内容
    举例:
    temp_dir = {
    'cui': 'abc',
    'rain':'bbb',
    'sir':'ccc'
    }
    temp_dir.clear()
    print(temp_dir)
    输出结果:{}

    eg:fromkeys(*args, **kwargs)------------------------------------------用来创建一个新的字典,并且返回
    举例:
    sqe = ['rain','sir']
    top = ['cyan','cjie']
    temp_new = dict.fromkeys(sqe)
    temp= dict.fromkeys(sqe,top)
    print(temp_new)
    print(temp)
    输出结果:{'rain': None, 'sir': None}
    {'rain': ['cyan', 'cjie'], 'sir': ['cyan', 'cjie']}

    eg:get(self, k, d=None)------------------------------------------------------------根据key值来获取Value,与用索引不同的是,如果没有这个key值那么get会给它返回一个空值,而索引不会则会报错,这里的d是默认的填充的值,当vlaue没有时我们可以用默认值来填充
    举例:
    temp = {
    'cui':'rain',
    'sir':'yan',
    'sea':'cheng'
    }
    print(temp.get('sir'))
    print(temp.get('sirese'))
    输出结果:yan
    None

    eg:pop(self, k, d=None)-------------------------------------------------------根据key值来移除并返回对于key对应的Value
    举例:
    temp = {
    'cui':'rain',
    'sir':'yan',
    'sea':'cheng'
    }
    temp_new = temp.pop('sir')
    print(temp)
    print(temp_new)
    输出结果:{'cui': 'rain', 'sea': 'cheng'}
    yan


    eg:popitem(self)------------------------------------------------------默认是从最后开始移除,并且没有返回值
    举例:

    temp = {
    'cui':'rain',
    'sir':'yan',
    'sea':'cheng'
    }
    temp.popitem()
    print(temp)
    输出结果:{'cui': 'rain', 'sir': 'yan'}

    eg:setdefault(self, k, d=None):---------------------------------------这个与get的用法差不多也是获取key相应的Value的值,但不同的是如果没有key值,那么他会把我们所查询的key值写到原有的字典中,默认key值所对就的Value的值是None
    举例:
    temp = {
    'cui':'rain',
    'sir':'yan',
    'sea':'cheng'
    }
    temp_new = temp.setdefault('sir')
    temp_12 = temp.setdefault('sirewer','mke')
    print(temp_new)
    print(temp_12)
    print(temp)
    输出结果:yan
    mke
    {'cui': 'rain', 'sir': 'yan', 'sea': 'cheng', 'sirewer': 'mke'}

    eg:update(self, E=None, **F)------------------------------将一个字典的内容更新到另一个字典时去
    举例:
    temp = {
    'cui':'rain',
    'sir':'yan',
    'sea':'cheng'
    }
    top = {
    'hell':'fore'
    }
    temp.update(top)
    print(temp)
    输出结果:{'cui': 'rain', 'sir': 'yan', 'sea': 'cheng', 'hell': 'fore'}


    enumerate()---------------------------------------------------------可迭代的对象添加序号,在输出的时候会在之前默认加个key值
    举例:
    temp = ['rain','sir','top']
    for k,i in enumerate(temp,1):
    print(k,i)
    ke = int(input("请输入序号:"))
    print(temp[ke])
    输出结果:1 rain
    2 sir
    3 top
    请输入序号:2
    top

    range与xrange区别
    在python,27的版本中会有xrange
    python2.7:对于range当我们去创建了range时候他们把里面的内容直接生成放在内存中比如range(1,100000)那么他会把1到100000所有的数都会生成放到内存中,而对于xrange不会,只有我们去迭代的时候他们一个一个的生成
    python3.7:这个版本只有range但是这个时候的range与xrange是一样的
    举例:
    for i in range(1,10):--------------输出1-9,这时大于等于1小于10
    print(i)

    for k in range(1,10,2):-------------------------输出1-10之间的数隔2输出
    print(k)
    for f in range (10,1,-1):------------------------从10到1依次输出
    print(f)

  • 相关阅读:
    世界上最遥远的距离(泰戈尔)
    肩周炎?
    [转]C#基础概念二十五问
    [转]ASP.NET 2.0的缓存技术简介
    SQL Server 2005下的分页SQL
    [转]基本的缓存类操作封装(抽象类)
    GridView鼠标经过行变色
    推荐一个好用的.NET2.0下的发送EMail的库
    Data Access Application Block for .NET
    Pet Shop 4.0的缓存机制
  • 原文地址:https://www.cnblogs.com/sea-sir/p/8472962.html
Copyright © 2011-2022 走看看