Python学习笔记(三)- 介绍Python对象类型(Introducing Python Object Types)

1.说出Python四种核心数据类型
答:数字(Numbers),字符串(strings),字典(dictionaries),元组(tuples),文件(files)和集合(sets)通常被认为是核心(数据)类型。类型(Types),None和布朗值(Booleans)有时也是这样被分类的。有多种数字类型(整型(integer),浮点数(floating point),复数(complex),分数(fraction)和小数(decimal))和多种字符串类型(在Python 2.X 中有简单字符串(simple strings)和Unicode码字符串(unicode strings);在Python 3.X 中有文本字符串(text strings)和字节字符串(字节字符串)。

 

2.为什么它们被称为“核心”数据类型?
答:它们被叫做“核心”类型是因为它们是Python语言本身的一部分,而且总是可得到的;为了创建其他对象,再导入的模块中,你通常必须调用函数。大多数核心类型有特定语法生成对象:例如,‘spam’是一个表达式,创建了一个字符串而且决定了可以应用于它的操作集。因为这个原因,核心类型硬链接到Python语法中。相反,你必须调用内建open函数去调用一个文件(file)对象(即时它也经常被认为是一种核心类型)

 

3.“不可变(immutable)”是什么意思,哪三种Python核心类型被认为是不可变的?
答:一个“不可变”对象,在被创建后不能被改变。数字,字符串和元组在Python归于这一类。虽然你不以改变一个不可变对象,但是你经常通过运行一个表达式来创建一个新的对象。字节数组(Bytearrays)最近为文本提供了可改性,但是他们不是正常的字符串,只有文本是简单的8位(e.g ASCII)才能应用于文本。

 

4.“序列(sequence)”是什么意思,哪三种类型归于这一类?
答:“序列”是按照位置排序的对象集合。字符串,列表和元组在Python中都是序列。它们享用常见的序列操作,例如索引(indexing),拼接(concatenation)和切片(slicing),但是有特定的类型的方法调用。一个相关的术语,“可迭代的(iterable)”意思是一个物理序列(physical sequence),或者按照需求生成其项的虚拟序列(virtual sequence)。


5.“映射(mapping)”是什么意思,那种核心类型是映射?
答:术语“映射”一词表示将键(keys)映射到相关值(values)的对象。Python的字典在核心类型集中是仅有的映射类型。映射不会维持任何从左到右位置顺序;它们支持用键(keys)访问存储的数据,还有特定类型的方法(method)调用。

 

6.“多态(polymorphism)”是什么,为什么你要关心?
答:“多态”一词表示操作的意思取决于被操作的对象。事实证明,这是一个关键的思想(可能是特定的关键思想)背后使用Python很好——如果不将代码是限制为特定类型,则该代码将自动适用于许多类型。

 

标注:转载《Learning Python 5th Edition》[奥莱理]


1. Name four of Python’s core data types.
2. Why are they called “core” data types?
3. What does “immutable” mean, and which three of Python’s core types are considered immutable?
4. What does “sequence” mean, and which three types fall into that category?
5. What does “mapping” mean, and which core type is a mapping?
6. What is “polymorphism,” and why should you care?


1. Numbers, strings, lists, dictionaries, tuples, files, and sets are generally considered to be the core object (data) types. Types, None, and Booleans are sometimes classified this way as well. There are multiple number types (integer, floating point, complex, fraction, and decimal) and multiple string types (simple strings and Unicode strings in Python 2.X, and text strings and byte strings in Python 3.X).
2. They are known as “core” types because they are part of the Python language itself and are always available; to create other objects, you generally must call functions in imported modules. Most of the core types have specific syntax for generating the objects: 'spam', for example, is an expression that makes a string and determines the set of operations that can be applied to it. Because of this, core types are hardwired into Python’s syntax. In contrast, you must call the built-in open function to create a file object (even though this is usually considered a core type too).
3. An “immutable” object is an object that cannot be changed after it is created. Numbers, strings, and tuples in Python fall into this category. While you cannot change an immutable object in place, you can always make a new one by running an expression. Bytearrays in recent Pythons offer mutability for text, but they are not normal strings, and only apply directly to text if it’s a simple 8-bit kind (e.g., ASCII).
4. A “sequence” is a positionally ordered collection of objects. Strings, lists, and tuples are all sequences in Python. They share common sequence operations, such as indexing, concatenation, and slicing, but also have type-specific method calls. A related term, “iterable,” means either a physical sequence, or a virtual one that produces its items on request.
5. The term “mapping” denotes an object that maps keys to associated values. Python’s dictionary is the only mapping type in the core type set. Mappings do not maintain any left-to-right positional ordering; they support access to data stored by key, plus type-specific method calls.
6. “Polymorphism” means that the meaning of an operation (like a +) depends on the objects being operated on. This turns out to be a key idea (perhaps the key idea) behind using Python well—not constraining code to specific types makes that code automatically applicable to many types.


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值