Tutorial by Examples

a, b = 1, 2 # Using the "+" operator: a + b # = 3 # Using the "in-place" "+=" operator to add and assign: a += b # a = 3 (equivalent to a = a + b) import operator # contains 2 argument arithmetic functions for the examp...
a, b = 1, 2 # Using the "-" operator: b - a # = 1 import operator # contains 2 argument arithmetic functions operator.sub(b, a) # = 1 Possible combinations (builtin types): int and int (gives an int) int and float (gives a float) int and comp...
a, b = 2, 3 a * b # = 6 import operator operator.mul(a, b) # = 6 Possible combinations (builtin types): int and int (gives an int) int and float (gives a float) int and complex (gives a complex) float and float (gives a float) float and complex (gives a comple...
Python does integer division when both operands are integers. The behavior of Python's division operators have changed from Python 2.x and 3.x (see also Integer Division ). a, b, c, d, e = 3, 2, 2.0, -3, 10 Python 2.x2.7 In Python 2 the result of the ' / ' operator depends on the type of the nu...
a, b = 2, 3 (a ** b) # = 8 pow(a, b) # = 8 import math math.pow(a, b) # = 8.0 (always float; does not allow complex results) import operator operator.pow(a, b) # = 8 Another difference between the built-in pow and math.pow is that the built-in po...
By default, the math.log function calculates the logarithm of a number, base e. You can optionally specify a base as the second argument. import math import cmath math.log(5) # = 1.6094379124341003 # optional base argument. Default is math.e math.log(5, math.e) # = 1.6094379124341003 ...
It is common within applications to need to have code like this : a = a + 1 or a = a * 2 There is an effective shortcut for these in place operations : a += 1 # and a *= 2 Any mathematic operator can be used before the '=' character to make an inplace operation : -= decrement the va...
a, b = 1, 2 import math math.sin(a) # returns the sine of 'a' in radians # Out: 0.8414709848078965 math.cosh(b) # returns the inverse hyperbolic cosine of 'b' in radians # Out: 3.7621956910836314 math.atan(math.pi) # returns the arc tangent of 'pi' in radians # Out: 1.2626272556789...
Like in many other languages, Python uses the % operator for calculating modulus. 3 % 4 # 3 10 % 2 # 0 6 % 4 # 2 Or by using the operator module: import operator operator.mod(3 , 4) # 3 operator.mod(10 , 2) # 0 operator.mod(6 , 4) # 2 You can also use negative nu...

Page 1 of 1