Python???freshman02
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python???freshman02相关的知识,希望对你有一定的参考价值。
?????????stat com ascii ?????? enumerate ?????? ?????? ?????? ??????
???????????????https://docs.python.org/3/library/functions.html?highlight=built#abs
??????????????????
??????1.abs()-????????????
??????2.divmod()-????????????????????????
??????3.max()-????????????????????????????????????????????????????????????????????????
1 >>> max(1,2,3) # ??????3????????? ???3??????????????? 2 3 3 >>> max(???1234???) # ??????1?????????????????????????????????????????? 4 ???4??? 5 >>> max(-1,0) # ?????????????????????????????? 6 0 7 >>> max(-1,0,key = abs) # ????????????????????????????????????????????????????????????????????????????????? 8 -1
??????4.min()?????????????????????????????????????????????????????????????????????????????????
??????5.pow()??????????????????????????????????????????????????????????????????
??????6.round()???????????????????????????????????????
??????7.sum()?????????????????????????????????????????????????????????????????????
??????????????????
??????1.bool????????????????????????????????????????????????????????????????????????0?????????????????????False???
??????2.int????????????????????????????????????????????????
??????3.float???????????????????????????????????????????????????
??????4.complex?????????????????????????????????????????????
??????5.str?????????????????????????????????????????????(?????????)
??????6.bytearray??????????????????????????????????????????????????????
1 a = bytes("abcde",encoding="utf-8") 2 # a[0]=50 #TypeError: ???bytes??? object does not support item assignment 3 print(a) #b???abcde??? 4 print(a.capitalize()) #b???Abcde??? 5 b = bytearray("abcde",encoding="utf-8") 6 #?????????????????????(bytes),bytearray?????? 7 print( b[1] ) #98 8 b[1]= 50 #?????????ascii???:50->2,97->a 9 print(b) #bytearray(b???a2cde???)
??????7.bytes???????????????????????????????????????????????????????????????
??????8.memoryview????????????????????????????????????????????????????????????
1 >>>v = memoryview(b???abcefg???) 2 >>> v[1] 3 98 4 >>> v[-1] 5 103
??????9.ord?????????Unicode?????????????????????
??????10.chr???????????????????????????Unicode??????
??????11.bin?????????????????????2???????????????
??????12.oct?????????????????????8??????????????????
??????13.hex?????????????????????16??????????????????
1 >>> ord(???a???) 2 97 3 >>> chr(97) #????????????????????? 4 ???a??? 5 >>> bin(3) 6 ???0b11??? 7 >>> oct(10) 8 ???0o12??? 9 >>> hex(15) 10 ???0xf???
??????14.tuple????????????????????????????????????????????????
??????15.list????????????????????????????????????????????????
??????16.dict???????????????????????????????????????????????????
1 >>>a={6:2,8:0,1:4,-5:6,99:11,4:22} 2 >>>print( sorted(a.items()) )# sorted(a)???key??????????????????key??? 3 [(-5,6),(1,4),(4,22),(6,2),(8,0),(99,11)]
4 >>>print( sorted(a.items()),key=lambda x:x[1] )#???value?????????
5 [(8,0),(6,2),(1,4),(-5,6),(99,11),(4,22)]
??????17.set????????????????????????????????????????????????
1 >>> tuple() #????????????????????????????????? 2 () 3 >>> tuple(???121???) #????????????????????????????????????????????????????????? 4 (???1???, ???2???, ???1???) 5 6 >>>list() # ????????????????????????????????? 7 [] 8 >>> list(???abcd???) # ????????????????????????????????????????????????????????? 9 [???a???, ???b???, ???c???, ???d???] 10 11 >>> dict() # ????????????????????????????????????????????? 12 {} 13 >>> dict(a = 1,b = 2) # ???????????????????????????????????? 14 {???b???: 2, ???a???: 1} 15 >>> dict(zip([???a???,???b???],[1,2])) # ??????????????????????????????????????? 16 {???b???: 2, ???a???: 1} 17 >>> dict(((???a???,1),(???b???,2))) # ?????????????????????????????????????????? 18 {???b???: 2, ???a???: 1} 19 20 >>>set() # ????????????????????????????????? 21 set() 22 >>> a = set(range(10)) # ???????????????????????????????????? 23 >>> a 24 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
??????18.frozenset?????????????????????????????????????????????????????????
>>> a = frozenset(range(10)) >>> a frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
??????19.enumerate??????????????????????????????????????????
>>> seasons = [???Spring???, ???Summer???, ???Fall???, ???Winter???] >>> list(enumerate(seasons)) [(0, ???Spring???), (1, ???Summer???), (2, ???Fall???), (3, ???Winter???)] >>> list(enumerate(seasons, start=1)) #??????????????? [(1, ???Spring???), (2, ???Summer???), (3, ???Fall???), (4, ???Winter???)]
??????20.range??????????????????????????????????????????range??????
>>> a = range(10) >>> b = range(1,10) >>> c = range(1,10,3) >>> a,b,c # ????????????a,b,c (range(0, 10), range(1, 10), range(1, 10, 3)) >>> list(a),list(b),list(c) # ????????????a,b,c????????? ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 4, 7]) >>>
??????21.iter?????????????????????????????????????????????????????????
>>> a = iter(???abcd???) #??????????????? >>> a <str_iterator object at 0x03FB4FB0> >>> next(a) ???a??? >>> next(a) ???b??? >>> next(a) ???c??? >>> next(a) ???d??? >>> next(a) Traceback (most recent call last): File "<pyshell#29>", line 1, in <module> next(a) StopIteration
??????22.slice???????????????????????????????????????????????????????????????
>>> c1 = slice(5) # ??????c1 >>> c1 slice(None, 5, None) >>> c2 = slice(2,5) # ??????c2 >>> c2 slice(2, 5, None) >>> c3 = slice(1,10,3) # ??????c3 >>> c3 slice(1, 10, 3)
??????23.super??????????????????????????????????????????????????????????????????????????????
#????????????A >>> class A(object): def __init__(self): print(???A.__init__???) #????????????B?????????A >>> class B(A): def __init__(self): print(???B.__init__???) super().__init__() #super?????????????????? >>> b = B() B.__init__ A.__init__
??????24.object?????????????????????object??????
>>> a = object() >>> a.name = ???kim??? # ?????????????????? Traceback (most recent call last): File "<pyshell#9>", line 1, in <module> a.name = ???kim??? AttributeError: ???object??? object has no attribute ???name???
??????????????????
??????1.all???????????????????????????????????????????????????True?????????0???True???
>>> all([1,2]) #????????????????????????????????????True?????????True True >>> all([0,1,2]) #?????????0???????????????False?????????False False >>> all(()) #????????? True >>> all({}) #????????? True ??????
??????2.any?????????????????????????????????????????????True????????????(??????????????????)
??????3.filter???????????????????????????????????????????????????
>>> a = list(range(1,10)) #???????????? >>> a [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> def if_odd(x): #???????????????????????? return x%2==1 >>> list(filter(if_odd,a)) #???????????????????????? [1, 3, 5, 7, 9]
??????4.map???????????????????????????????????????????????????????????????????????????????????????????????????
>>> a = map(ord,???abcd???) >>> a <map object at 0x03994E50> >>> list(a) [97, 98, 99, 100]
??????5.next????????????????????????????????????????????????
??????6.reversed??????????????????????????????????????????
??????7.sorted????????????????????????????????????????????????????????????
??????8.zip???????????????????????????????????????????????????????????????????????????????????????????????????
1 >>> x = [1,2,3] #??????3 2 >>> y = [4,5,6,7,8] #??????5 3 >>> list(zip(x,y)) # ???????????????3 4 [(1, 4), (2, 5), (3, 6)]
??????????????????
??????1.help??????????????????????????????
??????2.dir??????????????????????????????????????????????????????
??????3.id?????????????????????????????????
??????4.hash???????????????????????????
??????5.type??????????????????????????????????????????????????????????????????????????????
??????6.len????????????????????????
>>> len(???abcd???) # ????????? >>> len(bytes(???abcd???,???utf-8???)) # ???????????? >>> len((1,2,3,4)) # ?????? >>> len([1,2,3,4]) # ?????? >>> len(range(1,5)) # range?????? >>> len({???a???:1,???b???:2,???c???:3,???d???:4}) # ?????? >>> len({???a???,???b???,???c???,???d???}) # ?????? >>> len(frozenset(???abcd???)) #???????????????
??????7.ascii???????????????????????????????????????????????????
>>> ascii(1) ???1??? >>> ascii(???&???) "???&???" >>> ascii(9000000) ???9000000??? >>> ascii(????????????) #???ascii?????? "???\u4e2d\u6587???"
??????8.format?????????????????????
#?????????????????????????????? ???s??? None >>> format(???some string???,???s???) ???some string??? >>> format(???some string???) ???some string??? #???????????????????????????????????? ???b??? ???c??? ???d??? ???o??? ???x??? ???X??? ???n??? None >>> format(3,???b???) #?????????????????? ???11??? >>> format(97,???c???) #??????unicode????????? ???a??? >>> format(11,???d???) #?????????10?????? ???11??? >>> format(11,???o???) #?????????8?????? ???13??? >>> format(11,???x???) #?????????16?????? ?????????????????? ???b??? >>> format(11,???X???) #?????????16?????? ?????????????????? ???B??? >>> format(11,???n???) #???d?????? ???11??? >>> format(11) #?????????d?????? ???11??? #????????????????????????????????? ???e??? ???E??? ???f??? ???F??? ???g??? ???G??? ???n??? ???%??? None >>> format(314159267,???e???) #??????????????????????????????6????????? ???3.141593e+08??? >>> format(314159267,???0.2e???) #??????????????????????????????2????????? ???3.14e+08??? >>> format(314159267,???0.2E???) #??????????????????????????????2????????????????????????E?????? ???3.14E+08??? >>> format(314159267,???f???) #?????????????????????????????????6????????? ???314159267.000000??? >>> format(3.14159267000,???f???) #?????????????????????????????????6????????? ???3.141593??? >>> format(3.14159267000,???0.8f???) #?????????????????????????????????8????????? ???3.14159267??? >>> format(3.14159267000,???0.10f???) #?????????????????????????????????10????????? ???3.1415926700??? >>> format(3.14e+1000000,???F???) #??????????????????????????????????????????????????? ???INF??? #g?????????????????????????????????p???????????????????????????????????????????????????????????????????????????????????????????????????exp?????????-4<=exp<p???????????????????????????????????????p-1-exp???????????????????????????????????????????????????p-1?????????????????? >>> format(0.00003141566,???.1g???) #p=1,exp=-5 ==??? -4<=exp<p?????????????????????????????????????????????0???????????? ???3e-05??? >>> format(0.00003141566,???.2g???) #p=1,exp=-5 ==??? -4<=exp<p?????????????????????????????????????????????1???????????? ???3.1e-05??? >>> format(0.00003141566,???.3g???) #p=1,exp=-5 ==??? -4<=exp<p?????????????????????????????????????????????2???????????? ???3.14e-05??? >>> format(0.00003141566,???.3G???) #p=1,exp=-5 ==??? -4<=exp<p?????????????????????????????????????????????0???????????????E???????????? ???3.14E-05??? >>> format(3.1415926777,???.1g???) #p=1,exp=0 ==??? -4<=exp<p??????????????????????????????????????????0???????????? ???3??? >>> format(3.1415926777,???.2g???) #p=1,exp=0 ==??? -4<=exp<p??????????????????????????????????????????1???????????? ???3.1??? >>> format(3.1415926777,???.3g???) #p=1,exp=0 ==??? -4<=exp<p??????????????????????????????????????????2???????????? ???3.14??? >>> format(0.00003141566,???.1n???) #???g?????? ???3e-05??? >>> format(0.00003141566,???.3n???) #???g?????? ???3.14e-05??? >>> format(0.00003141566) #???g?????? ???3.141566e-05???
??????9.vars??????????????????????????????????????????????????????????????????????????????????????????????????????
??????????????????
- __import__?????????????????????
- isinstance????????????????????????????????????????????????????????????????????????
- issubclass?????????????????????????????????????????????????????????????????????????????????
>>> issubclass(bool,int) True >>> issubclass(bool,str) False >>> issubclass(bool,(str,int)) True
- hasattr?????????????????????????????????
#?????????A >>> class Student: def __init__(self,name): self.name = name >>> s = Student(???Aim???) >>> hasattr(s,???name???) #a??????name?????? True >>> hasattr(s,???age???) #a?????????age?????? False
- getattr???????????????????????????
#?????????Student >>> class Student: def __init__(self,name): self.name = name >>> getattr(s,???name???) #????????????name ???Aim??? >>> getattr(s,???age???,6) #???????????????age?????????????????????????????????????????? >>> getattr(s,???age???) #???????????????age???????????????????????????????????? Traceback (most recent call last): File "<pyshell#17>", line 1, in <module> getattr(s,???age???) AttributeError: ???Stduent??? object has no attribute ???age???
- setattr???????????????????????????
>>> class Student: def __init__(self,name): self.name = name >>> a = Student(???Kim???) >>> a.name ???Kim??? >>> setattr(a,???name???,???Bob???) >>> a.name ???Bob???
- delattr????????????????????????
#?????????A >>> class A: def __init__(self,name): self.name = name def sayHello(self): print(???hello???,self.name) #????????????????????? >>> a.name ???????????? >>> a.sayHello() hello ?????? #???????????? >>> delattr(a,???name???) >>> a.name Traceback (most recent call last): File "<pyshell#47>", line 1, in <module> a.name AttributeError: ???A??? object has no attribute ???name???
- callable?????????????????????????????????
>>> class B: #?????????B def __call__(self): print(???instances are callable now.???) >>> callable(B) #???B?????????????????? True >>> b = B() #?????????B >>> callable(b) #??????b?????????????????? True >>> b() #????????????b?????? instances are callable now.
??????????????????
- globals??????????????????????????????????????????????????????????????????
>>> globals() {???__spec__???: None, ???__package__???: None, ???__builtins__???: <module ???builtins??? (built-in)>, ???__name__???: ???__main__???, ???__doc__???: None, ???__loader__???: <class ???_frozen_importlib.BuiltinImporter???>} >>> a = 1 >>> globals() #????????????a {???__spec__???: None, ???__package__???: None, ???__builtins__???: <module ???builtins??? (built-in)>, ???a???: 1, ???__name__???: ???__main__???, ???__doc__???: None, ???__loader__???: <class ???_frozen_importlib.BuiltinImporter???>}
- locals??????????????????????????????????????????????????????????????????
>>> def f(): print(???before define a ???) print(locals()) #????????????????????? a = 1 print(???after define a???) print(locals()) #?????????????????????a???????????????1 >>> f <function f at 0x03D40588> >>> f() before define a {} after define a {???a???: 1}
- print????????????????????????????????????
>>> print(1,2,3) 1 2 3 >>> print(1,2,3,sep = ???+???) 1+2+3 >>> print(1,2,3,sep = ???+???,end = ???=????) 1+2+3=?
- input????????????????????????
>>> s = input(???please input your name:???) please input your name:Ain >>> s ???Ain???
??????????????????
- open????????????????????????????????????????????????????????????????????????
# t??????????????????b?????????????????? >>> a = open(???test.txt???,???rt???) >>> a.read() ???some text??? >>> a.close()
??????????????????
- compile????????????????????????????????????AST???????????????????????????exec?????????????????????eval????????????
>>> #??????????????????exec >>> code1 = ???for i in range(0,10): print (i)??? >>> compile1 = compile(code1,??????,???exec???) >>> exec (compile1) 0 1 2 3 4 5 6 7 8 9 >>> #????????????????????????eval >>> code2 = ???1 + 2 + 3 + 4??? >>> compile2 = compile(code2,??????,???eval???) >>> eval(compile2) 10
- eval????????????????????????????????????????????????????????????????????????????????????
>>> eval(???1+2+3+4???) 10
- exec????????????????????????
>>> exec(???a=1+2???) #???????????? >>> a 3
- repr?????????????????????????????????????????????(????????????)
>>> a = ???some text??? >>> str(a) ???some text??? >>> repr(a) "???some text???"
?????? ?????????
- property???????????????????????????
>>> class C: def __init__(self): self._name = ?????? @property def name(self): """i???m the ???name??? property.""" return self._name @name.setter def name(self,value): if value is None: raise RuntimeError(???name can not be None???) else: self._name = value >>> c = C() >>> c.name # ???????????? ?????? >>> c.name = None # ??????????????????????????? Traceback (most recent call last): File "<pyshell#84>", line 1, in <module> c.name = None File "<pyshell#81>", line 11, in name raise RuntimeError(???name can not be None???) RuntimeError: name can not be None >>> c.name = ???Kim??? # ???????????? >>> c.name # ???????????? ???Kim??? >>> del c.name # ????????????????????????deleter??????????????? Traceback (most recent call last): File "<pyshell#87>", line 1, in <module> del c.name AttributeError: can???t delete attribute >>> c.name ???Kim???
- classmethod???????????????????????????????????????
>>> class C: @classmethod def f(cls,arg1): print(cls) print(arg1) >>> C.f(??????????????????????????????) <class ???__main__.C???> ???????????????????????? >>> c = C() >>> c.f(????????????????????????????????????) <class ???__main__.C???> ??????????????????????????????
- staticmethod??????????????????????????????????????????
# ????????????????????????????????? >>> class Student(object): def __init__(self,name): self.name = name @staticmethod def sayHello(lang): print(lang) if lang == ???en???: print(???Welcome!???) else: print(???????????????) >>> Student.sayHello(???en???) #?????????,???en????????????lang?????? en Welcome! >>> b = Student(???Kim???) >>> b.sayHello(???zh???) #?????????????????????,???zh????????????lang?????? zh ??????
以上是关于Python???freshman02的主要内容,如果未能解决你的问题,请参考以下文章