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

一.int

 # 运算符,>=,比较self是否大于等于value,只要满足大于或者等于其中一个条件,就返回True,否则就返回False

 num = 8
result = num.__ge__(6)
print(result)
#结果输出 True num = 8
result = num.__ge__(8)
print(result)
#结果输出 True

__ge__ 比较运算符

 # 获取对象属性,如果存在就返回该对象属性,否则就报错,一般需要与hassttr一同使用
num = 8
print(hasattr(num, '__add__')) #使用hasattr对象是否有__add__这个函数存在 result = num.__getattribute__('__add__')
print(result)
#打印输出<method-wrapper '__add__' of int object at 0x0000000059BA39B0>
# 获取对象属性,如果存在就返回该对象属性,否则就报错,一般需要与hassttr一同使用

__getattribute__ 获取对象属性

 num = -9
result = num.__invert__()
print(result)
#结果输出 8 num = 9
result = num.__invert__()
print(result)
#结果输出-10 num = 90
result = num.__invert__()
print(result)
#结果输出 -91

__invert__ 非运算

  # 比较运算符,用于比较self与vlaue是否小于等于,如果小于或等于就返回结果为True,否则就为False

 num = 8
result = num.__le__(9)
print(result)
#结果输出 True num = 8
result = num.__le__(8)
print(result)
#结果输出 True
 # 左移运算,value参数几就表示像左移几位
num = 8
result = num.__lshift__(2)
print(result)
# 0 0 0 0 1 0 0 0 8
# 0 0 1 0 0 0 0 0 32
#结果输出 32
 # 比较运算,小于,判断self是否小于value,如果小于就返回结果为True,否则就为False

 num = 8
result = num.__lt__(9)
print(result)
#结果输出 True

__lt__ 比较运算符,小于

  # 取模运算,将得到的结果的余数返回

 num = 9
result = num.__mod__(4)
print(result) #结果输出 1

__mod__ 取模运算

 num = 8
result = num.__mul__(4)
print(result) #结果输出 32

__mul__ 乘法运算

 # 算术运算,判断两个对象是否不相等,如果不相等,就返回结果为True,否则返回结果为False
num = 8
result = num.__ne__(9)
print(result)
#结果输出 True num = 8
result = num.__ne__(8)
print(result)
#结果输出 False

__ne__ 算术运算

 #一元运算减法,返回对象相反的结果

 num = 90
result = num.__neg__()
print(result)
#结果输出 -90 num = -90
result = num.__neg__()
print(result)
#结果输出 90

__neg__ 一元运算减法

 # 位的或运算,当凡相同位为真,即为1,则结果为真,即1,所以结果为9

 num = 8
result = num.__or__(9)
print(result)
# # 0 0 0 0 1 0 0 0 8
# # 0 0 0 0 1 0 0 1 9
#结果输出 9
 #幂运算,即8**2次方
num = 8
result = num.__pow__(2)
print(result) #结果输出 64

__pow__ 幂运算

  #  加法,value+self
num = 8
result = num.__radd__(9)
print(result) #输出结果 17

__radd__ 加法运算

 #与&运算,相同位1则为1,返回结果相同位相加,不相同则为零
num = 8
result = num.__rand__(9)
print(result) #结果输出 8
# 0 0 0 0 1 0 0 0
# 0 0 0 0 1 0 0 1

__rand__ 与&运算

 num = 8
print(num.__or__(7))
#结果输出 15
# 0 0 0 0 1 0 0 0 8
# 0 0 0 0 0 1 1 1 7
# 0 0 0 0 1 1 1 1 15
#位的或运算,只要相同位为真就为真,即为1,则结果为真,所以最终结果为15

__or__ 或!运算

 #__rdivmod与divmod的结果相反
num = 9
print(num.__rdivmod__(3)) #结果输出 (0, 3)
#返回的结果(0,3),左边为余数,右边为整除的结果

__rdivmod__

 num = 7
print(num.__sizeof__()) #结果输出为28个字节,

__sizeof__计算数据类型占用内存大小

 num = 8
result = num.__str__()
print(type(result)) #结果输出 <class 'str'>
#将int数据类型转换为str数据类型

__str__ int转换为str

 num = 8
print(num.__sub__(6))
#结果输出 2
#对象本身减去传入的参数,得到最终的返回值

__sub__ 减法运算

 #真除,返回的数据类型为float,浮点型
num = 8
print(num.__truediv__(2)) #结果输出 4.0 num = 8
print(num.__truediv__(3))
#结果输出 2.6666666666666665

__truediv__真除

 num = 8
print(num.__xor__(4))
#结果输出 12 # 0 0 0 0 1 0 0 0 8
# 0 0 0 0 0 1 0 0 4
# 0 0 0 0 1 1 0 0 12
#同位比较,都是0则为假,都是1则为假,一真一假则为真

