随候鸟南飞对《Python核心编程(第二版)》的笔记(4)
随候鸟南飞 (Home sweet home)
-
第10页
#!/usr/bin/env python
-
第21页
下划线(_)在解释器中有特别的含义,表示最后一个表达式的值。
>>> s = 'hello world' >>> print s hello world >>> _ Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name '_' is not defined >>> s 'hello world' >>> _ 'hello world'
输出重定向到日志文件的例子:
>>> logfile = open('/tmp/mylog.txt', 'a') >>> print >> logfile, 'Fatal error: invalid input!' >>> logfile.close()
-
第29页
print语句最后添加一个逗号(,)可以让多个print输出在同一行,输出元素之间会自动添加一个空格
>>> print 'hello',;print 'world' hello world
-
第31页
之前读文件数据的时候一直是这样写的:
ids = [i.replace('\n', '') for i in open(filename).readlines()]
刚才意识到,原来readlines是多余
ids = [i.replace('\n', '') for i in open(filename)]
这样就好了
随候鸟南飞的其他笔记 · · · · · · ( 全部36条 )
- 猎魔人7:湖中女士
- 1
- The Go Programming Language
- 1
- SRE
- 1
- 程序员修炼之道
- 12
- 巨龙的颂歌
- 1
- 编程珠玑
- 2
- Servant of the Shard
- 1
- Realms of Valor
- 1
- 失控
- 1
- 触动人心
- 2
- 龙枪编年史3
- 1
- 龙枪编年史2
- 3
- Pro Git
- 5