Python入门的36个例子 之 24
# 027
toolName = 'Google'
if toolName.startswith('Go'):
print 'The tool\'s name starts with \"Go\".'
if 'oo' in toolName:
print 'The tool\'s name contains the stirng "oo".'
print toolName.find('gl') # 返回首次出现“gl”字符串的索引
if toolName.find('Baidu'):
print 'Can\'t find "Baidu" in tool\'s name.'
aList = ['a','b','c']
print '...'.join(aList)
output:
The tool's name starts with "Go".
The tool's name contains the stirng "oo".
3
Can't find "Baidu" in tool's name.
a...b...c
相关文档:
Python中的easy_install工具很好用,它的作用类似于Php中的pear,或者Ruby中的gem,或者Perl中的cpan。
如果想使用easy_install工具,可以直接安装ez_setup.py
脚本,再python ez_setup.py(之前先要安装python):
安装完后,最好确保easy_install所在目录已经被加到PATH环境变量里:
Windows: C:\Python25\Scripts
Li ......
从MoteLab返回的串口数据,包含messages.pickle文件这是MoteLab系统中串口收集数据的总和,但是这些数据需要解析后才能进行分析。下面的代码就是在python环境下提取message有效数据的代码。
使用命令 python TestOutput.py messages.pickle
生成的test.log就是获得的有效数据
返回数据的示例
1252985727.66 recei ......
二元运算符及其对应的特殊方法
二元运算符
特殊方法
+
__add__,__radd__
-
__sub__,__rsub__
*
__mul__,__rmul__
/
__div__,__rdiv__,__truediv__,__rtruediv__
//
__floordiv__,__rfloordiv__
%
__mod__,__rmod__
**
__pow__,__rpow__
<<
__lshift__,__rlshift__
>>
_ ......
StringIO的行为与file对象非常像,但它不是磁盘上文件,而是一个内存里的“文件”,我们可以将操作磁盘文件那样来操作StringIO。一个简单的例子,让你对StringIO有一个感性的认识: 1 #coding=gbk 2 3 import StringIO, cStringIO, sys 4 5 s ......
# 017
def lifeIsAMirror():
string = raw_input()
if string == 'I love you!':
return 'I love you, too!'
elif string == 'Fuck you!':
return ''
else:
return
# end of def
string = lifeIsAMirror()
if len(string) == 0:
print 'You have nothing.'
else: ......