Python训练营笔记 从变量到异常处理 Day1

简介: 学习笔记 - 天池龙珠计划 - Python 训练营 Task1 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


欢迎各位同学一起来交流学习心得!

目录
相关文章
|
16天前
|
Python
【10月更文挑战第5天】「Mac上学Python 8」基础篇2 - 变量深入详解
本篇将详细介绍Python中变量的使用方式和进阶操作,涵盖变量的输入与输出、变量的多重赋值、变量的内存地址管理以及变量的传递和交换等操作。通过本篇的学习,用户将对变量的使用有更深入的理解,并能灵活运用变量进行各种编程操作。
49 1
【10月更文挑战第5天】「Mac上学Python 8」基础篇2 - 变量深入详解
|
13天前
|
搜索推荐 Python
Leecode 101刷题笔记之第五章:和你一起你轻松刷题(Python)
这篇文章是关于LeetCode第101章的刷题笔记,涵盖了多种排序算法的Python实现和两个中等难度的编程练习题的解法。
16 3
|
16天前
|
存储 编译器 Python
Python--变量、输出与输入
【10月更文挑战第5天】
|
18天前
|
测试技术 Python
Python MagicMock: Mock 变量的强大工具
Python MagicMock: Mock 变量的强大工具
30 4
|
15天前
|
存储 Java 编译器
Python学习三:学习python的 变量命名规则,算数、比较、逻辑、赋值运算符,输入与输出。
这篇文章是关于Python编程语言中变量命名规则、基本数据类型、算数运算符、比较运算符、逻辑运算符、赋值运算符以及格式化输出与输入的详细教程。
18 0
Python学习三:学习python的 变量命名规则,算数、比较、逻辑、赋值运算符,输入与输出。
|
13天前
|
存储 程序员 Python
Python编程入门:探索变量和数据类型
【10月更文挑战第8天】本文是针对初学者的Python编程入门指南,重点介绍Python中变量的定义和使用以及不同的数据类型。我们将通过实例来理解基本概念,并展示如何在Python程序中应用这些知识。文章旨在帮助初学者建立扎实的基础,使他们能够更自信地编写Python代码。
|
13天前
|
算法 C++ Python
Leecode 101刷题笔记之第四章:和你一起你轻松刷题(Python)
这篇博客是关于LeetCode上使用Python语言解决二分查找问题的刷题笔记,涵盖了从基础到进阶难度的多个题目及其解法。
12 0
|
13天前
|
算法 C++ Python
Leecode 101刷题笔记之第三章:和你一起你轻松刷题(Python)
本文是关于LeetCode算法题的刷题笔记,主要介绍了使用双指针技术解决的一系列算法问题,包括Two Sum II、Merge Sorted Array、Linked List Cycle II等,并提供了详细的题解和Python代码实现。
12 0
|
13天前
|
算法 C++ 索引
Leecode 101刷题笔记之第二章:和你一起你轻松刷题(Python)
本文是关于LeetCode 101刷题笔记的第二章,主要介绍了使用Python解决贪心算法题目的方法和实例。
8 0
|
13天前
|
并行计算 Python
Python错误笔记(一):CUDA initialization: CUDA unknown error - this may be due to an incorrectly set up env
这篇文章讨论了CUDA初始化时出现的未知错误及其解决方案,包括重启系统和安装nvidia-modprobe。
55 0