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数据处理
28
2
2.4
 有用的工具
type
dir
help
Python
标准库中有几个内置工具,可以帮你确定变量的数据类型或对象类型,并给出这些
变量能做的事情(即它们都有哪些方法)。本节将学习三个工具,它们都是
Python
标准库
的一部分。
2.4.1
 
type
type
可以帮你确定你的对象属于哪种数据类型。想在
Python
代码中做到这一点,将
变量放到
type()
的括号里,例如,如果变量名是
dogs
,那就在
Python
提示符后输入
type(dogs)
。当你用变量保存数据,并想知道变量里的数据是什么类型时,这一方法是非
常有用的。回忆本章前面邮政编码的例子。
对于值
20011
,这里有两种不同的用法。在第一个例子中,它是保存成字符串的邮编。在
第二个例子中,它是一个整数:
'20011'
20011
如果将这两个值保存在变量中,将更难以确定变量的类型,我们可能不知道或不记得用的
是字符串还是整数。
如果将值
传递
给内置方法
type
Python
就会告诉我们对象属于那种数据类型。试一下:
type('20011')
type(20011)
第一行返回的是
str
,第二行返回的是
int
。将列表传递给
type
会返回什么?变量呢?
在你试图排查错误时,或者运行其他人的代码时,确定对象的类型是很有用的。还记得我
们试图从一个列表中减去另一个列表吗(见
2.3.2
节)?好吧,你也不能从一个字符串中
减去另一个字符串。所以,与整数
20011
相比,字符串
'20011'
具有许多不同的方法以及
用例。
2.4.2
 
dir
dir
会返回一个内置方法与属性的列表,帮你列出特定数据类型能做的所有事情 ...
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