1.2.2. 基本类型

1.2.2.1. 数值类型

Python supports the following numerical, scalar types:

Integer:
>>> 1 + 1
2
>>> a = 4
>>> type(a)
<type 'int'>
浮点数:
>>> c = 2.1
>>> type(c)
<type 'float'>
复数:
>>> a = 1.5 + 0.5j
>>> a.real
1.5
>>> a.imag
0.5
>>> type(1. + 0j)
<type 'complex'>
Booleans:
>>> 3 > 4
False
>>> test = (3 > 4)
>>> test
False
>>> type(test)
<type 'bool'>

因此,Python shell可以代替你的便携式计算器,其基本的算术运算+-*/(取模)由原生实现

>>> 7 * 3.
21.0
>>> 2**10
1024
>>> 8 % 3
2

类型转换(casting):

>>> float(1)
1.0

Warning

Integer division

In Python 2:

>>> 3 / 2   
1

In Python 3:

>>> 3 / 2   
1.5

为了安全起见:使用浮点数:

>>> 3 / 2.
1.5
>>> a = 3
>>> b = 2
>>> a / b # In Python 2
1
>>> a / float(b)
1.5

Future behavior: to always get the behavior of Python3

>>> from __future__ import division  
>>> 3 / 2
1.5

如果你明确想要整数除法,使用//

>>> 3.0 // 2
1.0

The behaviour of the division operator has changed in Python 3.

1.2.2.2. 容器

Python提供了许多高效的容器类型,其中可以存储对象的集合。

1.2.2.2.1. 列表

A list is an ordered collection of objects, that may have different types. For example:

>>> colors = ['red', 'blue', 'green', 'black', 'white']
>>> type(colors)
<type 'list'>

Indexing: accessing individual objects contained in the list:

>>> colors[2]
'green'

尾部计数使用负索引:

>>> colors[-1]
'white'
>>> colors[-2]
'black'

Warning

索引从0开始(如在C中),而不是1(如在Fortran或Matlab中)!

Slicing: obtaining sublists of regularly-spaced elements:

>>> colors
['red', 'blue', 'green', 'black', 'white']
>>> colors[2:4]
['green', 'black']

Warning

注意,colors[start:stop]包含索引为i的元素,即start <= i < stopi的范围为从startstop-1)。因此,colors[start:stop]具有(stop - start)个元素 。

Slicing syntax: colors[start:stop:stride]

All slicing parameters are optional:

>>> colors
['red', 'blue', 'green', 'black', 'white']
>>> colors[3:]
['black', 'white']
>>> colors[:3]
['red', 'blue', 'green']
>>> colors[::2]
['red', 'green', 'white']

Lists are mutable objects and can be modified:

>>> colors[0] = 'yellow'
>>> colors
['yellow', 'blue', 'green', 'black', 'white']
>>> colors[2:4] = ['gray', 'purple']
>>> colors
['yellow', 'blue', 'gray', 'purple', 'white']

Note

The elements of a list may have different types:

>>> colors = [3, -200, 'hello']
>>> colors
[3, -200, 'hello']
>>> colors[1], colors[2]
(-200, 'hello')

For collections of numerical data that all have the same type, it is often more efficient to use the array type provided by the numpy module. NumPy数组是包含固定大小元素的内存块。With NumPy arrays, operations on elements can be faster because elements are regularly spaced in memory and more operations are performed through specialized C functions instead of Python loops.

Python offers a large panel of functions to modify lists, or query them. Here are a few examples; for more details, see https://docs.python.org/tutorial/datastructures.html#more-on-lists

Add and remove elements:

>>> colors = ['red', 'blue', 'green', 'black', 'white']
>>> colors.append('pink')
>>> colors
['red', 'blue', 'green', 'black', 'white', 'pink']
>>> colors.pop() # removes and returns the last item
'pink'
>>> colors
['red', 'blue', 'green', 'black', 'white']
>>> colors.extend(['pink', 'purple']) # extend colors, in-place
>>> colors
['red', 'blue', 'green', 'black', 'white', 'pink', 'purple']
>>> colors = colors[:-2]
>>> colors
['red', 'blue', 'green', 'black', 'white']

