python-examples

2021-05-03 19:27

阅读:687

>>> any([0,0,1])
True

4 ascii展示对象  

调用对象的 __repr__ 方法,获得该方法的返回值,如下例子返回值为字符串

>>> class Student():
    def __init__(self,id,name):
        self.id = id
        self.name = name
    def __repr__(self):
        return ‘id = ‘+self.id +‘, name = ‘+self.name

调用:

>>> xiaoming = Student(id=‘1‘,name=‘xiaoming‘)
>>> xiaoming
id = 1, name = xiaoming
>>> ascii(xiaoming)
‘id = 1, name = xiaoming‘

5 十转二

将十进制转换为二进制:

>>> bin(10)
‘0b1010‘

6 十转八

十进制转换为八进制:

>>> oct(9)
‘0o11‘

7 十转十六

十进制转换为十六进制:

>>> hex(15)
‘0xf‘

8 判断是真是假  

测试一个对象是True, 还是False.

>>> bool([0,0,0])
True
>>> bool([])
False
>>> bool([1,0,1])
True

9 字符串转字节  

字符串转换为字节类型

>>> s = "apple"
>>> bytes(s,encoding=‘utf-8‘)
b‘apple‘

10 转为字符串  

数值型等转换为字符串类型

>>> i = 100
>>> str(i)
‘100‘

11 是否可调用  

判断对象是否可被调用

In [1]: callable(str)
Out[1]: True

In [2]: callable(int)
Out[2]: True

In [3]: xiaoming
Out[3]: id = 001, name = xiaoming

In [4]: callable(xiaoming)
Out[4]: False

如果想让xiaoming能被调用 xiaoming(), 需要重写Student类的__call__方法:

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
    ...:     def __call__(self):
    ...:         print(‘I can be called‘)
    ...:         print(f‘my name is {self.name}‘)
    ...: 
    ...: 

In [2]: t = Student(‘001‘,‘xiaoming‘)

In [3]: t()
I can be called
my name is xiaoming

12 十转ASCII

查看十进制整数对应的ASCII字符

In [1]: chr(65)
Out[1]: ‘A‘

13 ASCII转十

查看某个ASCII字符对应的十进制数

In [1]: ord(‘A‘)
Out[1]: 65

14 类方法 

classmethod 装饰器对应的函数不需要实例化,不需要 self 参数,但第一个参数需要是表示自身类的 cls 参数,可以来调用类的属性,类的方法,实例化对象等。

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
    ...:     @classmethod
    ...:     def f(cls):
    ...:         print(cls)

15 执行字符串表示的代码

将字符串编译成python能识别或可执行的代码,也可以将文字读成字符串再编译。

In [1]: s  = "print(‘helloworld‘)"
    
In [2]: r = compile(s,"", "exec")
    
In [3]: r
Out[3]: code object module> at 0x0000000005DE75D0, file "", line 1>
    
In [4]: exec(r)
helloworld

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 enumerate(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: could not convert string to float: ‘a‘


评论


亲,登录后才可以留言!