Skip to Content
Python数据处理
book

Python数据处理

by Jacqueline Kazil, Katharine Jarmul
July 2017
Intermediate to advanced
398 pages
11h 54m
Chinese
Posts & Telecom Press
Content preview from Python数据处理
70
4
出循环,这样就完全退出了
for
循环,继续执行下面的脚本。我们来添加
break
停止循环。
for
循环的结尾,添加下列代码,然后重新运行:
if
country == 'Zimbabwe':
break
如果国家名字是津巴布韦的话……
退出
for
循环。
添加了
break
之后,你有没有得到
NameError: name 'country' is not
defined
的错误信息?如果有的话,检查一下代码缩进。
for
循环中的
if
句要缩进四个空格。
逐步运行代码可以帮你发现问题所在。如果你想知道
for
循环中某个变量的
值,比如
country
,可以在
for
循环中添加
print
语句,在脚本报错退出之前
查看变量的值。这样你可能会发现出错的地方在哪里。
现在脚本的输出与我们的最终目标是一致的。我们要做的最后一件事情,是在脚本中添加
一些注释。
注  释
在代码中添加注释,可以让你(或其他人)在以后能够理解代码的含义。添加注释的
方法是在注释前加一个
#
号:
#
这是
Python
注释。
Python
会忽略该行。
添加多行注释的格式如下:
"""
这是多行注释的格式。
如果你的注释很长或者
你想插入一段较长的描述,
你应该使用这种类型的注释。
"""
你的脚本现在应该是这样的:
"""
这是用来分析童工和童婚数据的脚本。
本脚本中用到的Excel文件可以在以下链接中获取:
http://www.unicef.org/sowc2014/numbers/
"""
import ...
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

数据科学中的实用统计学(第2版)

数据科学中的实用统计学(第2版)

Peter Bruce, Andrew Bruce, Peter Gedeck
Java持续交付

Java持续交付

Daniel Bryant, Abraham Marín-Pérez
解密金融数据

解密金融数据

Justin Pauley

Publisher Resources

ISBN: 9787115459190