反转:

>>> rcolors = colors[::-1]
>>> rcolors
['white', 'black', 'green', 'blue', 'red']
>>> rcolors2 = list(colors)
>>> rcolors2
['red', 'blue', 'green', 'black', 'white']
>>> rcolors2.reverse() # in-place
>>> rcolors2
['white', 'black', 'green', 'blue', 'red']

Concatenate and repeat lists:

>>> rcolors + colors
['white', 'black', 'green', 'blue', 'red', 'red', 'blue', 'green', 'black', 'white']
>>> rcolors * 2
['white', 'black', 'green', 'blue', 'red', 'white', 'black', 'green', 'blue', 'red']

排序:

>>> sorted(rcolors) # new object
['black', 'blue', 'green', 'red', 'white']
>>> rcolors
['white', 'black', 'green', 'blue', 'red']
>>> rcolors.sort() # in-place
>>> rcolors
['black', 'blue', 'green', 'red', 'white']

Methods and Object-Oriented Programming

记号rcolors.method()(即rcolors.append(3)colors.pop())是我们的第一个面向对象编程(OOP)的例子。作为列表,对象rcolors拥有方法函数,它使用记号.调用。对于阅读本教程,OOP的知识只需理解记号.

Discovering methods:

提醒:在Ipython中:制表符补全(按Tab)

In [28]: rcolors.<TAB>
rcolors.__add__ rcolors.__iadd__ rcolors.__setattr__
rcolors.__class__ rcolors.__imul__ rcolors.__setitem__
rcolors.__contains__ rcolors.__init__ rcolors.__setslice__
rcolors.__delattr__ rcolors.__iter__ rcolors.__sizeof__
rcolors.__delitem__ rcolors.__le__ rcolors.__str__
rcolors.__delslice__ rcolors.__len__ rcolors.__subclasshook__
rcolors.__doc__ rcolors.__lt__ rcolors.append
rcolors.__eq__ rcolors.__mul__ rcolors.count
rcolors.__format__ rcolors.__ne__ rcolors.extend
rcolors.__ge__ rcolors.__new__ rcolors.index
rcolors.__getattribute__ rcolors.__reduce__ rcolors.insert
rcolors.__getitem__ rcolors.__reduce_ex__ rcolors.pop
rcolors.__getslice__ rcolors.__repr__ rcolors.remove
rcolors.__gt__ rcolors.__reversed__ rcolors.reverse
rcolors.__hash__ rcolors.__rmul__ rcolors.sort

1.2.2.2.2. 字符串

不同的字符串语法(单引号、双引号或三引号):

s = 'Hello, how are you?'
s = "Hi, what's up"
s = '''Hello, # tripling the quotes allows the
how are you''' # the string to span more than one line
s = """Hi,
what's up?"""
In [1]: 'Hi, what's up?'
------------------------------------------------------------
File "<ipython console>", line 1
'Hi, what's up?'
^
SyntaxError: invalid syntax

The newline character is \n, and the tab character is \t.

字符串是类似列表的集合。因此,它们可以使用相同的语法和规则索引和切片。

Indexing:

>>> a = "hello"
>>> a[0]
'h'
>>> a[1]
'e'
>>> a[-1]
'o'

(记住负索引对应于从右端计数。)

Slicing:

>>> a = "hello, world!"
>>> a[3:6] # 3rd to 6th (excluded) elements: elements 3, 4, 5
'lo,'
>>> a[2:10:2] # Syntax: a[start:stop:step]
'lo o'
>>> a[::3] # every three characters, from beginning to end
'hl r!'

