Expressions, and Prints-Learning Python-Chapter 11

Expression


1. In Python, you can use an expression as a statement, too—that is, on a line by itself.

1) for calls to functions and methods.

some functions and methods without a return value.

2) for printing values at the interactive prompt


2. Expression statements and in-place changes

expression statements are often used to run list methods that change a list in-place:

>>> L = [1, 2]
>>> L.append(3) # Append is an in-place change
>>> L
[1, 2, 3]

however, 

>>> L = L.append(4) # But append returns None, not L
>>> print(L)         # So we lose our list!
None


Print Operations

1. Call format

print([object, ...][, sep=' '][, end='\n'][, file=sys.stdout][, flush=False])

The sep, end, file, and (in 3.3 and later) flush parts, if present, must be given as keyword arguments—that is, you must use a special “name=value” syntax to pass the arguments by name instead of position

Keep in mind that the separator and end-of-line options provided by print operations are just conveniences, if you need to to display more complicated /specific formatting , don;t put this way, using string  formatting tool 


Print Stream Redirection

print (x,y)

 is equivalent to 

import sys

sys.stdout.write(str(x) +' ' +str(y) +'\n')


Automatic stream redirection

log = open('log.txt', 'a') # 3.X
print(x, y, z, file=log) # Print to a file-like object
print(a, b, c) # Print to original stdout


Importing from __future__

from __future__ import print_function


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值