标签:
python 整数,浮点型,转换int,float,长度限定
向下整除
1 # Arithmetic expressions - numbers, operators, expressions 2 3 print 3, -1, 3.14159, -2.8 4 5 # numbers - two types, an integer or a decimal number 6 # two corresponding data types int() and float() 7 8 print type(3), type(3.14159) 9 print type(3.0) 10 11 12 # we can convert between data types using int() and float() 13 # note that int() takes the "whole" part of a decimal number and doesn‘t round 14 # float() applied to integers is boring 15 16 print int(3.14159), int(-2.8) 17 print float(3), float(-1) 18 19 20 # floating point number have around 15 decimal digits of accuracy 21 # pi is 3.1415926535897932384626433832795028841971... 22 # square root of two is 1.4142135623730950488016887242096980785696... 23 24 # approximation of pi, Python displays 12 decimal digits 25 26 print 3.1415926535897932384626433832795028841971 27 28 # appoximation of square root of two, Python displays 12 decimal digits 29 30 print 1.4142135623730950488016887242096980785696 31 32 # arithmetic operators 33 # + plus addition 34 # - minus subtraction 35 # * times multiplication 36 # / divided by division 37 # ** power exponentiation 38 39 print 1 + 2, 3 - 4, 5 * 6, 2 ** 5 40 41 # Division in Python 2 42 # If one operand is a decimal (float), the answer is decimal 43 44 print 1.0 / 3, 5.0 / 2.0, -7 / 3.0 45 46 # If both operands are ints, the answer is an int (rounded down) 47 48 print 1 / 3, 5 / 2, -7 / 3 49 50 51 # expressions - number or a binary operator applied to two expressions 52 # minus is also a unary operator and can be applied to a single expression 53 54 print 1 + 2 * 3, 4.0 - 5.0 / 6.0, 7 * 8 + 9 * 10 55 56 # expressions are entered as sequence of numbers and operations 57 # how are the number and operators grouped to form expressions? 58 # operator precedence - "please excuse my dear aunt sallie" = (), **, *, /, +,- 59 60 print 1 * 2 + 3 * 4 61 print 2 + 12 62 63 64 # always manually group using parentheses when in doubt 65 66 67 print 1 * (2 + 3) * 4 68 print 1 * 5 * 4
标签:
原文地址:http://www.cnblogs.com/yatong/p/4661265.html