Make a note because I forget it every time. Updated from time to time.
(1, 2, 3) etc. Cannot be changed. The list can be changed.
L = [[1, 2], [3, 4]]
for x, y in L:
print(x, y)
# 1, 2
# 3, 4
[start:end:step]
#Reverse order
[::-1]
L.sort()
L = sorted(L)
a, b = divmod(10, 3)
print(a, b)
# 3 1
pow(x, y, z)
min(10, n)
math.factorial(5)
# p = n! / (n-r)!Than
def permutations_count(n, r):
return math.factorial(n) // math.factorial(n - r)
# c = n! / (r! * (n - r)!)Than
def combinations_count(n, r):
return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
# a>b
def gcd(a, b)
while b:
a, b = b, a % b
#Or
import math
print(math.gcd(6, 4))
def lcm(a, b):
return a * b // gcd(a, b)
#Or
import math
print(math.lcm(6, 4))
def divisor(n):
lower_divisors, upper_divisors = [], []
i = 1
while i * i <= n:
if n % i == 0:
lower_divisors.append(i)
if i != n // i:
upper_divisors.append(n // i)
i += 1
return lower_divisors + upper_divisors[::-1]
def prime_decomposition(n):
i = 2
table = []
while i * i <= n:
while n % i == 0:
n /= i
table.append(i)
i += 1
if n > 1:
table.append(n)
return table
def is_prime(n):
for i in range(2, n+1):
if i * i > n:
break
if n % i == 0:
return False
return n != 1
str.format(i, 'o')
Recommended Posts