Python 学习笔记一

来源:互联网 发布:软件接口通信 编辑:程序博客网 时间:2024/05/02 04:48


1、

'/' performs float division and '//' performs integer division

<span style="color:#39424e;">>>> </span><span style="color:#ff0000;">from __future__ import division</span><span style="color:#39424e;">>>> print 4/31.3333333333333333 >>> print 4//31</span>

2、

One of the built-in functions of Python is divmod, which takes two arguments a and b and returns a tuple containing the quotient of a/b (a//b) and remainder a.

Here a/b can be compared with integer division a//b.

>>> print divmod(177,10)(17, 7)

Here 177//10 => 17 and 177%10 => 7

3、

We have only heard of the powers of Python, so far; now we will witness them :)

Power or exponent in Python can be calculated using the built-in power function. Which can be called as for ab

>>> pow(a,b) 

or

>>> a**b

It's also possible to calculate abmodm.

>>> pow(a,b,m)  

This is very helpful in computations where you have to print result % mod.

Note that here a and b can be floats and even negatives; but ifa third argument is present, cannot be negative.

Note Python has a module math, which has its own pow() but it takes two arguments and returns a float. Frankly speaking, we willnever use math.pow()



0 0
原创粉丝点击