天天看點

Python訓練營筆記 從變量到異常處理 Day1

天池龍珠計劃 Python訓練營

所記錄的知識點

  1. bin(十進制表示的負數)
  2. 指數運算符的優先級最高
  3. assert(斷言)
  4. enumerate
  5. finally else

1、bin(十進制表示的負數)

bin(十進制表示的負數)時,輸出的結果是 負号 + 對應正數的原碼
In [1]: bin(33)
Out[1]: '0b100001'

In [2]: bin(-33)
Out[2]: '-0b100001'

In [3]: help(bin)
Help on built-in function bin in module builtins:

bin(number, /)
    Return the binary representation of an integer.

    >>> bin(2796202)
    '0b1010101010101010101010'           

2、指數運算符的優先級最高

-4 ** 2的運算順序是 -(4 ** 2)=-16
In [1]: -4 ** 2
Out[1]: -16

In [2]: (-4) **2
Out[2]: 16

In [3]: 2 ** -4
Out[3]: 0.0625

In [4]: 2 ** -1
Out[4]: 0.5           

3、assert(斷言)

assert False時,抛出AssertionError
In [1]: assert True

In [2]: assert False
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-2-a871fdc9ebee> in <module>
----> 1 assert False

AssertionError:

In [3]: assert False,"find a error"
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-3-1b8239f2dd88> in <module>
----> 1 assert False,"find a error"

AssertionError: find a error

In [4]: help(AssertionError)
Help on class AssertionError in module builtins:

class AssertionError(Exception)
 |  Assertion failed.
 |
 |  Method resolution order:
 |      AssertionError
 |      Exception
 |      BaseException
 |      object
 |
 |  Methods defined here:
 |
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from BaseException:
 |
 |  __delattr__(self, name, /)
 |      Implement delattr(self, name).
 |
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |
 |  __reduce__(...)
 |      Helper for pickle.
 |
 |  __repr__(self, /)
 |      Return repr(self).
 |
 |  __setattr__(self, name, value, /)
 |      Implement setattr(self, name, value).
 |
 |  __setstate__(...)
 |
 |  __str__(self, /)
 |      Return str(self).
 |
 |  with_traceback(...)
 |      Exception.with_traceback(tb) --
 |      set self.__traceback__ to tb and return self.
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from BaseException:
 |
 |  __cause__
 |      exception cause
 |
 |  __context__
 |      exception context
 |
 |  __dict__
 |
 |  __suppress_context__
 |
 |  __traceback__
 |
 |  args           

4、enumerate

In [1]: my_list = ["good","good","study"]

In [2]: for ele in my_list:
   ...:     print(ele)
   ...:
good
good
study

In [3]: for ele in enumerate(my_list):
   ...:     print(ele)
   ...:
(0, 'good')
(1, 'good')
(2, 'study')

In [4]: for index,ele in enumerate(my_list):
   ...:     print(index,"---",ele)
   ...:
0 --- good
1 --- good
2 --- study

In [5]: for index,ele in enumerate(my_list,3):
   ...:     print(index,"---",ele)
   ...:
3 --- good
4 --- good
5 --- study

In [6]: for ele in enumerate(my_list):
   ...:     print(ele,type(ele))
   ...:
(0, 'good') <class 'tuple'>
(1, 'good') <class 'tuple'>
(2, 'study') <class 'tuple'>

In [7]: print(type(enumerate(my_list)))
<class 'enumerate'>

In [8]: help(enumerate)
Help on class enumerate in module builtins:

class enumerate(object)
 |  enumerate(iterable, start=0)
 |
 |  Return an enumerate object.
 |
 |    iterable
 |      an object supporting iteration
 |
 |  The enumerate object yields pairs containing a count (from start, which
 |  defaults to zero) and a value yielded by the iterable argument.
 |
 |  enumerate is useful for obtaining an indexed list:
 |      (0, seq[0]), (1, seq[1]), (2, seq[2]), ...
 |
 |  Methods defined here:
 |
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |
 |  __iter__(self, /)
 |      Implement iter(self).
 |
 |  __next__(self, /)
 |      Implement next(self).
 |
 |  __reduce__(...)
 |      Return state information for pickling.
 |
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.           

5、finally else

try-except-finally:無論try中有沒有異常,finally中的代碼都會執行

try-except-else:當try中沒有異常時,才會執行else中的代碼

In [2]: try:
   ...:    print("ok")
   ...: except BaseException as e:
   ...:    print(e)
   ...: else:
   ...:    print("else")
   ...:
ok
else

In [3]: try:
   ...:    raise NameError('HiThere')
   ...: except BaseException as e:
   ...:    print(e)
   ...: else:
   ...:    print("else")
   ...:
HiThere

In [4]: try:
   ...:    print("ok")
   ...: except BaseException as e:
   ...:    print(e)
   ...: finally:
   ...:    print("finally")
   ...:
ok
finally

In [5]: try:
   ...:    raise NameError('HiThere')
   ...: except BaseException as e:
   ...:    print(e)
   ...: finally:
   ...:    print("finally")
   ...:
HiThere
finally           

歡迎各位同學一起來交流學習心得!