《Python学习手册》(二)

《Python学习手册》(二)

——类型和运算

数字

十六进制 八进制 二进制
0x 0o 0b
hex() oct() bin()
>>>int('10',2)
2
>>>int('10',16)
16
>>>int(3.14159)
3
>>>float(3)
3.0

about yield
http://www.cnblogs.com/tqsummer/archive/2010/12/27/1917927.html

about lambda:non-understanding

about str,repr
http://www.guokr.com/post/91890/

5/-2 python 2.6 python 3.0
'/' -3 -2.5
'//' -3 -3

for both 3.0 & 2.6:

>>>import math
>>>math.trunc(5/-2)
-2

将整数转化为8进制和16进制的字符串:

>>> '{0:o}, {1:x}, {2:b}'.format(64, 64, 64)
'100, 40, 1000000'

>>> '%o, %x, %X' % (64, 255, 255)
'100, ff, FF'

求二进制的位数

>>>X = 99
>>>bin(X), X.bit_length()
('0b1100011', 7)

python内置函数:

pow(), abs(), sum((1, 2, 3, 4)), max(), min(), round()...

>>>round(2.567, 2)
2.57
>>> '%.1f' %2.567, '{0:.2f}'.format(2.567)
('2.6', '2.57')

math模块:

math.pi, math.e
math.sin(),math.sqrt(), math.floor(), math.trunc()

random模块:

import random
random.random()
random.randint(1, 10)
random.choice(['Brian', 'Grail', 'Life'])

小数

>>> 0.1 + 0.1 + 0.1 - 0.3
5.551115123125783e-17

>>> from decimal import Decimal
>>> Decimal('0.1') + Decimal('0.1') + Decimal('0.1') - Decimal('0.3')
Decimal('0.0')

>>> Decimal('0.1') + Decimal('0.1') + Decimal('0.1') - Decimal('0.30')
Decimal('0.00')

从一个浮点对象创建一个小数对象:
	decimal.Decimal.from_float(1.25)

设全局精度:适用于调用线程中创建的所有小数
	decimal.getcontext().prec = 4

设临时精度:
>>> with decimal.localcontext() as ctx:
...		ctx.prec = 2
...		decimal.Decimal('1.00') / decimal.Decimal('3.00')
...
Decimal('0.33')

分数

>>> from fractions import Fraction
>>> x = Fraction(1, 3)

>>> x
Fraction(1, 3)

>>> print(x)
1/3

>>> Fraction('.25')
Fraction(1, 4)

转换和混合类型

>>> (2.5).as_integer_ratio()
(5, 2)

>>> f = 2.5
>>> z = Fraction(*f.as_integer_ratio())
>>> z
Fraction(5, 2)

>>>Fraction.from_float(1.75)
Fraction(7, 4)

>>> x = Fraction(1, 3)
>>> a = x + Fraction(*(4.0 / 3).as_integer_ratio())
>>> a
Fraction(22517998136852479, 13510798882111488)   # Precision loss from float	

>>> a.limit_denominator(10)					# 限制最大分母
Fraction(5, 3)

集合

in python 2.6

x = set('abcde')
>>> x
set(['a', 'c', 'b', 'e', 'd'])


# operations
(
	'e' in x

	x - y			# difference

	x | y			# union

	x & y			# intersection

	x ^ y			#symmetric difference (XOR)
	
	x > y, x < y 	# superset, subset

# methods
(
	x.interaction(y) 	# same as x & y; '-', '|', '^' just like so
	x.issubset(range(1, 5))
	z.add('SPAM')		# insert one item
	z.update(set(['X', 'Y']))	# merge
	z.remove('b')		# delete one item
	
>>> for item in set('abc'): print(item * 3)
...
aaa
bbb
ccc

notice: set([1, 2, 3]) is set, [1, 2, 3] is list

in python 3.0

We can also build a set in this way:
{1, 2, 3, 5}

>>> type({})
<class 'dict'>

创建空集合:
s = set()

集合解析:
>>> {x ** 2 for x in [1, 2, 3, 4]}
{16, 1, 4, 9}

for both python 2.6 & 3.0:

集合只能包含不可变(即可散列的)对象,因此,列表和字典不能嵌入集合

若需要在另一个集合中存储一个集合,可以调用frozenset,创建一个不可变集合且能嵌套到其他集合中。

集合应用

  1. 去除重复项

    L = [1, 2, 1, 3, 2, 4, 5]

    L = list(set(L))

  2. 遍历图形或其他回环结构时,用来记录已经访问过的位置

  3. 处理较大的数据集合(例如数据库查询结果)

数字扩展

NumPy 提供了高级的数字编程工具,例如矩形数据类型、向量处理和高级的计算库

posted @ 2016-02-10 22:48  Christen  阅读(215)  评论(0编辑  收藏  举报