前三章介绍Python的历史,特性,以及相关编辑器等关系,暂时不考虑研究,如有需要可以参考wiki百科,自行阅读
其中一些取自Wiki的资料,可以了解一下
我们日常使用比较多的是C版本的Python,也就是用C语言实现的Python解释器,除此之外,还有Java实现的Jython和.net实现的IronPython
第四章 Introducing Python Object Types
Python’s Core Data Types
Python 核心数据类型
Numbers 1234, 3.1415, 999L, 3+4j, Decimal
Strings 'spam', "guido's"
Lists [1, [2, 'three'], 4]
Dictionaries {'food': 'spam', 'taste': 'yum'}
Tuples (1,'spam', 4, 'U')
Files myfile = open('eggs', 'r')
Other types Sets, types, None, Booleans
Python的动态在于,可以将一个变量指向不同的类型,静态在于调用方法时,必须是指定类型才可以进行调用
Number类型,相关的模块
Math--提供与Java类似的Java包功能
random--随机数,功能比Java使用更方便一些,如random.random(), random.choice([1,3,4,5,6])
字符串,简单的sequences of one-character strings 字符链表,可以直接使用下标的方式,访问指定索引的字
符,注意Python下不包含char类型
[1:3] 链表中所用的切片取值
也可以使用* 与+ 来进行字符串的拼接,不过需要注意,字符串是不可修改的,所有的操作将会返回一个新的对
象,代表为,s[0]='3' 将会抛出异常
在Python中,Number,string,tuples都是不可修改的, list 和dictionaries是可以修改的
List,tuples,string中很多方法都是共同拥有的,可以使用dir查看所拥有的属性与方法
'''xx''' 用于定义多行字符串,多用于HTML/CSS,以及docString
Python支持使用r和u关键字修饰字符串: 如u'xxx',其中u代表Unicode r代表2进制流格式,主要是用于忽视/
的转义符
正则模式匹配
使用re模块,需要import
如
match = re.match('Hello[ \t]*(.*)world', 'Hello Python world')
match.group(1)
List,可以修改的序列,可以动态的收缩和添加,对对象本身方法的调用将会影响到对象本身
使用的时候,需要注意区域检查,防止索引越界,尤其进行添加元素时候,需要使用append方法,移除的话,可以使
用pop或者remove方法
List中可以随意嵌套其他类型,如List
一种比较特殊的List遍历取值方式
M=[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
col2 = [row[1] for row in M]
col2==[2, 5, 8],区域每个元素中的第2个字符串,不知可否多重进行嵌套,不过增加了不少代码复杂度
上述需要注意[]的使用,下面加上过滤条件的例子
[row[1] for row in M if row[1] % 2 == 0]
[c * 2 for c in 'spam'] //对结果进行计算
Dictionaries 字典, 定义的格式 d={key:value}
其中key一般使用字符串表示,value可以为任意的类型
可以通过将对象的引用变量 =0,将一个对象回收 //并不确认
字典的循环, 使用keys()方法,获取到字典所有的key,然后在循环keys中进行访问
Ks = D.keys()
for key in Ks:
print key, '=>', D[key]
在进行循环的时候,可以注意效率的写法
>>> squares = []
>>> for x in [1, 2, 3, 4, 5]: # This is what a list comp does
squares.append(x ** 2)
>>> squares
这样只会创建一个List对象
使用字典时,也要防止出现key不存在,而导致的访问错误
D.has_key('f'),进行判断
Tuples,元组 定义方式(), 类似List的数据类型,区别在于值不可修改,而且定义单元素的tuples时,需要注意
使用,逗号, 常用于参数传递时,防止被修改
Files 类型
可以使用内置的open函数,传入方法名+打开方式进行文件的创建
f = open('data.txt', 'w') //w 代表写入,如果不存在将会创建,如果存在将会被覆盖.
主要使用close方法进行关闭
具体方法可以使用help 与dir进行查看
dir(file)
help(file.seek)
其他内置类型
Sets 集
X = set('spam'),其中包含的内容都是唯一的
decimal 小数,代表高精度的小数运算格式
Booleans 布尔 包含 False True, 注意首字符大写,转换成int类型时,分别为0,1
None类型, 类似Null的存在
type(),同样也返回一个特殊的type类型:type(type(False))进行获取.
下面复制书上一些章节的问题,都是不错的题目,用空可以复习一下(其实我是为了凑字数)
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?
Quiz Answers
1. Numbers, strings, lists, dictionaries, tuples, and files are generally considered to
be the core object (data) types. Sets, types, None, and Booleans are sometimes
classified this way as well. There are multiple number types (integer, long, floating
point, and decimal) and two string types (normal and Unicode).
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.
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.
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.
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.
其中一些取自Wiki的资料,可以了解一下
我们日常使用比较多的是C版本的Python,也就是用C语言实现的Python解释器,除此之外,还有Java实现的Jython和.net实现的IronPython
第四章 Introducing Python Object Types
Python’s Core Data Types
Python 核心数据类型
Numbers 1234, 3.1415, 999L, 3+4j, Decimal
Strings 'spam', "guido's"
Lists [1, [2, 'three'], 4]
Dictionaries {'food': 'spam', 'taste': 'yum'}
Tuples (1,'spam', 4, 'U')
Files myfile = open('eggs', 'r')
Other types Sets, types, None, Booleans
Python的动态在于,可以将一个变量指向不同的类型,静态在于调用方法时,必须是指定类型才可以进行调用
Number类型,相关的模块
Math--提供与Java类似的Java包功能
random--随机数,功能比Java使用更方便一些,如random.random(), random.choice([1,3,4,5,6])
字符串,简单的sequences of one-character strings 字符链表,可以直接使用下标的方式,访问指定索引的字
符,注意Python下不包含char类型
[1:3] 链表中所用的切片取值
也可以使用* 与+ 来进行字符串的拼接,不过需要注意,字符串是不可修改的,所有的操作将会返回一个新的对
象,代表为,s[0]='3' 将会抛出异常
在Python中,Number,string,tuples都是不可修改的, list 和dictionaries是可以修改的
List,tuples,string中很多方法都是共同拥有的,可以使用dir查看所拥有的属性与方法
'''xx''' 用于定义多行字符串,多用于HTML/CSS,以及docString
Python支持使用r和u关键字修饰字符串: 如u'xxx',其中u代表Unicode r代表2进制流格式,主要是用于忽视/
的转义符
正则模式匹配
使用re模块,需要import
如
match = re.match('Hello[ \t]*(.*)world', 'Hello Python world')
match.group(1)
List,可以修改的序列,可以动态的收缩和添加,对对象本身方法的调用将会影响到对象本身
使用的时候,需要注意区域检查,防止索引越界,尤其进行添加元素时候,需要使用append方法,移除的话,可以使
用pop或者remove方法
List中可以随意嵌套其他类型,如List
一种比较特殊的List遍历取值方式
M=[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
col2 = [row[1] for row in M]
col2==[2, 5, 8],区域每个元素中的第2个字符串,不知可否多重进行嵌套,不过增加了不少代码复杂度
上述需要注意[]的使用,下面加上过滤条件的例子
[row[1] for row in M if row[1] % 2 == 0]
[c * 2 for c in 'spam'] //对结果进行计算
Dictionaries 字典, 定义的格式 d={key:value}
其中key一般使用字符串表示,value可以为任意的类型
可以通过将对象的引用变量 =0,将一个对象回收 //并不确认
字典的循环, 使用keys()方法,获取到字典所有的key,然后在循环keys中进行访问
Ks = D.keys()
for key in Ks:
print key, '=>', D[key]
在进行循环的时候,可以注意效率的写法
>>> squares = []
>>> for x in [1, 2, 3, 4, 5]: # This is what a list comp does
squares.append(x ** 2)
>>> squares
这样只会创建一个List对象
使用字典时,也要防止出现key不存在,而导致的访问错误
D.has_key('f'),进行判断
Tuples,元组 定义方式(), 类似List的数据类型,区别在于值不可修改,而且定义单元素的tuples时,需要注意
使用,逗号, 常用于参数传递时,防止被修改
Files 类型
可以使用内置的open函数,传入方法名+打开方式进行文件的创建
f = open('data.txt', 'w') //w 代表写入,如果不存在将会创建,如果存在将会被覆盖.
主要使用close方法进行关闭
具体方法可以使用help 与dir进行查看
dir(file)
help(file.seek)
其他内置类型
Sets 集
X = set('spam'),其中包含的内容都是唯一的
decimal 小数,代表高精度的小数运算格式
Booleans 布尔 包含 False True, 注意首字符大写,转换成int类型时,分别为0,1
None类型, 类似Null的存在
type(),同样也返回一个特殊的type类型:type(type(False))进行获取.
下面复制书上一些章节的问题,都是不错的题目,用空可以复习一下(其实我是为了凑字数)
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?
Quiz Answers
1. Numbers, strings, lists, dictionaries, tuples, and files are generally considered to
be the core object (data) types. Sets, types, None, and Booleans are sometimes
classified this way as well. There are multiple number types (integer, long, floating
point, and decimal) and two string types (normal and Unicode).
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.
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.
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.
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.