__xor__ 异或^运算

 num = 8
print(num.bit_length()) #结果输出为4 # 0 0 0 0 0 1 0 0 #长度为4

bit_length 显示数据类型占位长度

 num = 2.3 - 2.5j
result = num.real #复数的实部
print(result) #打印输出2.3
result = num.imag #复数的虚部
print(result) #打印输出2.5 result = num.conjugate() #返回该复数的共轭复数
print(result) #打印输出 (2.3+2.5j)

conjugate

 print(int.from_bytes(bytes=b'', byteorder='little'))

 #打印输出51,即将字符3转换为十进制

from_bytes 字符转换十进制

 num = 8
result = num.to_bytes(3, byteorder='little')
print(result)
#打印输出 b'\x08\x00\x00' for i in result:
print(i) #打印输出
8
0
0

to_bytes_int转换为字节

二.str

 #python 3.x
print(dir(str))
#['__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'] #python 2.x
dir(str)
#['__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']
 a = 'hello'
print(a.__add__(' world')) #输出结果 hello world

__add__字符串拼接

 #用来判断参数是否在对象中,如果存在就返回True,不存在就返回False

 name = 'LaiYing'
print(name.__contains__('ai'))
#结果输出 True name = 'LaiYing'
print(name.__contains__('x'))
#结果输出False

__contains__ 判断字符串是否存在

 #__eq__,用来比较对象与参数的字符串是否相同,如果相同就返回True,否则就返回False
name = 'jack'
print(name.__eq__('jack'))
#结果输出 True name = 'jack'
print(name.__eq__('Jack'))
#结果输出False

__eq__ 比较字符串是否相等

 #用来判断对象是否包含传入参数的属性,一般与hasattr配合使用
name = 'LaiYing'
print(hasattr(name, '__add__')) #先使用hasattr查看对象是否有这个属性
print(name.__getattribute__('__add__')) #结果输出<method-wrapper '__add__' of str object at 0x00000000006AF1F0>

__getattribute__ 判读对象属性

 #获取对应字符,下标从0开始,传的参数如果超出下标会报错
name = 'LaiYing'
print(name.__getitem__(2)) #输出下标为2的字符 i

__getitem__ 获取对应字符

 name = 'LaiYing'
print(name.__getnewargs__())
#结果输出 ('LaiYing',)
#将字符类型转换为元组输出

__getnewwargs__转换成元组

 #判断传入的参数和对象的内容是否相同,字符串不区分大小写,如果相同就返回True,否则就返回False
name = 'laiying'
print(name.__ge__('LAIYING')) #结果输出 True
print(name.__ge__('LaiYING')) #结果输出 True
print(name.__ge__('laiying')) #结果输出 True print(name.__eq__('laiyinghh')) #结果输出为假

__ge__ 字符串比较

 strA = 'Hello'
print(strA.__gt__('HellO'))
#打印输出True #字符串比较的是传入的参数每个字符首先得包含对象,其次如果字符串之间比较,大写比小写大,,如果传入参数都为大写,且对象也都为大写,那么结果为假,字符串比较首先比较的是字符串是否相同,不相同则为假,再次每个字符进行比较,只要前面有一位大于对方,则不继续比较了 #比如 HelLo与HEllo,首先两个字符串都是一样的,然后再比较第一位,第一位也一样,再比较第二位,大写比小写大,所以第二个字符串大,就不会继续比较下去了

__gt__字符串大于判断

 name = 'laiying'
print(name.__hash__()) #结果输出 -1321548470539019197

__hash__ 生成一个临时的hash值

 name = 'lai'
result = name.__iter__()
for i in result:
print(i) #结果输出
# l
#a
# i

__iter__字符串迭代

 name = 'LaiYing'
print(name.__len__()) #打印输出结果 7

__len__判断字符串长度

 name = 'LaiYing'
print(name.__le__('Ying'))
#结果输出 True
#字符串小于运算比较,先比较对象是否包含传入参数,当包含则再比较相同位的字母,大小字母比小写字母大,当前面有一位比较出谁大谁小了,则不再继续比下去了

__le__ 小于等于

 name = 'laiying'
print(name.__ne__('LaiYing'))
#结果返回True #字符串不等于运算比较,凡是对象与传入参数只要有一个字母大小写不一样则为真,否则为假

__ne__ 不等于比较

 name = 'laiying'
print(name.zfill(8))
#结果显示 0laiying #当传入的参数长度比对象长度大时,多余的长度则以0进行填充

zfill 以0填充

 #将所有字母转换为大写

 name = 'laiying'
print(name.upper())
#结果输出 LAIYING

upper字母转换大写

 #标题,将首字母大小,并默认把其他字母小写
