1.8.1 Built-in Atomic Data Types
Python has two main built-in numeric classes(内置函数,int 和 float) that implement the integer and floating point data types。
other operation are remainder operator %(取模运算),the operator ** is exponentiation(指数运算)
python 中的变量 variable 不像C++、java一样,同一个类型的variable能够refer to many different types of data
1.8.2. Built-in Collection Data Types:
Lists, strings, and tuples (tuples 元组)are ordered collections that are very similar in general structure but have specific differences 。
Sets and dictionaries are unordered collections.
类表,字符串和元组是有序的集合,Sets和字典无序集合
1.list,List中object不需要全部相同类型
.
>>> [1,3,True,6.5] [1, 3, True, 6.5] >>> myList = [1,3,True,6.5] >>> myList [1, 3, True, 6.5] |
, >>> myList = [0] * 6 >>> myList [0, 0, 0, 0, 0, 0] list 列表[1,2,3]*3,说的是将此[1,2,3]重新复制3片,与numpy中array,中* 针对数组中每个元素乘以其常数K import numpy |
List列表的操作特性,List 可以进行pop和insert,pop 弹出的是list中最末尾的元素.
remove 操作可以直接删除list中某个值。()__add__(),called it add method,标识符
2.string:
>>> "David" ‘David‘ >>> myName = "David" >>> myName[3]
‘i‘ >>> myName*2 ‘DavidDavid‘ >>> len(myName) 5 >>>
A major difference between lists and strings is that lists can be modified while strings cannot
list 与string最大不同的点在于List中元素能够进修改,string不能
3.Tuple ,元组中数据不能被修改的 The difference is that a tuple is immutable, like a string
myTuple=(1,True,4.98)
>>> myTuple
(1, True, 4.98)
>>> myTuple*3
(1, True, 4.98, 1, True, 4.98, 1, True, 4.98)
>>> myTuple[1]=9
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
myTuple[1]=9
TypeError: ‘tuple‘ object does not support item assignment
>>>
4.Set集合,集合中元素不会有重复的
1.set 中元素显示不会按照一定的顺序,会一种unordered方式:
{3,6,"cat",4.5,False} {False, 4.5, 3, 6, ‘cat‘} >>> mySet = {3,6,"cat",4.5,False} >>> mySet {False, 4.5, 3, 6, ‘cat‘} >>>
mylist=[1,2,3,1]
>>> set(mylist)
set([1, 2, 3])
intersection从两个set选择相同的元素setA.intersection(setB)
5.词典:dict.items():得到字典中的每一个类型的元素,dict.get(k,0),从词典中有k得到k,没有的话返回成0