Skip to Content
Python数据分析基础
book

Python数据分析基础

by Clinton W. Brownley
August 2017
Intermediate to advanced
274 pages
7h 44m
Chinese
Posts & Telecom Press
Content preview from Python数据分析基础
Python
基础
35
try
:
print
("Output #138: {}".format(getMean(my_list2)))
except ZeroDivisionError as
detail:
print
("Output #138 (Error): {}".format(float('nan')))
print
("Output #138 (Error): {}".format(detail))
在这种处理异常的方法中,函数
getMean()
中没有检验序列是否包含数值的
if
句。如果序列是空的,就像列表
my_list2
一样,那么调用这个函数会导致一个异常
ZeroDivisionError
要想使用
try-except
代码块,需要将你要执行的代码放在
try
代码块中,然后,使用
except
代码块来处理潜在的错误并打印出错误信息来帮助你理解程序错误。在某些情况
下,异常具有特定的值。你可以通过在
except
行中加上
as
短语来引用异常值,然后打印
出你为异常指定的变量。因为
my_list2
中不包含任何数值,所以执行
except
代码块,打
印出
nan
Error: float division by zero
9.
try-except-else-finally
# 完整形式
try
:
result
= getMean(my_list2)
except ZeroDivisionError as
detail:
print
("Output #142 (Error): {}" ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

数据驱动力:企业数据分析实战

数据驱动力:企业数据分析实战

Carl Anderson
大数据项目管理:从规划到实现

大数据项目管理:从规划到实现

Ted Malaska, Jonathan Seidman

Publisher Resources

ISBN: 9787115463357