早会2-保留小数&print换行

关于python保留2位小数,我觉得可以参考下这篇文章:

http://www.cnblogs.com/psztswcbyy/p/9258579.html

虽然我也没有看完。。。

但总的来说,我们最先想到的就会是用round(x,n)的形式,毕竟数据库也有这种形式所以会是第一个想到的;

但是会出现以下场景:

c=[1.24233,341.3,333]; for i in range(len(c)): print(round(c[i],2));

所以说round并不一定满足我们的需求,尤其是当我们需要强制性的保留2为小数的时候。

但是用格式化字符串的形式就可以实现(即我们的第二种方法)。

最后还有一种:

不过相对而言也是不利于输出的。

------------------------------------------------------------------------------------------------------******************************----------------------

另:使用的时候一直弄错,在想怎样才能实现换行:斜杠\就可以满足

print('nishuos'\
      'meid')####采用\直接换行。。。心累啊

>>nishuosmeid

#

Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

默认是\n;如果使用end=' ' 也可以实现不换行的形式

for i in range(len(s)):
    print(s[i],end=' ')

>>11 22 33 44

至于这2种则有待尝试!

猜你喜欢

转载自blog.csdn.net/qq_26349791/article/details/81205731
今日推荐