hello,大家好,今天又是小编带大家了解python基础代码的一天!!😁😁 下面我们接着上一部分继续:
16 创建复数
In [1]:complex(1,2)
Out [1]:(1+2j)
17 动态属性删除
In [1]:delattr(xiaoming,'id')
In [2]:hasattr(xiaoming,'id')
Out [2]:False
18 转为字典
创建数据字典
In [1]: dict()
Out [1]:{}
In [2]: dict(a='a',b='b')
Out [2]:{'a':'a','b':'b'}
In [3]: dict(zip(['a','b'],[1,2]))
Out [3]:{'a':1,'b':2}
In [4]: dict([('a',1),('b',2)])
Out [4]:{'a':1,'b':2}
19 一键查看对象所有方法
不带参数时返回当前范围内的变量、方法和定义的类型列表;带参数时返回参数的属性,方法列表
In [96]: dir(xiaoming)
Out [96]:
['__class__',
'__delattr__',
'__dict__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__le__',
'__lt__',
'__module__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'name']
20 取商和余数
分别取商和余数
In [1]:divmod(10,3)
Out [1]:(3,1)
21 枚举对象
返回一个可以枚举的对象,该对象的next()方法将返回一个元组
In [1]:s=["a","b","c"]
for i,v in encumerate(s,1):
print(i,v)
1 a
2 b
3 c
22 计算表达式
将字符串str当成有效的表达式来求值并返回计算结果取出字符串中内容
In [1]:s="1+3+5"
eval(s)
Out [1]: 9
23 查看变量所占字节数
In [1]:import sys
In [2]:a = {'a':1,'b':2.0}
In [3]:sys.getsizeof(a) # 占用240个字节
Out [3]:240
24 过滤器
在函数中设定过滤条件,迭代元素,保留返回值为True的元素:
In [1]:fil = filter(lambda x:x>10,[1,11,2,45,7,6,13])
In [2]:list(fil)
Out [2]:[11,45,13]
25 转为浮点类型
将一个整数或数值型字符型转换为浮点数
In [1]:float(3)
Out [1]:3.0
如果不能转换为浮点数,则会报ValueError:
In [2]:float('a')
ValueError Traceback(most recent call last)
<ipython-input-11-99859da4e72c> in <module>()
----> 1 float('a')
ValueError: could not convert string to float :'a'
26 字符串格式化
格式化输出字符串,format(valuie,format_spec)实质上是调用了value的format(format_spec)方法
In [104]:print("i am {0},age{1}".format("tom",18))
i am tom,age18
| 3.1415926 | {:.2f} | 3.14 | 保留小数点后两位 |
| 3.1415926 | {:+.2f} | +3.14 | 带符号保留小数点后两位 |
| -1 | {:+.2f} | -1.00 | 带符号保留小数点后两位 |
| 2.71828 | {:.0f} | 3 | 不带小数 |
| 5 | {:0>2d} | 05 | 数字补零(填充左边,宽度为2) |
| 5 | {:x<4d} | 5xxx | 数字补x(填充右边,宽度为4) |
| 10 | {:x<4d} | 10xx | 数字补x(填充右边,宽度为4) |
| 1000000 | {:,} | 1,000,000 | 以逗号分隔的数字格式 |
| 0.25 | {:.2%} | 25.00% | 百分比格式 |
| 1000000000 | {:.2e} | 1.00e+09 | 指数记法 |
| 18 | {:>10d} | ' 18' | 右对齐(默认,宽度为10) |
| 18 | {:<10d} | '18' | 左对齐(宽度为10) |
| 18 | {:^10d} | ' 18 ' | 中间对齐(宽度为10) |
27 冻结集合
创建一个不可修改集合。
In [1]:frozenset([1,1,3,2,3])
Out [1]:frozenset({1,2,3})
因为不可修改,所以没有像set那样的add和pop方法
28 动态获取对象属性
获取对象的属性
In [1]:class Student():
def __init__(self,id,name):
self.id = id
self.name = name
def __repr__(self):
return 'id = '+self.id+', name = '+self.name
In [2]: xiaoming = Student(id='001',name='xiaoming')
In [3]: getattr(xiaoming,'name') # 获取xiaoming这个实例的name的属性值
Out [3]:'xiaoming'
29 对象是否有这个属性
In [1]: class Student():
def __init__(self,id,name):
self.id = id
self.name = name
def __repr__(self):
return 'id = '+self.id+', name = '+self.name
In [2]: xiaoming = Student(id='001',name='xiaoming')
In [3]: hasattr(xiaoming,'name')
Out [3]: True
In [4]: hasattr(xiaoming,'address')
Out [4]:False
30 返回对象的哈希值
返回对象的哈希值,值得注意的是自定义的实例都是可哈希的,list,dict,set等可变对象都是不可哈希的(unhashable)
In [1]: hash(xiaoming)
Out [1]:6139638
In [2]: hash([1,2,3])
TypeError Traceback(most recent call last)
<ipython-input-32-fb5b1b1d9906> in <module>()
----> 1 hash([1,2,3])
TypeError:unhashable type :'list'
今天小编分享的python基础代码就到这里了,各位发现不正确的地方可私信小编!!!大家多多点赞、评论哟!!❤❤









