长整数的处理:
>>>
1000000000000000000
1000000000000000000L
2.2及以前版本处理的范围:2147483647 (or smaller than –2147483648)
16进制:
>>> 0xAF
175
八进制:
>>>
010
8
变量必须赋值才能使用。
位操作符:
x
x >> y Right shift
x & y Bitwise and
x | y Bitwise or
x ^ y Bitwise xor (exclusive or)
~x Bitwise negation
python在位运算不会自动截断,要注意到是否会产生巨长的结果。
其他运算符:
abs(x) Absolute value
divmod(x,y) Returns (x // y, x %
y)
pow(x,y [,modulo]) Returns (x **
y) % modulo
round(x,[n]) Rounds to the nearest
multiple of 10-n (floating-point numbers
only)
注意pow可以作为三元运算符,一般用于加密算法。
round以远离0为目标:round(0.5)得1,round(-0.5)得-1。Python 3中有点怪异:round(0.5)和round(0.5)得0,round(1.5)得2, round(-1.5)得-2。
算数比较符:
x
x > y Greater than
x == y Equal to
x != y Not equal to
x >= y Greater than or equal to
x
它们的连接,比如w ,理解为w
python中的隐式转换并不多。
*复合运算符
x +=y
x -=y
x *=y
x /=y
x //=y
x **=y
x %=y
x
&=y
x |=y
x ^=y
x
>>=y
x
a = 3
b = [1,2]
c = "Hello %s %s"
a += 1 # a = 4
b[1] += 10 # b = [1, 12]
c %= ("Monty",
"Python") # c = "Hello Monty Python"
*点号
*函数调用()
def
foo(x,y,z):
return x+y+z
from
functools import partial
f =
partial(foo,1,2)
f(3)
这个东东和currying进程很类似。
*函数调用()
def
foo(x,y,z):
return x+y+z
from
functools import partial
f =
partial(foo,1,2)
f(3)
这个东东和currying进程很类似。
*类型转换
int(x [,base]) Converts x to an integer.
base specifies the base if x
is a
string.
float(x)
Converts x to a floating-point number.
complex(real
[,imag]) Creates a complex number.
str(x)
Converts object x to a string representation.
repr(x)
Converts object x to an expression string.
format(x
[,format_spec]) Converts object x to a formatted string.
eval(str)
Evaluates a string and returns an object.
tuple(s)
Converts s to a tuple.
list(s)
Converts s to a list.
set(s)
Converts s to a set.
dict(d)
Creates a dictionary. d must be a sequence of
(key,value)
tuples.
frozenset(s)
Converts s to a frozen set.
chr(x)
Converts an integer to a character.
unichr(x)
Converts an integer to a Unicode character (Python 2
only).
ord(x)
Converts a single character to its integer value.
hex(x)
Converts an integer to a hexadecimal string.
bin(x)
Converts an integer to a binary string.
oct(x)
Converts an integer to an octal string.
a =
int("34") # a = 34
b =
long("0xfe76214", 16) # b = 266822164L (0xfe76214L)
b =
float("3.1415926") # b = 3.1415926
c =
eval("3, 5, 6") # c = (3,5,6)
*布尔类型操作符
x or y
If x is false, return y; otherwise, return x.
x and y
If x is false, return x; otherwise, return y.
not x If
x is false, return 1; otherwise, return 0.
*对象相等
相等:(x == y)
是同一对象:is
*运算顺序
除了乘方以外,都是由左至右的。
优先级由高到低:
(...),
[...], {...} Tuple, list, and dictionary creation
s,
s[i:j] Indexing and slicing
s.attr
Attributes
f(...)
Function calls
+x, -x,
~x Unary operators
x ** y
Power (right associative)
x * y, x
/ y, x // y, x % y Multiplication, division, floor division, modulo
x + y, x
- y Addition, subtraction
x
> y Bit-shifting
x &
y Bitwise and
x ^ y
Bitwise exclusive or
x | y
Bitwise or
x
tests
x >
y, x >= y,
x == y,
x != y
x is y,
x is not y
x in s,
x not in s
not x
Logical negation
x and y
Logical and
x or y
Logical or
lambda
args: expr Anonymous function