使用Python做数值运算,python数值运算,#!/usr/bin/p


#!/usr/bin/python# Some calculations.  Note the lack of semicolons.  Statements end at the end# of the line.  Also, variables need not start with a special symbol as in# perl and some other Unix-bred languages.fred = 18barney = FRED = 44;                     # python 是大小写敏感的bill = (fred + barney * FRED - 10)alice = 10 + bill / 100                 # Integer division truncatesfrank = 10 + float(bill) / 100print "fred = ", fredprint "bill = ", billprint "alice = ", aliceprint "frank = ", frank print# Each variable on the left is assigned the corresponding value on the right.fred, alice, frank = 2*alice, fred - 1, bill + frankprint "fred = ", fredprint "alice = ", aliceprint "frank = ", frank print# Exchange w/o a temp.fred, alice = alice, fredprint "fred = ", fredprint "alice = ", aliceprint# Python allows lines to be continued by putting a backslash at the end of# the first part.  fred = bill + alice + frank - \       barneyprint "fred =", fredprint# The compiler will also combine lines when the line break in contained# in a grouping pair, such as parens.joe = 3 * (fred +        bill - alice)print "joe =", fred

注意:

python做除法,默认是整除,会舍弃余数部分。当做除法的除数或者被除数有一个是float类型时会做小数除法python的print后面是元组时会自动在各个元素之间加空格,每个print语句都会换行一个语句可以同时给多个变量赋值,变量赋值的顺序是从右向左依次赋值fred, alice = alice, fred 语句可以用来交换fred和alice变量的值,但不需要临时变量

评论关闭