Python basic grammar / algorithm

Make a note because I forget it every time. Updated from time to time.

Basic grammar


(1, 2, 3) etc. Cannot be changed. The list can be changed.

How to receive the list

L = [[1, 2], [3, 4]]
for x, y in L:
    print(x, y)

# 1, 2
# 3, 4

List slice


#Reverse order


L = sorted(L)

Get the quotient and remainder of the division at the same time

a, b = divmod(10, 3)
print(a, b)
# 3 1

Returns the remainder of x ** y divided by z

pow(x, y, z)

n is at most 10

min(10, n)




# 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))

Greatest common divisor

# a>b
def gcd(a, b)
    while b:
        a, b = b, a % b

import math
print(math.gcd(6, 4))

Least common multiple

def lcm(a, b):
    return a * b // gcd(a, b)

import math
print(math.lcm(6, 4))


def divisor(n):
    lower_divisors, upper_divisors = [], []
    i = 1
    while i * i <= n:
        if n % i == 0:
            if i != n // i:
                upper_divisors.append(n // i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

Prime factorization

def prime_decomposition(n):
  i = 2
  table = []
  while i * i <= n:
    while n % i == 0:
      n /= i
    i += 1
  if n > 1:
  return table

Primality test

def is_prime(n):
    for i in range(2, n+1):
        if i * i > n:
        if n % i == 0:
            return False
    return n != 1

Convert to an octal string

str.format(i, 'o')

Recommended Posts

Python basic grammar / algorithm
Python3 basic grammar
Python basic grammar (miscellaneous)
Python basic grammar note (4)
Python basic grammar note (3)
Python basic grammar memo
Basic Python 3 grammar (some Python iterations)
Python algorithm
Python installation and basic grammar
Python Basic Grammar Memo (Part 1)
Python basic grammar (miscellaneous) Memo (3)
Python basic grammar (miscellaneous) Memo (2)
Basic Python grammar for beginners
I learned Python basic grammar
Python basic grammar (miscellaneous) Memo (4)
Python (Python 3.7.7) installation and basic grammar
Java and Python basic grammar comparison
Basic grammar of Python3 system (dictionary)
RF Python Basic_01
python grammar check
Python memorandum (algorithm)
Basic Python writing
Python grammar notes
RF Python Basic_02
[Basic grammar] Differences between Ruby / Python / PHP
[Python] I personally summarized the basic grammar.
Basic grammar of Python3 system (character string)
Basic grammar of Python3 series (list, tuple)
Basic grammar of Python3 system (included notation)
[Go] Basic grammar ① Definition
Python basic course (12 functions)
Python I'm also basic
A * algorithm (Python edition)
Python Basic Course (7 Dictionary)
Python basic course (2 Python installation)
Basic sorting in Python
[Go] Basic grammar ② Statement
Python ~ Grammar speed learning ~
Python basic course (9 iterations)
[python] class basic methods
Genetic algorithm in python
Python Basic Course (11 exceptions)
Python basic course (6 sets)
Python3 cheat sheet (basic)
Python Basic Course (Introduction)
Python basic memorandum part 2
python basic on windows ②
Algorithm in Python (Bellman-Ford)
[Go] Basic grammar ③ Pointer
Python basic memo --Part 2
VBA user tried using Python / R: basic grammar
Relearn Python (Algorithm I)
Python basic course (13 classes)
Basic Python command memo
Basic knowledge of Python
OpenCV basic code (python)
Python basic memo --Part 1
python memorandum super basic
Python basic course (8 branches)
Python basic if statement
Algorithm in Python (Dijkstra's algorithm)