天池龙珠计划 Python训练营
所记录的知识点
- bin(十进制表示的负数)
- 指数运算符的优先级最高
- assert(断言)
- enumerate
- 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
欢迎各位同学一起来交流学习心得!