name = 'hello world'
print(name.title()) #结果输出 Hello World
#将每个单词的首字母大写输出,且首字母后的单词都会变成小写,如hELLo,最终会格式化为Hello

title 标题

 name = 'LaiYing'
print(name.swapcase())
#结果输出 lAIyING

swapcase大小写转换

 name = ' lai  ying   '
print(name.strip()) #结果输出 lai ying

strip 去除字符串两边的空格

 print('laiying'.startswith('lai'))
#输出结果Ture print('laiying'.startswith('ai'))
#输出结果False #startswith这个函数可以指定起始位置进行判断字符是否存在

startswith判断字符串的开头位置是否以你的参数开头

 print('hello\nworld'.splitlines())
#结果输出 ['hello', 'world'] #splitlines默认以\n换行符进行分割字符,最终返回一个列表

splitlines以换行分割字符串

 print('hello word'.split())
#结果输出 ['hello', 'word'] print('hello word'.split('\n'))
#结果输出 ['hello word'] #默认以空格进行分割,可以指定分割符

split默认以空格进行分割

 print('  lai  ying  '.rstrip())
#结果输出 lai ying #打印将会把ying后面的空格去除

rstrip去除右边空格

 print('hello world'.rpartition('ll'))
#结果输出 ('he', 'll', 'o world')
#只返回传入参数且存在字符串里的字符,然后组合成一个新的元组

rpartition返回字符串的一部分

 print('hello world'.rjust(20))
#结果输出 hello world
#默认以空格填充,从左到最后一个单词d结尾,一个长度为20,也就是说h前面有9个空格 print('hello world'.rjust(20,'*')) #结果输出 *********hello world
#'*'这里可以指定填充的字符,这样看着更具体,前面有9个*被用来填充

rjust向右偏移

 print('hello world'.rindex('r'))
#结果输出 8
#通过查找字符'r',获取字符串在hello world里面的下标位置,这里从左往右数,第8个位置,字符串的下标默认从0开始,当找不到时则抛出异常

rindex查找下标

 a = 'hello 12'
table1 = str.maketrans('','赖英')
print(a.translate(table1))
#结果输出 hello 赖英 #将字符串里面的12通过table进行翻译成对应的值,table的12长度必须和‘赖英’长度对应 a = 'hello 12' #翻译
table1 = str.maketrans('','赖英好')
print(a.translate(table1))
#结果输出 hello 我很

translate翻译

 print('laiying'.find('i'))
#输出结果 2

find找到字符串下标

 print('laiying'.rfind('i'))
#结果输出 4 print('laiying'.rfind('ii'))
#结果输出 -1
#如果找到,则结果为对应的下标,否则返回-1

rfind从右开始查找

 print('laiying'.replace('l','y'))
#结果输出 yaiying
#将字符串里面所有的l替换为y,区分大小写

replace替换字符串

 print('laiying'.rpartition('yi'))
#结果输出 ('lai', 'yi', 'ng')
#效果与partiton相似

rpartition截取字符串

 table1 = str.maketrans('', '你好吗')
print(table1)
#结果输出{49: 20320, 50: 22909, 51: 21527}
#首先传入的必须的两个参数,且 长度相等
#返回的结果将是一个字典类型,每一个字符串将会映射到第二个参数的相同位置的字符串上
#当这里存在三个参数时,第三个参数必须是一个字符串类型,且整个字符串将被映射成None a = 'hello 123您好'
table1 = a.maketrans('','你好吗', '很好')
print(a.translate(table1))
print(table1)
#结果输出,如下
#{24456: None, 49: 20320, 50: 22909, 51: 21527, 22909: None} #这个字典的值将会被映射成unicode值,如49表示unicode的1

maketrans翻译表

 print('  lai ying  '.lstrip())
#结果输出 lai ying #将lai左边的空格去除

lstrip去除左边空格

 print('HELLo WorlD'.lower())
#结果输出 hello world
#将所有字母全部转换为小写

lower将所有字母转换为小写

 print('hello world'.ljust(20, '*'))
#结果输出 hello world*********
#从右像左进行填充,总长度为20,可指定填充字符

ljust右填充

 print('+'.join(('hello', 'world')))
#结果输出 hello+world
#通过一个字符串去与join里面的一个迭代器里的字符串进行连接生成一个新的字符串

join生成一个字符串

 print('Hello'.isupper())
#结果输出False print('HELLO'.isupper())
#结果输出 True #判断字母是否为全是大写,如果是大写返回真,否则为假

isupper判断是否为大写

 print('Hello World'.istitle())
#结果输出 True print('hello world'.istitle())
#结果输出 False #判断每个单词首字母是否大写,是则为真,否则为假

istitle是否为标题

 #isspace判断是否为空格,是为真,否则为假
print('hello'.isspace())
#结果输出 False print(' '.isspace())
#结果输出True

