python实战打卡—day1
-
求绝对值
取绝对值或复数的模
abs(-6) # 6
-
元素都为真
接受一个迭代器,如果迭代器的所有元素都为真,则返回True,否则返回False
什么是迭代器?
迭代是python中访问集合元素的一种非常强大的一种方式。迭代器是一个可以记住遍历位置的对象,因此不会像列表那样一次性全部生成,而是可以等到用的时候才生成,因此节省了大量的内存资源。迭代器对象从集合中的第一个元素开始访问,直到所有的元素被访问完。迭代器有两个方法:iter()和next()方法。
可迭代对象?
类似于list、tuple、str 等类型的数据可以使用for …… in…… 的循环遍历语法从其中依次拿到数据并进行使用,我们把这个过程称为遍历,也称迭代。python中可迭代的对象有list(列表)、tuple(元组)、dict(字典)、str(字符串)set等。
除此之外,也可以创建一个可迭代的对象:只要此对象含有_ iter _方法,那么它就是一个可迭代的对象。
all([1,0,3,6]) # False all([1,3,6]) # True # 0为假
-
元素至少一个为真
接受⼀个迭代器,如果迭代器⾥ ⾄少有⼀个 元素为真,那么返回 True,否则返回 False
any([0,0,0,[]]) # False any([1,2,3]) # True
-
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='001',name='xiaoming') print(xiaoming) # id='001',name='xiaoming' ascii(xiaoming) # 'id=001,name=xiaoming'
ascii() 函数类似 repr() 函数, 返回一个表示对象的字符串, 但是对于字符串中的非 ASCII 字符则返回通过 repr() 函数使用 \x, \u 或 \U 编码的字符。 生成字符串类似 Python2 版本中 repr() 函数的返回值。
ascii('runoob') # "'runoob'"
-
十转二
将十进制转为二进制
bin(10) # '0b1010'
-
十转八
十进制转为八进制
oct(9) # '0o11'
-
十转十六
十进制转为十六进制
hex(15) # '0xf'
-
判断是真是假
测试⼀个对象是True, 还是False。bool 是 int 的子类。
bool([0,0,0]) # False bool([]) # Flase bool([1,0,1]) # True
-
字符串转字节
将一个字符串转成字节类型。
s='apple' bytes(s,encoding='utf-8') # b'apple'
-
转为字符串
将字符类型、数值类型等转化为字符串类型。
i=100 str(i) # '100'
-
是否可调用
判断对象是否可被调⽤,能被调⽤的对象就是⼀个 callable 对象,⽐如函数 str, int 等都是可被调⽤的,但是例⼦4 中 xiaoming实例是不可被调⽤的:
callable(str) # True callable(xiaoming) # False
如果想让 xiaoming能被调⽤ xiaoming(), 需要重写 Student类的 _ _call__⽅法:
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}') t=Student('001','xiaoming') t() ''' i can be called my name isxiaoming '''
-
十转ASCII
查看⼗进制整数对应的 ASCII字符。
chr(65) # 'A'
-
ASCII转十
查看某个 ASCII字符 对应的⼗进制数。
ord('A') # 65
-
类方法
classmethod 装饰器对应的函数不需要实例化,不需要 self参数,但第⼀个参数需要是表⽰⾃⾝类 的 cls 参数,可以来调⽤类的属性,类的⽅法,实例化对象等。‘‘类方法让类模板具有记忆力’’。classmethod 装饰器对应的函数不需要实例化,不需要 self参数,但第⼀个参数需要是表⽰⾃⾝类的 cls 参数,可以来调⽤类的属性,类的⽅法,实例化对象等。类方法的参数不是对象,而是类。
类的普通方法不可以通过类名调用,类方法可以通过类名调用。
class test(): def ts(self): print('hello') @classmethod def ts2(cls): print('hello world') test.ts() # 错误 test.ts2() # hello world
一句话,类方法就是在类里面自定义一个函数方法,把*@classmethod*这玩意往头上一放,类的名字就进这个函数方法里了
定义:
@classmethod def 方法名称(cls,参数): 方法体
调用:
类名.方法名(参数) # 不建议通过对象访问类方法
说明:
- 至少有一个形参,第一个形参用于绑定类,一般命名为’cls’
- 使用@classmethod修饰的目的是调用类方法时可以隐式传递类
- 类方法中不能访问实例成员,实例方法中可以访问类成员
类方法的两种调用方式:
- 使用类的名称调用
- 使用实例调用
注意: 从以上的两种调用方式,我们需要知道,类方法调用的时候,会向他的第一个参数传递类的名称
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,str1): print(cls,str1)
g=Student('1','2') print(g) # id=1,name=2
实例调用:
g.f('xiaoli') # <class '__main__.Student'> xiaoli
类名调用:
Student.f('xiaoli') # <class '__main__.Student'> xiaoli
-
执行字符串表示的代码
将字符串编译成python能识别或可执⾏的代码,也可以将⽂字读成字符串再编译。
s='print("helloworld")' r=compile(s,"<string>","exec") r # <code object <module> at 0x00000157A4BC30C0, file "<string>", line 1> exec(r) # helloworld
-
创建复数
a=complex(1,2) a # (1+2j) abs(a) # 2.23606797749979
-
动态删除属性
删除对象的属性。
delattr(xiaoming,'id') hasattr(xiaoming,'id') # False
-
转为字典
创建数据字典。
dict() # {} dict(a='a',b='b') # {'a': 'a', 'b': 'b'} dict(zip(['a','b'],[1,2])) # {'a': 1, 'b': 2} dict([('a',1),('b',2)]) # {'a': 1, 'b': 2}
-
一键查看对象所有方法
不带参数时返回当前范围内的变量、⽅法和定义的类型列表;带参数时返回参数的属性,⽅法列表。
dir(xiaoming) ''' ['__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'] '''
-
取商和余数
分别取商和余数。
divmod(10,3) # (3,1)