人生苦短,我学python – 入门篇
name = raw_input('Enter name : ')
help(raw_input)
int(name)
str(name)
dir(int)
type(name)
len(name)
logfile = open('/tmp/,mylog.txt','a')
print >> logfile,'error : invalid input !'
print "%s is number %d !" % ("python",1)
3××5
print '=' * 5
aList = [1,2,3,4]
print aList[0]
print aList[2:]
print aList[:3]
- 元组 ( , , , ,) 比较吊的是可以存不同类型,缺点是不可以添加新的元素
aTuple = ('robot',77,93,'try')
aDict = {'host':'earth'}
aDict['port'] = 80
for key in aDict:
print key,aDict[key]
if expression1:
if_suite
elif expression2:
elif_suite
else:
else_suite
- range() 的使用 因为默认是按容器的迭代器遍历,按下标的话要加range
foo = 'abc'
for i in range(len(foo)):
print i,foo[i]
squared = [x**2 for x in range(4)]
for i in squared:
print i,
sqdEvents = [x**2 for x in range(8) if not x % 2]
for i in sqdEvents:
print i,
- 文件读写 r:读取 w:写入 + :读写 b:二进制访问
filename = raw_input('Enter file filename :')
fobj = open(filename,'r')
for line in fobj:
print line,
fobj.close()
try:
...
except IOError,e:
print 'file open error : ',e
def func_name(para):
'我是函数注释'
return para
import sys
print sys.platform
print sys.version
ls = os.linesep