python 学习日记

LPTHW NOTES 1

2016-10-31  本文已影响0人  小黑天天快乐

format string 

conversion characters:

r     raw string

s     string

%   a '%' character

f     floating point number

e    floating point number exponential format ( lowercase )

i     signed integer

print 'i like %s like %s%s' % ('nothing','this'+'ever','hi')

## string+string is considered as one string

round(number[,ndigits])  (四舍五入)

Return the floating point value number rounded to ndigits digits after the decimal point. If ndigits is omitted, it defaults to zero. The result is a floating point number. Values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done away from 0 (so, for example, round(0.5) is 1.0 and round(-0.5)is-1.0).

Note

The behavior of round() for floats can be surprising: for example, round(2.675,2)gives2.67instead of the expected2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float.  SeeFloating Point Arithmetic: Issues and Limitations for more information.

slicing

>>> 'abcd'[0:2]

'ab'                ## left : close  right : open

>>> 'hello' == 'HELLO'

False            ## uppercase and lowercase are different

>>> 'world'[:-1]

'worl'             ## if not pointed out, from left to right

上一篇 下一篇

猜你喜欢

热点阅读