2023 年へようこそ。皆様が幸せで健康であることを願っています。編集者は、新年には疫病が収まり、すべての悪運が逃げ去ることを願って、みんなのために花火を打ち上げるためにここに来ました!
テキストを開始します。
1 腹筋()
複素数の絶対値または剰余
In [1]: abs(-6)
Out[1]: 6
2 全部()
イテレータを受け取り、イテレータのすべての要素が true の場合は True を返し、そうでない場合は False を返します。
In [2]: all([1,0,3,6])
Out[2]: False
In [3]: all([1,2,3])
Out[3]: True
3 任意()
イテレータを受け入れ、イテレータ内に true の要素がある場合は True を返し、それ以外の場合は False を返します。
In [4]: any([0,0,0,[]])
Out[4]: False
In [5]: any([0,0,1])
Out[5]: True
4 アスキー()
调用对象的repr() 方法,获得该方法的返回值
In [30]: class Student():
...: def __init__(self,id,name):
...: self.id = id
...: self.name = name
...: def __repr__(self):
...: return 'id = '+self.id +', name = '+self.name
In [33]: print(xiaoming)
id = 001, name = xiaoming
In [34]: ascii(xiaoming)
Out[34]: 'id = 001, name = xiaoming'
5 dict()
データディクショナリの作成
In [92]: dict()
Out[92]: {}
In [93]: dict(a='a',b='b')
Out[93]: {'a': 'a', 'b': 'b'}
In [94]: dict(zip(['a','b'],[1,2]))
Out[94]: {'a': 1, 'b': 2}
In [95]: dict([('a',1),('b',2)])
Out[95]: {'a': 1, 'b': 2}
6 ディレクトリ()
パラメータがない場合は現在のスコープ内の変数、メソッド、定義型のリストを返し、パラメータがある場合はパラメータの属性とメソッドのリストを返します。
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']
7 divmod()
商と余りを別々に計算する
In [97]: divmod(10,3)
Out[97]: (3, 1)
8 isinstance(オブジェクト, クラス情報)
オブジェクトが classinfo クラスのインスタンスであるかどうかを判断し、true を返します。
In [20]: class Student():
...: ...: def __init__(self,id,name):
...: ...: self.id = id
...: ...: self.name = name
...: ...: def __repr__(self):
...: ...: return 'id = '+self.id +', name = '+self.name
...:
In [21]: xiaoming = Student('001','xiaoming')
In [22]: isinstance(xiaoming,Student)
Out[22]: True
9 issubclass(クラス, クラス情報)
class が classinfo クラスのサブクラスの場合は True を返します。
In [27]: class undergraduate(Student):
...: def studyClass(self):
...: pass
...: def attendActivity(self):
...: pass
...:
In [28]: issubclass(undergraduate,Student)
Out[28]: True
In [29]: issubclass(object,Student)
Out[29]: False
In [30]: issubclass(Student,object)
Out[30]: True
如果class是classinfo元组中某个元素的子类,也会返回True
In [26]: issubclass(int,(int,float))
Out[26]: True
10 iter(オブジェクト、センチネル)
反復可能なオブジェクトを返します。センチネルは省略できます
In [72]: lst = [1,3,5]
In [73]: for i in iter(lst):
...: print(i)
...:
1
3
5
Sentinel は反復オブジェクトの番兵として理解され、この要素が反復されるとすぐに終了します。
In [81]: class TestIter(object):
...: def __init__(self):
...: self.l=[1,3,2,3,4,5]
...: self.i=iter(self.l)
...: def __call__(self): #定义了__call__方法的类的实例是可调用的
...: item = next(self.i)
...: print ("__call__ is called,which would return",item)
...: return item
...: def __iter__(self): #支持迭代协议(即定义有__iter__()函数)
...: print ("__iter__ is called!!")
...: return iter(self.l)
...:
In [82]: t = TestIter()
...: t1 = iter(t, 3)
...: for i in t1:
...: print(i)
...:
__call__ is called,which would return 1
1
__call__ is called,which would return 3
最大 11 (反復可能、*[、キー、デフォルト])
最大値を返します。
In [99]: max(3,1,4,2,1)
Out[99]: 4
In [100]: max((),default=0)
Out[100]: 0
In [89]: di = {'a':3,'b1':1,'c':4}
In [90]: max(di)
Out[90]: 'c'
In [102]: a = [{'name':'xiaoming','age':18,'gender':'male'},{'name':'
...: xiaohong','age':20,'gender':'female'}]
In [104]: max(a,key=lambda x: x['age'])
Out[104]: {'name': 'xiaohong', 'age': 20, 'gender': 'female'}
12 分(反復可能、*[、キー、デフォルト])
最小値を返す
13 メモリビュー(obj)
オブジェクトがコピーせずにバッファ プロトコルをサポートしている限り、指定された引数で作成された「メモリ ビュー」オブジェクト、オブジェクトの内部データにアクセスする Python コードを返します。
14 次(反復子,[,デフォルト])
反復可能オブジェクトの次の要素を返します。
In [129]: it = iter([5,3,4,1])
In [130]: next(it)
Out[130]: 5
In [131]: next(it)
Out[131]: 3
In [132]: next(it)
Out[132]: 4
In [133]: next(it)
Out[133]: 1
In [134]: next(it,0) #迭代到头,默认返回值为0
Out[134]: 0
In [135]: next(it)
----------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-135-bc1ab118995a> in <module>
----> 1 next(it)
StopIteration:
15 オブジェクト()
特徴のない新しいオブジェクトを返します。object はすべてのクラスの基本クラスです。
15 object()
返回一个没有特征的新对象。object 是所有类的基类。
より多くのリソース
我整理了更多关于Java,Python的学习资源,视频+文档+源码+项目,零基础和进阶的都有
如果有需要,可以关注我的公众号“IT就自学”回复Python、Java免费获取哦
更多资源也可以直接私信我!