十六進製制
八進位制二進位制
0x0o
0bhex()
oct()
bin()
>>>int('10',2)
2>>>int('10',16)
16>>>int(3.14159)
3>>>float(3)
3.0
aboutyield:
aboutlambda:non-understanding
aboutstr,repr:
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進製制的字串:
>>> ', , '.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, ''.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
bbbccc
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:
>>> type({})
建立空集合:
s = set()
集合解析:
>>>
for both python 2.6 & 3.0:
集合只能包含不可變(即可雜湊的)物件,因此,列表和字典不能嵌入集合
若需要在另乙個集合中儲存乙個集合,可以呼叫frozenset,建立乙個不可變集合且能巢狀到其他集合中。
集合應用
去除重複項
l = [1, 2, 1, 3, 2, 4, 5]
l = list(set(l))
遍歷圖形或其他回環結構時,用來記錄已經訪問過的位置
處理較大的資料集合(例如資料庫查詢結果)
numpy 提供了高階的數字程式設計工具,例如矩形資料型別、向量處理和高階的計算庫
Python學習手冊(二)numpy常用函式
1.python語法 列表推導式 2.numpy 一些函式 3.matplotlib 一些函式 4.matplotlib 高階函式 5.執行緒 程序 函式 6.cython的使用 7.lmdb的讀取 y c red if y else blue for y in y 將真值放if 前面,判斷條件放中...
Python學習手冊 02
物件無非是記憶體中的一部分,包含數值和相關操作的集合。python程式可以分解成模組,語句,表示式,物件。1,程式由模組構成 2,模組包含語句 3,語句包含表示式 4,表示式建立並處理物件 python提高程式設計效率的方法之一 內建型別 像函式,模組,類這樣的程式設計單元也是物件,由def,cla...
python學習手冊 簡記
匹配hello開頭 world結尾字串 中間的任意字元儲存在group中.import re match re.match hello t world hello python world match group 1 python match re.match usr home lumberjack...