Powered by Markdown
Python division operator - And what does double slash work in Python?
In Python, slash operator works division.
x = 6 / 2
print(x) # 3.0
x is not 3 but 3.0 (...
Zero division in Python
1 can't be devided by 0 in Python.
a = 1 / 0
# ZeroDivisionError: division by zero
Get the quotient and remainder of a division in Python
The `divmod()`, a Python built-in function, returns the quotient and remainder of a division.
a = divmod(17, 3)
print(a) # (5, 2...
Absolute value in Python: abs can calculate absolute of complex numbers
Python built-in function `abs` returns the absolute value.
print(abs(2)) # 2
print(abs(3.1)) # 3.1
print(abs(-1)) # 1
Python max and min: How to get the max of a list, tuple, set
The `max` or `min` calculates the max or min, respectively, of a list, tuple, set, etc.
s = [1, 2, 3]
a = max(s)
b = min(s)
The average or mean of a Python list, tuple, set
The average or mean of a Python list is calculated by statistics module.
a = [1, 2, 3]
m = statistics.mean(a...
Python GCD - How to get GCD of 2 integers or a list
The simplest way to get GCD of 2 integers is importing `math` module and using `gcd` function.
g = math.gcd(6, 8)
Python complex number - "j" or "J" is the imaginary unit in Python
Python complex number is expressed with `j`, which is the imaginary unit or unit imaginary number.
a = 5 + 12j
b = complex(5, 12)
Python pow and math.pow: how to calculate exponentiation
In Python, double asterisks means power operator.
a = 3 ** 2
b = 3 ** 3
c = 3 ** 4
print(a) # 9
print(b) # 27
print(c) # 8...
Python Hexadecimal - How to convert an integer to the hexadecimal string
Python `hex` converts an integer to the hex string. For example, 19 is 16 + 3 so 19 is converted to `0x13`. `0x` is a prefix meaning the value i...
Get the number of permutations in Python: Calculate nPk (the number of ways to choose and order items)
The `perm()` in `math` module returns the number of patterns to choose k objects from n objects and order them, that is nPk
Python Date & Time
Python Built in