Python基础教程(二)
2013-08-06 19:19
411 查看
Python 中字符串被定义为引号之间的字符集合。Python 支持使用成对的单引号或双引号,
三引号(三个连续的单引号或者双引号)可以用来包含特殊字符。使用索引运算符( [ ] )和切
片运算符( [ : ] )可以得到子字符串。字符串有其特有的索引规则:第一个字符的索引是 0,
最后一个字符的索引是 -1
加号( + )用于字符串连接运算,星号( * )则用于字符串重复。下面是几个例子:
列表:
元组:
if语句:
while循环:
for循环和range()内建函数:
数字循环:
字符串迭代:
列表解析
文件和内建函数open() 、file()
如何打开文件
handle = open(file_name, access_mode = 'r')
file_name 变量包含我们希望打开的文件的字符串名字, access_mode 中 'r' 表示读取,
'w' 表示写入, 'a' 表示添加。其它可能用到的标声还有 '+' 表示读写, 'b'表示二进制访
问. 如果未提供 access_mode , 默认值为 'r'。如果 open() 成功, 一个文件对象句柄会被
返回。所有后续的文件操作都必须通过此文件句柄进行。当一个文件对象返回之后, 我们就可
以访问它的一些方法, 比如 readlines() 和close()
下面有一些代码, 提示用户输入文件名, 然后打开一个文件, 并显示它的内容到屏幕上:
filename = raw_input('Enter file name: ')
fobj = open(filename, 'r')
for eachLine in fobj:
print eachLine,
fobj.close()
错误和异常:
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
定义函数:
def addMe2Me(x):
'apply + operation to argument'
return (x + x)
这个函数, 干的是“在我的值上加我”的活。它接受一个对象, 将它的值加到自身, 然
后返回和
三引号(三个连续的单引号或者双引号)可以用来包含特殊字符。使用索引运算符( [ ] )和切
片运算符( [ : ] )可以得到子字符串。字符串有其特有的索引规则:第一个字符的索引是 0,
最后一个字符的索引是 -1
加号( + )用于字符串连接运算,星号( * )则用于字符串重复。下面是几个例子:
>>> pystr = 'Python' >>> iscool = 'is cool!' >>> pystr[0] 'P' Edit By Vheavens Edit By Vheavens >>> pystr[2:5] 'tho' >>> iscool[:2] 'is' >>> iscool[3:] 'cool!' >>> iscool[-1] '!' >>> pystr + iscool 'Pythonis cool!' >>> pystr + ' ' + iscool 'Python is cool!' >>> pystr * 2 'PythonPython' >>> '-' * 20 '--------------------' >>> pystr = '''python ... is cool''' >>> pystr 'python\nis cool' >>> print pystr python is cool
列表:
>>> aList = [1, 2, 3, 4] >>> aList [1, 2, 3, 4] >>> aList[0] 1 >>> aList[2:] [3, 4] >>> aList[:3] [1, 2, 3] >>> aList[1] = 5 >>> aList [1, 5, 3, 4]
元组:
>>> aTuple = ('robots', 77, 93, 'try') >>> aTuple ('robots', 77, 93, 'try') >>> aTuple[:3] ('robots', 77, 93) >>> aTuple[1] = 5 Traceback (innermost last): File "<stdin>", line 1, in ? TypeError: object doesn't support item assignment
if语句:
if expression1: if_suite elif expression2: elif_suite else: else_suite
while循环:
>>> counter = 0 >>> while counter < 3: ... print 'loop #%d' % (counter) ... counter += 1
for循环和range()内建函数:
>>> print 'I like to use the Internet for:' I like to use the Internet for: >>> for item in ['e-mail', 'net-surfing', 'homework', 'chat']: ... print item ...
数字循环:
>>> for eachNum in range(3): ... print eachNum ...
字符串迭代:
>>> foo = 'abc' >>> for c in foo: ... print c ...
>>> foo = 'abc' >>> for i in range(len(foo)): ... print foo[i], '(%d)' % i ...
>>> for i, ch in enumerate(foo): ... print ch, '(%d)' % i ...
列表解析
>>> sqdEvens = [x ** 2 for x in range(8) if not x % 2] >>> >>> for i in sqdEvens: ... print i
文件和内建函数open() 、file()
如何打开文件
handle = open(file_name, access_mode = 'r')
file_name 变量包含我们希望打开的文件的字符串名字, access_mode 中 'r' 表示读取,
'w' 表示写入, 'a' 表示添加。其它可能用到的标声还有 '+' 表示读写, 'b'表示二进制访
问. 如果未提供 access_mode , 默认值为 'r'。如果 open() 成功, 一个文件对象句柄会被
返回。所有后续的文件操作都必须通过此文件句柄进行。当一个文件对象返回之后, 我们就可
以访问它的一些方法, 比如 readlines() 和close()
下面有一些代码, 提示用户输入文件名, 然后打开一个文件, 并显示它的内容到屏幕上:
filename = raw_input('Enter file name: ')
fobj = open(filename, 'r')
for eachLine in fobj:
print eachLine,
fobj.close()
错误和异常:
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
定义函数:
def addMe2Me(x):
'apply + operation to argument'
return (x + x)
这个函数, 干的是“在我的值上加我”的活。它接受一个对象, 将它的值加到自身, 然
后返回和
相关文章推荐
- Python基础教程 - Tdcqma
- python基础:数据类型和变量-python简易教程(四)
- 【Python基础教程】第2章 列表和元组-2.2通用序列操作
- Python从基础到高级视频教程
- 【书山有路】Python基础教程 第9章
- Python基础教程学习笔记----第一章
- Python基础教程系列目录,最全的Python入门系列教程!
- Python基础教程学习笔记----第五章 条件、循环和其他语句
- <<Python基础教程>>学习笔记 | 第04章 | 字典
- 【教程】记录:python基础爬虫代码(下载妹子福利图片)
- Python程序设计与算法基础教程·第三章上机实践
- python基础教程之实现石头剪刀布游戏示例
- python基础教程共60课-第28课字符串的分割
- Python学习入门基础教程(learning Python)--2.1 Python下自定义函数 .
- 【Python基础教程】第7章 类
- python基础教程_学习笔记5:字符串
- python基础教程学习笔记---(6)文件读写
- Python2.7.x基础教程笔记--input与raw_input的区别
- Python基础教程1--搭建Python开发环境
- Python基础教程(一)