Page 208 - Computer Science V2.0 Class 11
P. 208
For instance, the following function calls computes sum of all elements in the given sequence:
>>> values = (1, 3, 5, 7)
>>> sum(values)
16
>>> sum(values,5)
21
>>> sum((1, 3, 5, 7))
16
Note that the on invoking the function sum(values,5), it not only computes the sum of 1, 3, 5, and 7,
but also adds 5 to the sum of 16 to yield 21.
divmod()
The divmod() function takes two numeric values—integer or floating point numbers, say num1 and num2 as input
from the user and returns a pair of values comprising quotient and remainder when num1 is divided by num2. The
syntax of the function is :
divmod(num1, num2)
For instance, the following functions calls compute quotient and remainder for different pairs of dividends and divisors:
>>> divmod(17, 4)
(4, 1)
>>> divmod(21.5, 5)
(4.0, 1.5)
>>> divmod(-21, 5)
(-5, 4)
Note that when one of the dividend and divisor is positive and the other is negative, the quotient is negative and
the value of the remainder is adjusted so that the sign of the remainder matches the sign of the divisor, for example,
-21 = 5×(-5) + 4. Similarly,
>>> divmod(21, -5)
(-5, -4)
As before, in the above example, we note that 21 = -5×(-5) + (-4).
>>> divmod(-21, -5)
(4, -1)
Note that when each of the dividend and divisor is negative, the quotient is positive. Again, the value of the remainder
is adjusted so that the sign of the remainder matches the sign of the divisor, for example. -21 = -5 × 4 + (-1).
abs()
The abs() function returns absolute value of integer, floating-point, or complex number provided as the argument.
For instance, the following function calls return the absolute values of -12, -14.7, 21, and 4 + 3j.
>>> abs(-12)
12
>>> abs(-14.7)
14.7
>>> abs(21)
21
>>> abs(4 + 3j)
5.0
pow()
The pow() function takes two (or more numeric values), say num1, num2, and num3 (optional) as an input from the
user and returns num1 raised to the power of num2. Optional input, num3, when provided, is used to compute
the remainder obtained on dividing num1 num2 by num3, i.e. to compute num1 num2 %num3. For instance, the
3
4
5
following function calls compute the values of 14 , 3 %5, and -2.3 .
194 Touchpad Computer Science (Ver. 2.0)-XI

