Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 424|回复: 0

python-内置函数及捕获异常

[复制链接]
  • TA的每日心情
    奋斗
    2024-4-6 11:05
  • 签到天数: 748 天

    [LV.9]以坛为家II

    2034

    主题

    2092

    帖子

    70万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    705612
    发表于 2021-9-4 10:24:08 | 显示全部楼层 |阅读模式

     

    eval:把字符串转换成有效表达式

    repr:把有效表达式转换成字符串

     

    round(...)

    round(number[, ndigits]) -> number

     

        Round a number to a given precision in decimal digits (default 0 digits).

        This returns an int when called with one argument, otherwise the

    same type as the number. ndigits may be negative.

    pow(x, y, z=None, /)

    Equivalent to x**y (with two arguments) or x**y % z (with three arguments)

     

    Some types, such as ints, are able to use a more efficient algorithm when

    invoked using the three argument form.

     

    class map(object)

     |  map(func, *iterables) --> map object

     |

     |  Make an iterator that computes the function using arguments from

     |  each of the iterables.  Stops when the shortest iterable is exhausted.

     

    >>> a

    [1, 2, 3]

    >>> b

    ['a', 'b', 'c']

    >>> dict(zip(a,b))

    {1: 'a', 2: 'b', 3: 'c'}

     

    >>> mylist.extend([1,2,3])

    >>> mylist

    [2, 1, 3, 2, 1, 4, 3, 2, 1, 'a', 'a', 1, 2, 3]

    >>> mylist.append([1,2,3])

    >>> mylist

    [2, 1, 3, 2, 1, 4, 3, 2, 1, 'a', 'a', 1, 2, 3, [1, 2, 3]]

     

    -------------------------------------------以上是内置函数------------------------------------------

     

    异常,错误

    程序没法处理的任务,会抛出异常

     

     

    错误一般是,逻辑错误,语法错误,无法生成结果等

     

     

    >>> a

    Traceback (most recent call last):

      File "<stdin>", line 1, in <module>

    NameError: name 'a' is not defined

     

     

    >>> 1 / 0

    Traceback (most recent call last):

      File "<stdin>", line 1, in <module>

    ZeroDivisionError: division by zero

     

    >>> if a = b

      File "<stdin>", line 1

        if a = b

             ^

    SyntaxError: invalid syntax

     

    >>> b[4]

    Traceback (most recent call last):

      File "<stdin>", line 1, in <module>

    IndexError: list index out of range

     

    python,大部分异常都是基于Exception 这个类

     

    KeyboardInterrupt  Exception  exit

    基类 

     

    Exception  

     

    python可以通过 

    try语句检测异常:

    要检测异常的语句

         except 语句来处理异常:

     

    >>> try:

    ...     print(a)

    ... except NameError:

    ...     print('ABC')

    ...

    ABC

     

    >>> try:

    ...     print(a)

    ... except Exception as error:

    ...     print(error)

    ...

    name 'a' is not defined

     

    try:

       要检测异常的语句

    except 检测的异常(如果我们使用Execption)则是捕捉所有异常:#但是不建议大家这么做,因为有些异常是我们需要去真实看到的,

       执行捕获异常之后的事情

    else:

    用来在没有任何异常情况下执行这里的内容

    finally:

       不管异常是否发生,这里面写的语句都会执行

    我们一般用来关闭socket,关闭文件,回收线程,进程创建释放,数据库连接,mysql句柄,做一些对象内存释放的工作,

     

     

    >>> try:

    ...     print(a)

    ... except Exception:

    ...     print('abc')

    ... else:

    ...     print('--------')

    ... finally:

    ...     print('********')

    ...

    abc

    ********

     

    raise 可以抛出异常

     

    断言

    assert当判断的语句为false 那么会抛出一个AssertionError 异常

     

    断言一般用来判断一些bool语句,在断言成功时不采取任何措施,否则触发AssertionError(断言错误)的异常,

     

    try:

    assert 1 == 0

    except AssertionError:

    print('not equal')

     

    with语句,with语句其实和tryfinally语句是一样的,只不过,他只对支持上下文管理协议context

    management protocol,比如我们的mysql链接数据库,会在完成业务之后他有关闭,打开文件也会有关闭文件,

    close()

     

    我们通过with语句打开一个文件对象,如果没有出错,文件对象就是file

    之后我们做的一系列操作不论是否会发生错误,抛出异常,都会执行内存清理的语句,刷新缓冲区,关闭文件等

     

    -----------------------------------------------异常------------------------------------------------

     

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-4-29 23:21 , Processed in 0.063426 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表