Python学习记录

Python IDLE使用:

部分快捷键

Tab 自动提示

Alt + 3 注释

Alt + 3 取消注释

F5 运行

运算符

** 乘方运算 (3 ** 2表示3的2次方)

// 浮点除法 (4 // 5结果是0.8, 而不是0)

# 注释符号

and or not (与 或 非)

[] 数组索引

[0] 返回数组第一个

[:2] 返回从索引0到2的数组

[2:5] 返回从索引2到5的数组

[3:] 返回从索引3到结尾的数组

!!!不支持 ++ 与 -- 运算符 

常用函数

input = raw_input("...") #接收输入

os.listdir(rootdir) #列举目录rootdir下的所有文件和目录(不遍历子目录)

os.walk(rootdir) #遍历目录rootdir下的所有文件和目录(遍历子目录)

os.rename(srcfile, tarfile) #文件(或文件夹)重命名

print "%d %s" %(int, string) #打印(类似于C中的printf)

print "hello", "world" #输出hello world, 注意两个单词间的空格

其他

str = "hello"

str *= 2 (字符串重复: str的值为"hellohello")


dict = {'key' : 'value'} #字典

dict['key2'] = 80 #添加key value


条件

if <表达式> :

<执行内容>


循环

while <表达式> :

<执行内容>


>>> for eachNum in range(3):
... print eachNum
...
0
1
2


>>> foo = 'abc'
>>> for c in foo:
... print c
...
a
b
c


>>> foo = 'abc'
>>> for i in range(len(foo)):
... print foo[i], '(%d)' % i
...
a (0)
b (1)
c (2)


>>> for i, ch in enumerate(foo):
... print ch, '(%d)' % i
...
a (0)
b (1)
c (2)


列表解析

>>> sqdEvens = [x ** 2 for x in range(8) if not x % 2] #自右向左
>>>
>>> for i in sqdEvens:
... print i
0
4
16
36

#异常

try:
filename = raw_input('Enter file name: ')
fobj = open(filename, 'r')
for eachLine in fobj:
print eachLine, fobj.close()
except IOError, e:
print 'file open error:', e

#步进切片

a = "123456"

a[::-1] #显示"654321"


a is b #判断两个对象是否相同


#如果模块直接执行, 则运行func()

if __name__ == "__main__": 

func()


(未完待续)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值