if Statement
>>> def f(x):
... if x<0:
... print 'Negative'
... elif x==0:
... print 'Zero'
... elif x>0:
... print 'Positive'
...
>>> f(1)
Positive
>>> f(-1)
Negative
>>> f(0)
Zero
>>>
for Statement
>>> a = ['cat','window','defenestrate']
>>> for x in a[:]:
... if len(x) > 6:
... a.insert(0,x)
...
>>> a
['defenestrate', 'cat', 'window', 'defenestrate']
>>>
range(start,stop,step) --start default 0 ,step default 1
>>> range(4)
[0, 1, 2, 3]
>>> range(2,4)
[2, 3]
>>> range(4,2)
[]
>>> range(2,8,2)
[2, 4, 6]
>>>
break,continue,pass
定义函数
def f(n):
def f(**keywords):
keys=keywords.keys()
keys.sort()
for kw in keys:
print kw,":",keywords[kw]
>>> f(a=1,c=2,b=3)
a : 1
b : 3
c : 2
>>>
数据结构
1.序列--包括列表,元组,字符,buffer,unicode,range
1.1 定义列表
>>> a=[1,2,3,4]
>>> b=1,2,3
>>> c='huqiang'
>>> d=range(1,4)
1.2 序列的操作
>>> a
[1, 2, 3, 4]
>>> 1 in a
True
>>> 1 not in a
False
>>>
>>>
>>> e=[5,6,7]
>>> a+e
[1, 2, 3, 4, 5, 6, 7]
>>> 5*a
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
>>> a*5
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
>>> a
[1, 2, 3, 4]
>>> a[0]
1
>>> a[1:3]
[2, 3]
>>> a[0:3:2]
[1, 3]
>>> len(a)
4
>>> min(a)
1
>>> max(b)
3
>>> max(a)
4
>>>
2.列表
简单脚本总结:
if os.path.isdir("/tmp"):
print "/tmp is a directory"
else:
print "/tmp is not a directory"
python 学习小结
最新推荐文章于 2024-05-10 06:33:29 发布