python 学习小结

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"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值