Comment
(#) not inside ("") begins a comment
miscellaneous
empty physical line to terminate a multiline statement
() join two adjacent physical line into a single logical line.
but, if an open parenthesis ( (,[,{ ) has not been closed,
python automatically joins them.
continuation line do not need to be indented but only the logical line begins with.
_ in interactive : last expression
identifier nomenclature
[A-Z]w* : class names ^_ : private identifier ^__ : strongly private identifier ^__w+__$ : language-defined special name _ in interactive : last expression
numeric data type
d*(l|L)$ : explicitly denote a long integer, (‘l’) is not recommended!! 长得太像1了!
sys.maxint(as 0111) : lagest positive plain integer
-sys.maxint-1(as 1000=1111(-1)-0111) : most negative plain integer
z.imag : this bizar name(maybe wanna to keep 4 characters long as ‘real’) means
the image part of a complex literal.
关于浮点,这篇文章不得不提:
What Every Computer Scientist Should Know About Floating-Point Arithmetic
at http://docs.sun.com/source/806-3568/ncg_goldberg.html
有空去翻译一遍。
Sequences
Sequences 有strings,tuples,lists。strings下面有单独介绍。
分别用tuple(an iterable ),list(an iterable )来构造。
Python 中的iterable是sequence概念的推广,all sequences are iterables.
tuple series of expressions (the items of the tuple) separated by commas (,).
外面的括号不是必须的,但要表示一个tuple传给函数做变量则需要括号,否则逗号会被
解释为函数参数列表分隔用。当然,要表示空tuple就少不了;要表示一个项的tuple,
还需要在最后右括号前加逗号。如下:
need_tuple((3,4,5)) # 一个tuple做一个参数 need_3args(3,4,5) # 3个int作3个参数 (),tuple() # 空tuple (3,) # 一个项的tuple (3) # 3 , an int
list 与tuple不同,需要外面的([和] )。
比较有特色的是List comprehension 。实际就是用for loop把
一个个满足条件的target, append到生成的list里面。
比如:
# 生成一个不为0的list result1 = [x+1 for x in some_sequence if x!=0] # 还可以多嵌套, [x+y for x in alist for y in another] # 这与下面的双循环等价 result6 = [] for x in alist: for y in another: result6.append(x+y)
Sets
set and frozenset which are found in module ‘sets’ 。
use the following codes to auto select for either Python 2.3 or Python 2.4:
try: set except NameError: from sets import Set as set, ImmutableSet as frozenset # create a set set set('rrrrr') #->set(['r'])
Items in sets is Hashable , frozenset it self is Hashable, but not set.
So no set(except frozenset) can belong to set(include frozenset).
Dictionaries
As sets, keys in a dict should be hashable , value could be any type.
dict itself is mutable, not hashable.
# only last key matters {'x':1, 'x':2} #=> {'x':2} dict() #=> empty dict dict([(1,2)]) #=> from an itarable of **pairs** dict(x=1) #=> what the fucking is this??!! a {'x':1} ?? I hate it! #and the following eccentric mixture! gives {'y': 2, 'x': 42, 'z': 7} dict({'z':1} ,x=42, y=2, z=7) #this is useful dict.fromkeys('iloveu',8) #=> {'e': 8, 'i': 8, 'l': 8, 'o': 8, 'u': 8, 'v': 8}
boolean
Any nonzero number or nonempty container(string, tuple, list, set, or dictionary)
are True .
0 (of any numeric type), None, and empty containers are false.
i = 1 i += True # i = 2
String !
一般的quoting可以使用( ‘或" ),但python比较特色的triple-quoting( """或”’ )
二者的区别就在于一般的quoting里不能有 换行 和 quote符号本身 ,相同点是:都不能有
没被反斜杠的反斜杠(原因当然是因为反斜杠比较反斜杠,或者叫比较转义,另类的意思)
开头加r或R的则上面的连反斜杠的问题都解决了,缺点当然就是你输入不了DDD或xXX,因为已经
变成平民了,不再具有任何特权。不过想连接多行(single-quoting),行末尾的还是少不了的,
但不是起原来反斜杠的作用了,而是表示“这里换行前面有一个我其实不想要,但不得不要的 "
Unicode String
如果代码里有中文,那么只有把中文放在u"" 这unicode string里,而且还要在文件开头 指定编码,
就像:
# -*- coding: cp936 -*-
unicode string除了有相同的反斜杠类:行尾的, \, ‘, ", a(Bell), b(Backspace),
f(form feed, 不知道干吗的),n, r(Carriage return, 著名的^M), t, v, DDD, xXX,
other(不变,但没有),还多了个uXXXX指明一个unicode字符,而且还有一个N{name },
name 到
http://www.unicode.org/charts/
里找。(比如N{Copyright Sign})
All about character/encoding/utf-8
use other character set ONLY in string literals and comments
add following at the start of source file
# -*- coding: utf-8 -*-