zoukankan      html  css  js  c++  java
  • 初识python第二天(3)

    我们接着上一篇博客,继续来来了解Python一些常见类的函数使用方法

    一.int

     1 # 运算符,>=,比较self是否大于等于value,只要满足大于或者等于其中一个条件,就返回True,否则就返回False
     2 
     3 num = 8   
     4 result = num.__ge__(6)
     5 print(result)
     6 #结果输出  True
     7 
     8 num = 8   
     9 result = num.__ge__(8)
    10 print(result)
    11 #结果输出  True
    __ge__ 比较运算符
    1 # 获取对象属性,如果存在就返回该对象属性,否则就报错,一般需要与hassttr一同使用
    2 num = 8
    3 print(hasattr(num, '__add__'))  #使用hasattr对象是否有__add__这个函数存在
    4 
    5 result = num.__getattribute__('__add__')   
    6 print(result)
    7 #打印输出<method-wrapper '__add__' of int object at 0x0000000059BA39B0>
    8 # 获取对象属性,如果存在就返回该对象属性,否则就报错,一般需要与hassttr一同使用
    __getattribute__ 获取对象属性
     1 num = -9    
     2 result = num.__invert__()
     3 print(result)
     4 #结果输出 8
     5 
     6 num = 9    
     7 result = num.__invert__()
     8 print(result)
     9 #结果输出-10
    10 
    11 num = 90
    12 result = num.__invert__()
    13 print(result)
    14 #结果输出  -91
    __invert__ 非运算
     1  # 比较运算符,用于比较self与vlaue是否小于等于,如果小于或等于就返回结果为True,否则就为False
     2 
     3 num = 8     
     4 result = num.__le__(9)
     5 print(result)
     6 #结果输出  True
     7 
     8 num = 8     
     9 result = num.__le__(8)
    10 print(result)
    11 #结果输出  True
    1 # 左移运算,value参数几就表示像左移几位
    2 num = 8    
    3 result = num.__lshift__(2)
    4 print(result)
    5 # 0 0 0 0 1 0 0 0    8
    6 # 0 0 1 0 0 0 0 0    32
    7 #结果输出  32
    1 # 比较运算,小于,判断self是否小于value,如果小于就返回结果为True,否则就为False
    2 
    3 num = 8  
    4 result = num.__lt__(9)
    5 print(result)
    6 #结果输出  True
    __lt__ 比较运算符,小于
    1  # 取模运算,将得到的结果的余数返回
    2 
    3 num = 9  
    4 result = num.__mod__(4)
    5 print(result)
    6 
    7 #结果输出 1
    __mod__ 取模运算
    1 num = 8   
    2 result = num.__mul__(4)
    3 print(result)
    4 
    5 #结果输出 32
    __mul__ 乘法运算
     1 # 算术运算,判断两个对象是否不相等,如果不相等,就返回结果为True,否则返回结果为False
     2 num = 8   
     3 result = num.__ne__(9)
     4 print(result)
     5 #结果输出 True
     6 
     7 num = 8   
     8 result = num.__ne__(8)
     9 print(result)
    10 #结果输出 False
    __ne__ 算术运算
     1 #一元运算减法,返回对象相反的结果
     2 
     3 num = 90  
     4 result = num.__neg__()
     5 print(result)
     6 #结果输出  -90
     7 
     8 num = -90  
     9 result = num.__neg__()
    10 print(result)
    11 #结果输出  90
    __neg__ 一元运算减法
    1 # 位的或运算,当凡相同位为真,即为1,则结果为真,即1,所以结果为9
    2 
    3 num = 8    
    4 result = num.__or__(9)
    5 print(result)
    6 # # 0 0 0 0 1 0 0 0     8
    7 # # 0 0 0 0 1 0 0 1     9
    8 #结果输出  9
    View Code
    1 #幂运算,即8**2次方
    2 num = 8    
    3 result = num.__pow__(2)
    4 print(result)
    5 
    6 #结果输出  64
    __pow__ 幂运算
    1  #  加法,value+self
    2 num = 8 
    3 result = num.__radd__(9)
    4 print(result)
    5 
    6 #输出结果  17
    __radd__ 加法运算
    1 #与&运算,相同位1则为1,返回结果相同位相加,不相同则为零
    2 num = 8   
    3 result = num.__rand__(9)
    4 print(result)
    5 
    6 #结果输出  8
    7 # 0 0 0 0 1 0 0 0
    8 # 0 0 0 0 1 0 0 1
    __rand__ 与&运算
    1 num = 8
    2 print(num.__or__(7))
    3 #结果输出  15
    4 # 0 0 0 0 1 0 0 0   8
    5 # 0 0 0 0 0 1 1 1    7
    6 # 0 0 0 0 1 1 1 1     15
    7 #位的或运算,只要相同位为真就为真,即为1,则结果为真,所以最终结果为15
    __or__ 或!运算
    1 #__rdivmod与divmod的结果相反
    2 num = 9
    3 print(num.__rdivmod__(3))
    4 
    5 #结果输出 (0, 3)
    6 #返回的结果(0,3),左边为余数,右边为整除的结果
    __rdivmod__
    1 num = 7
    2 print(num.__sizeof__())
    3 
    4 #结果输出为28个字节,
    __sizeof__计算数据类型占用内存大小
    1 num = 8
    2 result = num.__str__()
    3 print(type(result))
    4 
    5 #结果输出  <class 'str'>
    6 #将int数据类型转换为str数据类型
    __str__ int转换为str
    1 num = 8
    2 print(num.__sub__(6))
    3 #结果输出  2
    4 #对象本身减去传入的参数,得到最终的返回值
    __sub__ 减法运算
    1 #真除,返回的数据类型为float,浮点型
    2 num = 8
    3 print(num.__truediv__(2))  #结果输出  4.0
    4 
    5 num = 8
    6 print(num.__truediv__(3))  
    7 #结果输出  2.6666666666666665
    __truediv__真除
    1 num = 8
    2 print(num.__xor__(4))
    3 #结果输出 12
    4 
    5 # 0 0 0 0 1 0 0 0    8
    6 # 0 0 0 0 0 1 0 0    4
    7 # 0 0 0 0 1 1 0 0   12
    8 #同位比较,都是0则为假,都是1则为假,一真一假则为真
    __xor__ 异或^运算
    1 num = 8
    2 print(num.bit_length())
    3 
    4 #结果输出为4
    5 
    6 # 0 0 0 0 0 1 0 0 #长度为4
    bit_length 显示数据类型占位长度
    1 num = 2.3 - 2.5j
    2 result = num.real     #复数的实部
    3 print(result)              #打印输出2.3
    4 result = num.imag    #复数的虚部
    5 print(result)               #打印输出2.5
    6 
    7 result = num.conjugate()     #返回该复数的共轭复数
    8 print(result)     #打印输出 (2.3+2.5j)
    conjugate
    1 print(int.from_bytes(bytes=b'3', byteorder='little'))
    2 
    3 #打印输出51,即将字符3转换为十进制
    from_bytes 字符转换十进制
     1 num = 8
     2 result = num.to_bytes(3, byteorder='little')
     3 print(result)
     4 #打印输出 b'x08x00x00'
     5 
     6 for i in result:
     7     print(i)
     8 
     9 #打印输出
    10 8
    11 0
    12 0
    to_bytes_int转换为字节

    二.str

    1 #python 3.x
    2 print(dir(str))
    3 #['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
    4 
    5 #python 2.x
    6 dir(str)
    7 #['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
    1 a = 'hello'
    2 print(a.__add__(' world'))
    3 
    4 #输出结果 hello world
    __add__字符串拼接
    1 #用来判断参数是否在对象中,如果存在就返回True,不存在就返回False
    2 
    3 name = 'LaiYing'
    4 print(name.__contains__('ai'))
    5 #结果输出 True 
    6 
    7 name = 'LaiYing'
    8 print(name.__contains__('x'))
    9 #结果输出False
    __contains__ 判断字符串是否存在
    1 #__eq__,用来比较对象与参数的字符串是否相同,如果相同就返回True,否则就返回False
    2 name = 'jack'
    3 print(name.__eq__('jack'))
    4 #结果输出 True
    5 
    6 name = 'jack'
    7 print(name.__eq__('Jack'))
    8 #结果输出False
    __eq__ 比较字符串是否相等
    1 #用来判断对象是否包含传入参数的属性,一般与hasattr配合使用
    2 name = 'LaiYing'
    3 print(hasattr(name, '__add__'))  #先使用hasattr查看对象是否有这个属性
    4 print(name.__getattribute__('__add__'))
    5 
    6 #结果输出<method-wrapper '__add__' of str object at 0x00000000006AF1F0>
    __getattribute__ 判读对象属性
    1 #获取对应字符,下标从0开始,传的参数如果超出下标会报错
    2 name = 'LaiYing'   
    3 print(name.__getitem__(2))
    4 
    5 #输出下标为2的字符  i
    __getitem__ 获取对应字符
    1 name = 'LaiYing'
    2 print(name.__getnewargs__())
    3 #结果输出 ('LaiYing',)
    4 #将字符类型转换为元组输出
    __getnewwargs__转换成元组
    1 #判断传入的参数和对象的内容是否相同,字符串不区分大小写,如果相同就返回True,否则就返回False
    2 name = 'laiying'
    3 print(name.__ge__('LAIYING'))   #结果输出 True
    4 print(name.__ge__('LaiYING'))    #结果输出 True
    5 print(name.__ge__('laiying'))      #结果输出 True
    6 
    7 print(name.__eq__('laiyinghh'))  #结果输出为假
    __ge__ 字符串比较
    1 strA = 'Hello'
    2 print(strA.__gt__('HellO'))
    3 #打印输出True
    4  
    5 #字符串比较的是传入的参数每个字符首先得包含对象,其次如果字符串之间比较,大写比小写大,,如果传入参数都为大写,且对象也都为大写,那么结果为假,字符串比较首先比较的是字符串是否相同,不相同则为假,再次每个字符进行比较,只要前面有一位大于对方,则不继续比较了
    6  
    7  #比如 HelLo与HEllo,首先两个字符串都是一样的,然后再比较第一位,第一位也一样,再比较第二位,大写比小写大,所以第二个字符串大,就不会继续比较下去了
    __gt__字符串大于判断
    1 name = 'laiying'
    2 print(name.__hash__())
    3 
    4 #结果输出 -1321548470539019197
    __hash__ 生成一个临时的hash值
    1 name = 'lai'   
    2 result = name.__iter__()
    3 for i in result:
    4     print(i)
    5 
    6 #结果输出
    7 # l
    8 #a 
    9 # i
    __iter__字符串迭代
    1 name = 'LaiYing'  
    2 print(name.__len__())
    3 
    4 #打印输出结果 7
    __len__判断字符串长度
    1 name = 'LaiYing'   
    2 print(name.__le__('Ying'))
    3 #结果输出 True
    4 #字符串小于运算比较,先比较对象是否包含传入参数,当包含则再比较相同位的字母,大小字母比小写字母大,当前面有一位比较出谁大谁小了,则不再继续比下去了
    __le__ 小于等于
    1 name = 'laiying'  
    2 print(name.__ne__('LaiYing'))
    3 #结果返回True
    4 
    5 #字符串不等于运算比较,凡是对象与传入参数只要有一个字母大小写不一样则为真,否则为假
    __ne__ 不等于比较
    1 name = 'laiying'
    2 print(name.zfill(8))
    3 #结果显示 0laiying
    4 
    5 #当传入的参数长度比对象长度大时,多余的长度则以0进行填充
    zfill 以0填充
    1 #将所有字母转换为大写
    2 
    3 name = 'laiying'
    4 print(name.upper())
    5 #结果输出 LAIYING
    upper字母转换大写
    1 #标题,将首字母大小,并默认把其他字母小写
    2 name = 'hello world'  
    3 print(name.title())
    4 
    5 #结果输出 Hello World
    6 #将每个单词的首字母大写输出,且首字母后的单词都会变成小写,如hELLo,最终会格式化为Hello
    title 标题
    1 name = 'LaiYing'  
    2 print(name.swapcase())
    3 #结果输出  lAIyING
    swapcase大小写转换
    1 name = ' lai  ying   '
    2 print(name.strip())
    3 
    4 #结果输出  lai  ying
    strip 去除字符串两边的空格
    1 print('laiying'.startswith('lai'))
    2 #输出结果Ture
    3 
    4 print('laiying'.startswith('ai'))
    5 #输出结果False
    6 
    7 #startswith这个函数可以指定起始位置进行判断字符是否存在
    startswith判断字符串的开头位置是否以你的参数开头
    1 print('hello
    world'.splitlines())
    2 #结果输出  ['hello', 'world']
    3 
    4 #splitlines默认以
    换行符进行分割字符,最终返回一个列表
    splitlines以换行分割字符串
    1 print('hello word'.split())
    2 #结果输出  ['hello', 'word']
    3 
    4 print('hello word'.split('
    '))
    5 #结果输出  ['hello word']
    6 
    7 
    8 #默认以空格进行分割,可以指定分割符
    split默认以空格进行分割
    1 print('  lai  ying  '.rstrip())
    2 #结果输出    lai  ying
    3 
    4 #打印将会把ying后面的空格去除
    rstrip去除右边空格
    1 print('hello world'.rpartition('ll'))
    2 #结果输出  ('he', 'll', 'o world')
    3 #只返回传入参数且存在字符串里的字符,然后组合成一个新的元组
    rpartition返回字符串的一部分
    1 print('hello world'.rjust(20))
    2 #结果输出         hello world
    3 #默认以空格填充,从左到最后一个单词d结尾,一个长度为20,也就是说h前面有9个空格
    4 
    5 print('hello world'.rjust(20,'*'))
    6 
    7 #结果输出  *********hello world
    8 #'*'这里可以指定填充的字符,这样看着更具体,前面有9个*被用来填充
    rjust向右偏移
    1 print('hello world'.rindex('r'))
    2 #结果输出 8
    3 #通过查找字符'r',获取字符串在hello world里面的下标位置,这里从左往右数,第8个位置,字符串的下标默认从0开始,当找不到时则抛出异常
    rindex查找下标
     1 a = 'hello 12'
     2 table1 = str.maketrans('12','赖英')
     3 print(a.translate(table1))
     4 #结果输出 hello 赖英
     5 
     6 #将字符串里面的12通过table进行翻译成对应的值,table的12长度必须和‘赖英’长度对应
     7 
     8 a = 'hello 12' #翻译
     9 table1 = str.maketrans('123','赖英好')
    10 print(a.translate(table1))
    11 #结果输出 hello 我很
    translate翻译
    1 print('laiying'.find('i'))
    2 #输出结果 2
    find找到字符串下标
    1 print('laiying'.rfind('i'))
    2 #结果输出  4
    3 
    4 print('laiying'.rfind('ii'))
    5 #结果输出 -1
    6 #如果找到,则结果为对应的下标,否则返回-1
    rfind从右开始查找
    1 print('laiying'.replace('l','y'))
    2 #结果输出 yaiying
    3 #将字符串里面所有的l替换为y,区分大小写
    replace替换字符串
    1 print('laiying'.rpartition('yi'))
    2 #结果输出 ('lai', 'yi', 'ng')
    3 #效果与partiton相似
    rpartition截取字符串
     1 table1 = str.maketrans('123', '你好吗')
     2 print(table1)
     3 #结果输出{49: 20320, 50: 22909, 51: 21527}
     4 #首先传入的必须的两个参数,且 长度相等
     5 #返回的结果将是一个字典类型,每一个字符串将会映射到第二个参数的相同位置的字符串上
     6 #当这里存在三个参数时,第三个参数必须是一个字符串类型,且整个字符串将被映射成None
     7 
     8 
     9 a = 'hello 123您好'
    10 table1 = a.maketrans('123','你好吗', '很好')
    11 print(a.translate(table1))
    12 print(table1)
    13 #结果输出,如下
    14 #{24456: None, 49: 20320, 50: 22909, 51: 21527, 22909: None}
    15 
    16 
    17 #这个字典的值将会被映射成unicode值,如49表示unicode的1
    maketrans翻译表
    1 print('  lai ying  '.lstrip())
    2 #结果输出 lai ying
    3 
    4 #将lai左边的空格去除
    lstrip去除左边空格
    1 print('HELLo WorlD'.lower())
    2 #结果输出 hello world
    3 #将所有字母全部转换为小写
    lower将所有字母转换为小写
    1 print('hello world'.ljust(20, '*'))
    2 #结果输出  hello world*********
    3 #从右像左进行填充,总长度为20,可指定填充字符
    ljust右填充
    1 print('+'.join(('hello', 'world')))
    2 #结果输出 hello+world
    3 #通过一个字符串去与join里面的一个迭代器里的字符串进行连接生成一个新的字符串
    join生成一个字符串
    1 print('Hello'.isupper())
    2 #结果输出False
    3 
    4 print('HELLO'.isupper())
    5 #结果输出 True
    6 
    7 #判断字母是否为全是大写,如果是大写返回真,否则为假
    isupper判断是否为大写
    1 print('Hello World'.istitle())
    2 #结果输出  True
    3 
    4 
    5 print('hello world'.istitle())
    6 #结果输出 False
    7 
    8 #判断每个单词首字母是否大写,是则为真,否则为假
    istitle是否为标题
    1 #isspace判断是否为空格,是为真,否则为假
    2 print('hello'.isspace())  
    3 #结果输出  False
    4 
    5 
    6 print(' '.isspace())
    7 #结果输出True
    isspace判断是否为空格
    1 print('hello world '.isprintable())
    2 #结果输出 True
    3 
    4 print('
    '.isprintable())
    5 #结果输出False
    6 
    7 #由于换行符是特殊字符,不能被打印,所以结果为假
    isprinttable是否可以被打印
    1 print('111'.isnumeric())
    2 print(''.isnumeric())
    3 print('1y'.isnumeric())
    4 
    5 #True
    6 #True
    7 #False
    8 #True包含unicode数字,全角数字(双字节),罗马数字,汉字数字
    isnumeric是否是数字
    1 print('hello'.islower())  
    2 #结果输出 True
    3 
    4 print('HelLo'.islower())
    5 #结果输出False
    6 
    7 #判断字母是否全是小写,是为真,否则为假
    islower 是否全部小写
    1 print('def'.isidentifier())  
    2 print('lai'.isidentifier())
    3 print('2b2'.isidentifier())
    4 #True
    5 #True
    6 #False
    7 
    8 #用来检查标识符是否可用,也就是说这个名字能不能用来作为变量名,是否符合命令规范,如果符合则为真,否则为假
    9 #通常会结合keyword.iskeyword()这个方法去做判断是否是关键字,防止因命名不规范导致某些内置功能不可用
    isidentifier
  • 相关阅读:
    【转】15 个 JavaScript Web UI 库
    【转】C# WebRequest发起Http Post请求模拟登陆并cookie处理示例
    【转】33个JavaScript自动完成脚本
    大数据运算问题
    页面占位符的使用
    sql 数据库默认值问题
    jquery动态绑定像Repeater和dataList一样绑定数据源
    seo页面优化的要点
    asp.net控件不可乱用的属性
    jquery经验篇
  • 原文地址:https://www.cnblogs.com/YingLai/p/5881873.html
Copyright © 2011-2022 走看看