当前位置:文档之家› python学习笔记

python学习笔记


尔“与” and y返回
and y,由于x是False,返回
False,否则它返 False。在这里,Python不会计
回y的计算值。 算y,因为它知道这个表达式的
值肯定是False(因为x
是False)。这个现象称为短路
计算。
or 布 如果x是True,它 x = True; y = False; x or
&
按位与
<<,>>
移位
+,-
加法与减法
*,/,%
乘法、除法与
取余
+x,-x
正负号
~x
按位翻转
**
指数
x.attribute
属性参考
x[index]
下标
x[index:index]
寻址段
f(arguments...) 函数调用
(experession,...) 绑定或元组显

[expression,...] 列表显示
print
Really, that's all it
p.__del__.__doc__ does.'''
所有成员都是公有的
print 'hello, how are
you?', a, b
p = Person('gfl')
p.SayHi(1, 2)
特殊方法
__init__(self,...) 这个方法
{key:datum,...} 字典显示
'expression,...' 字符串转换
python配置文件,保存到/root下.pythonstartup,并
在.bash_profile中PYTHONSTARTUP=/root/.pythonstartup并 export PYTHONSTARTUP # python startup file import readline import rlcompleter import atexit import os
|
L.count(value) -> integer -- return number of
occurrences of value
|
| extend(...)
|
L.extend(iterable) -- extend list by appending
elements from the iterable
index
|
| pop(...)
|
L.pop([index]) -> item -- remove and return item
at index (default last).
|
Raises IndexError if list is empty or index is
out of range.
பைடு நூலகம்
|
python学习笔记
2012年12月17日
11:27
1. 语法结构
if condition:
statement
elif condition:
statement
else:
statement
while condition:
else是可选的,会在while结束
statement
执行一次
else:
statement
iterable's items
|-------------------------------------------------------
---------------
| append(...)
|
L.append(object) -- append object to end
|
| count(...)
for condition:
else是可选的,会在for结束执
statement
行一次
else
statement
def HelloFun(a, b):
定义函数
print 'hello', a, b
def HelloFun(a, b = 10): 默认参数
print 'hello', a, b
def HelloFun(a, b = 10, c 关键参数:
| remove(...)
|
L.remove(value) -- remove first occurrence of
value.
|
Raises ValueError if the value is not present.
|
| reverse(...)
|
L.reverse() -- reverse *IN PLACE*
| index(...)
|
L.index(value, [start, [stop]]) -> integer --
return first index of value.
|
Raises ValueError if the value is not present.
|
| insert(...)
|
L.insert(index, object) -- insert object before
same object.
|-------------------------------------------------------
---------------
| count(...)
|
T.count(value) -> integer -- return number of
occurrences of value
尔“或” 返回True,否则它y返回True。短路计算在这里也
返回y的计算值。 适用。
6. 运算符优先级
运算符
描述
lambda
Lambda表达式
or
布尔“或”
and
布尔“与”
not x
布尔“非”
in,not in
成员测试
is,is not
同一性测试
<,<=,>,>=,!=,==比较
|
按位或
^
按位异或
|
| sort(...)
|
L.sort(cmp=None, key=None, reverse=False) --
stable sort *IN PLACE*;
|
cmp(x, y) -> -1, 0, 1
|
|=================================================================
|
| index(...)
|
T.index(value, [start, [stop]]) -> integer --
return first index of value.
|
Raises ValueError if the value is not present.
|=================================================================
法。
__getitem__(self,key)使用
x[key]索
引操作符
的时候调
__len__(self)
用。 对序列对 象使用内 建的len() 函数的时 候调用。
python常用标准库: sys, os
列表综合 listone = [2, 3, 4]; listtwo = [2*i
for i in listone if i > 2]; print listtwo
次的字符串
** 幂 返回x的y次幂 3 ** 4得到81(即3 * 3 * 3
* 3)
/ 除 x除以y
4/3得到1(整数的除法得到整数
结果)。4.0/3或4/3.0得到
1.3333333333333333
// 取整除 返回商的整数部分 4 // 3.0得到1.0
% 取模 返回除法的余数 8%3得到2。-25.5%2.25得到
# tab compeletion readline.parse_and_bind('tab: complete')
# history file histfile = os.path.join(os.environ['HOME'], '.pythonhistory') try:
readline.read_history_file(histfile) except IOError:
via:
class dict(object)
| dict() -> new empty dictionary
| dict(mapping) -> new dictionary initialized from a
mapping object's (key, value) pairs
| dict(iterable) -> new dictionary initialized as if
表示假。这分别与 True。
特殊的变量True和
False等价。注
意,这些变量名的
大写。
> 大于 返回x是否大于y 5 > 3返回True。如果两个操作
数都是数字,它们首先被转换为
一个共同的类型。否则,它总是
返回False。
<= 小于等 返回x是否小于等 x = 3; y = 6; x <= y返回
相关主题