重音符号和特殊字符也可以在Unicode字符串中处理(请参阅https://docs.python.org/tutorial/introduction.html#unicode-strings)。

A string is an immutable object and it is not possible to modify its contents. One may however create new strings from the original one.

In [53]: a = "hello, world!"
In [54]: a[2] = 'z'
---------------------------------------------------------------------------
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
In [55]: a.replace('l', 'z', 1)
Out[55]: 'hezlo, world!'
In [56]: a.replace('l', 'z')
Out[56]: 'hezzo, worzd!'

Strings have many useful methods, such as a.replace as seen above. 记住a.面向对象符号,并使用制表符补全或help(str)搜索新方法。

另见

Python提供了操作字符串、寻找模式或格式化的高级操作。The interested reader is referred to https://docs.python.org/library/stdtypes.html#string-methods and https://docs.python.org/library/string.html#new-string-formatting

字符串格式化:

>>> 'An integer: %i; a float: %f; another string: %s' % (1, 0.1, 'string')
'An integer: 1; a float: 0.100000; another string: string'
>>> i = 102
>>> filename = 'processing_of_dataset_%d.txt' % i
>>> filename
'processing_of_dataset_102.txt'

1.2.2.2.3. 字典

字典基本上是将键映射到值的高效的表。It is an unordered container

>>> tel = {'emmanuelle': 5752, 'sebastian': 5578}
>>> tel['francis'] = 5915
>>> tel
{'sebastian': 5578, 'francis': 5915, 'emmanuelle': 5752}
>>> tel['sebastian']
5578
>>> tel.keys()
['sebastian', 'francis', 'emmanuelle']
>>> tel.values()
[5578, 5915, 5752]
>>> 'francis' in tel
True

It can be used to conveniently store and retrieve values associated with a name (a string for a date, a name, etc.). See https://docs.python.org/tutorial/datastructures.html#dictionaries for more information.

字典可以具有不同类型的键(和相应的值):

>>> d = {'a':1, 'b':2, 3:'hello'}
>>> d
{'a': 1, 3: 'hello', 'b': 2}

1.2.2.2.4. 更多容器类型

Tuples

Tuples are basically immutable lists. The elements of a tuple are written between parentheses, or just separated by commas:

>>> t = 12345, 54321, 'hello!'
>>> t[0]
12345
>>> t
(12345, 54321, 'hello!')
>>> u = (0, 2)

集合:元素无序、唯一:

>>> s = set(('a', 'b', 'c', 'a'))
>>> s
set(['a', 'c', 'b'])
>>> s.difference(('a', 'b'))
set(['c'])

1.2.2.3. 赋值运算符

Python library reference says:

赋值语句用于(重新)将名称绑定到值和修改可变对象的属性或元素。

In short, it works as follows (simple assignment):

  1. 计算右手侧的表达式,创建/获得相应的对象
  2. 在左手侧的名称被赋值或绑定到r.h.s.的对象

Things to note:

  • a single object can have several names bound to it:

    In [1]: a = [1, 2, 3]
    
    In [2]: b = a
    In [3]: a
    Out[3]: [1, 2, 3]
    In [4]: b
    Out[4]: [1, 2, 3]
    In [5]: a is b
    Out[5]: True
    In [6]: b[1] = 'hi!'
    In [7]: a
    Out[7]: [1, 'hi!', 3]
  • 原地更改列表,请使用索引/切片:

    In [1]: a = [1, 2, 3]
    
    In [3]: a
    Out[3]: [1, 2, 3]
    In [4]: a = ['a', 'b', 'c'] # Creates another object.
    In [5]: a
    Out[5]: ['a', 'b', 'c']
    In [6]: id(a)
    Out[6]: 138641676
    In [7]: a[:] = [1, 2, 3] # Modifies object in place.
    In [8]: a
    Out[8]: [1, 2, 3]
    In [9]: id(a)
    Out[9]: 138641676 # Same as in Out[6], yours will differ...
  • 这里的关键概念是可变对象和不可变对象

    • mutable objects can be changed in place
    • immutable objects cannot be modified once created

另见

A very good and detailed explanation of the above issues can be found in David M. Beazley’s article Types and Objects in Python.