Python开发第一篇 基础篇(二)-------运算符与基本数据类型

对于python而言,一切事物都是对象,对象是基于类创建的,对象继承了类的属性,方法等特性

  一.int

    首先我们来查看一下int包含了哪些函数

    

复制代码
# python3.x
dir(int)
# ['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']


# python 2.x dir(int) # ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']
复制代码
__abs__ 绝对值
__add__ 加法
__and__ 与&运算
__bool__ 布尔值
__divmod__ 除法取整取模
__eq__ ==比较运算符
__float__ 转换为浮点数
__floordiv__地板除//
__getattribute__获取对象属性
__ge__ 比较运算>=
__invert__ 非~运算
__le__ 小于等于
__lshift__左移运算
__lt__小于
__mod__取模运算
__mul__ 乘法运算
__neg__一元运算减法
__ne__ 不等于比较
__or__ 或|运算
__pow__ 幂运算
__rdivmod__ 与divmod返回的结果相反
__sizeof__ 计算数据类型占用内存大小
__str__ int转换成str
__sub__ 减法运算
__truediv__ 真除
__xor__ 异或^运算
bit_length 显示数据所占位长度
conjugate
__format__ 格式化输出
from_bytes 字符转换十进制
to_bytes int转换为字节

  二.str

复制代码
1 #python3.5
2 dir(str)
3 #['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__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 #python2.7 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']
复制代码
__add__ 字符串拼接
__contains__ 包含判断
__eq__ 字符串==比较
__getattribute__获取对象属性
__getitem__获取对应字符
__getnewargs__转换成元组
__ge__ 字符串比较
__gt__ 字符串大于判断
__hash__ 生成一个临时的hash值
__iter__ 字符串迭代
__len__ 判断字符串长度
__le__小于等于
__lt__ 小于
__mul__ 乘法运算
__ne__ 不等于比较
zfill 以0填充
upper 字母转换大写
title 标题
swapcase 大小写转换
strip 去除字符串两边的空格
startswith 字符串是否存在该字符
splitlines 以换行符分割字符串
split 默认以空格分割字符
rstrip 去除右边的空格
rpartition 返回字符串的一部分
rjust 向右偏移
rindex 查找下标
rindex 查找下标
rindex 查找下标
rindex 查找下标
rindex 查找下标
rindex 查找下标
translate 翻译
rfind 从左到右查找
replace 字符串替换
partition 截取字符串
maketrans 翻译表
maketrans 翻译表
maketrans 翻译表
maketrans 翻译表
lstrip 去除左边的空格
lower 转换小写
ljust 右填充
join 生存一个字符串
isupper 是否全部大小
istitle 是否是标题
isspace 是否是空格
issprintable 是否可以被打印
isnumeric 是否是数字
islower 是否是小写
isidentifier
isdigit 是否是数字
isdecimal 是否是数字
isalpha 是否是字母
isalnum 是否为数字或字母
index 通过字符查找下标
find查找字符串下标
format 格式化输出字符串
expandtabs 制表符长度
endswith 判断结束字符
encode 编码
count 统计相同的字符
center 中心显示
casefold 字母转换小写
capitalize 首字母大写

  三.dict 

复制代码
1 #python3.5
2 dir(dict)
3 #['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'] 4 5 #python2.x 6 dir(dict) 7 #['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']
复制代码
clear 清空字典
copy 浅拷贝字典
fromkeys 返回一个新字典
get 获取字典值
items 获取字典的key,values
keys 以元组形式返回字典键
pop 删除指定的键值对
popitem 随机删除键值对
setdefault 设置默认的键值对
update 更新字典
values 以列表形式返回字典的值

 

猜你喜欢

转载自www.cnblogs.com/dusihan/p/10130426.html