2014年7月9日 09:23:59
in Terminal:
file operations
python 优点:处理文件快
based on python R2.7.5
www.python.org
readability counts
更多处理字符串类型
安装
uhuntu: apt-get install python
centos: yum install python
my first python demo: 1.py
uhuntu: apt-get install python
centos: yum install python
my first python demo: 1.py
#!/user/bin/python
import os,sys
def calcs(first,sec,third):
result=first+sec+third
print result
def main():
file="./1.txt"
f=open(file,'r')
str=f.readlines()
str2=str[2].split(':')[0]
str3=str[2].split(':')[1]
print 'str2 :'+str2+'\n'+'str3:'+str3
if __name__=='__main__':
main()
import sys
import os
类似于c中include头文件
Data types:
1代码格式对齐,保持统一缩进格式。
2一行多个语句,分号分割
3反斜杠\,扩展到下一行
4关键字
function type() to check the type of the data
in Terminal:
>>> str = 'hello,world'
>>> str
'hello,world'
>>> str[2:]
'llo,world'
[]注意左闭右开,[a,b] a=<result<blist1=['physics','chemistry',1997,2000];
cpm(list1,list2)
len(list)
max(list)
min(list)
list(seq) convert tuple to list
exit()退出
list.count(字符)不行?
可以,list[1].count('o')
dictionary 类似于 hashmap
dict_a={'1':['A','X'],2:'STR1'}
demo:
demo:
>>> dict_a={'1':['A','X'],2:'STR1'}
>>> dict_a.keys()
['1', 2]
>>> dict_a[2]
'STR1'
str(dict_a) : convert the dictionary into str,类似于数据类型强制转换。
how to run py file:
how to run py file:
python /root/a.py
2014年7月9日 10:40:12
if expression:
fun1();
else:
fun2();
for loop
for target in expression_list:
suite
else:
suite
format();
1.func中的参数,11对应
2.位置互换
3.其中某个参数带默认值
file operations
open
f=open(file,'r')
str_obj=f.read()
f.close()
练习!
str_obj=f.readline()
str_obj=f.readlines()
with open() as fd;
str='hellw_world'
str.split('_')[1]
python -m pdb: debug
c: next
b: breakpoint
l: show context
s: step into the function
n: next statment
second method of pdb:
import pdb
pdb.set_trace() 自动加断点,when the py project is too large