isspace判断是否为空格

 print('hello world '.isprintable())
#结果输出 True print('\n'.isprintable())
#结果输出False #由于换行符是特殊字符,不能被打印,所以结果为假

isprinttable是否可以被打印

 print(''.isnumeric())
print('壹'.isnumeric())
print('1y'.isnumeric()) #True
#True
#False
#True包含unicode数字,全角数字(双字节),罗马数字,汉字数字

isnumeric是否是数字

 print('hello'.islower())
#结果输出 True print('HelLo'.islower())
#结果输出False #判断字母是否全是小写,是为真,否则为假

islower 是否全部小写

 print('def'.isidentifier())
print('lai'.isidentifier())
print('2b2'.isidentifier())
#True
#True
#False #用来检查标识符是否可用,也就是说这个名字能不能用来作为变量名,是否符合命令规范,如果符合则为真,否则为假
#通常会结合keyword.iskeyword()这个方法去做判断是否是关键字,防止因命名不规范导致某些内置功能不可用

isidentifier

初识python第二天(3)的更多相关文章

  1. 初识python第二天(2)

    整理Python常见数据类型内置函数的使用方法如下: 一.int 首先我们来查看一下int包含了哪些函数 #python3.x print(dir(int)) #['__abs__', '__add_ ...

  2. 初识Python第二天(1)

    在Python中,一切事物都是对象,对象是基于类创建的,对象继承了类的属性,方法等. 一.传递参数 1.1新建python文件,名为twoday_args.py,输出以下代码 import sys p ...

  3. 初识Python第二天(4)

    '.isdecimal()) print('壹'.isdecimal()) print('11d'.isdecimal()) #True #False #False #只有全部为unicode数字,全 ...

  4. Python开发【第二篇】:初识Python

    Python开发[第二篇]:初识Python   Python简介 Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏 ...

  5. python学习【第二篇】初识python

    python的安装 windows 1.下载安装包 https://www.python.org/downloads/ 2.安装 默认安装路径:C:\python27 3.配置环境变量 [右键计算机] ...

  6. 孤荷凌寒自学python第二十七天python的datetime模块及初识datetime.date模块

    孤荷凌寒自学python第二十七天python的datetime模块及初识datetime.date模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.datetime模块 dateti ...

  7. 孤荷凌寒自学python第二十五天初识python的time模块

    孤荷凌寒自学python第二十五天python的time模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 通过对time模块添加引用,就可以使用python的time模块来进行相关的时间操 ...

  8. Python导出Excel为Lua/Json/Xml实例教程(一):初识Python

    Python导出Excel为Lua/Json/Xml实例教程(一):初识Python 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出 ...

  9. Python开发【第一篇】:初识Python

    初识python 一.python简介 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解 ...

随机推荐

  1. [LintCode] Scramble String 爬行字符串

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  2. [LintCode] Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection.Notice Each element in the result m ...

  3. awesome-scala

    https://github.com/lauris/awesome-scala Awesome Scala  A community driven list of useful Scala libra ...

  4. [转] - MC、MC、MCMC简述

    贝叶斯集锦(3):从MC.MC到MCMC 2013-07-31 23:03:39 #####一份草稿 贝叶斯计算基础 一.从MC.MC到MCMC 斯坦福统计学教授Persi Diaconis是一位传奇 ...

  5. ArcGIS AddIN开发之自定义鼠标样式

    如果想修改Windows默认的鼠标样式,可以这样 //设置鼠标样式 this.Cursor = System.Windows.Forms.Cursors.Cross; 可是如果想设置成一些自定义的很好 ...

  6. Unity3D游戏引擎最详尽基础教程

    第一节 加入重力 我们先打开Unity3d,新建一个场景(Scene),新建的时候应该会有对话框要求你加入哪些Asset Package,记得选择Physics Material,因为后面我们一定要用 ...

  7. Canvas 属性,方法

      context . restore() //弹出堆最上面保存的绘图状态 context . save() //在绘图状态堆上保存当前绘图状态 绘图状态可以看作当前画面应用的所有样式和变形的一个快照 ...

  8. chrome 优秀的插件推荐

    就本人使用过的chrome插件推荐下: 1:Adblock Plus 免费的广告拦截器,可阻止所有烦人的广告及恶意软件和跟踪. 2:ChaZD 英文翻译,妈妈再也不用担心我英文看不懂了,ChaZD 查 ...

  9. Javascript算术运算

    Javascript中Math对像的一些复杂算术运算方法: Math.pow(2,53)  //2的53次幂 结果:9007199254740992 Math.round(0.6)  //0.6四舍五 ...

  10. angularJs表单校验(超级详细!!!)

    html代码 <!DOCTYPE html> <html ng-app="angularFormCheckModule"> <head> < ...