python整理一——解释器上的几个简单操作

来源:互联网 发布:校园机房网络拓扑图 编辑:程序博客网 时间:2024/06/08 19:32

1. 在python解释器中, _表示最后一个表达式的值,如:

 

  1. >>> s1 = 'Hello'
  2. >>> s1
  3. 'Hello'
  4. >>> s1 += ', Python!'
  5. >>> s1
  6. 'Hello, Python!'
  7. >>> _
  8. 'Hello, Python!'

 

第5行和第7行的打印值一样。

 

2.>>表示重定向,相当于cmd中的 >,如:

  1. >>> f = open('log.txt''a')
  2. >>> print >> f, 'this is a log file.'
  3. >>> f.close()
  4. >>> [line.strip() for line in open('log.txt''r')]
  5. ['this is a log file.']
  6. >>> 

第2行中 >> 操作符就进行了重定向操作。

 

 

原创粉丝点击