Techniques often used in python short coding (Notepad)

I will update the techniques I learned in show coding recently started on the site codefights from time to time.

Short coding consideration

Basic

<And <=

The comparison operator <= can basically be omitted and can be reduced by ** 1 character **

# long
a <= 3

# short (When a is an integer)
a < 4

~-and-~

Corresponds to -1 and +1 respectively However, since it has a higher priority than the multiplication operator, it can be reduced by ** 2 characters ** by omitting the parentheses.

# long
b * (a - 1) * 5

# short
b * ~-a * 5

Ternary operator

: (Colon) and the left side of the assignment can be omitted

# long
if a < b:
    c = 4
else:
    c = 2

# short
c = 4 if a < b else 2

repr literal

(Python2 only) Use \ \ to convert to string

# long
str(1600)

# short
`1600`

True, False constant

If you want to return a logical value exactly, use a logical operation without using True or False.

# long
b = False

# short
b = 0 > 1



# long
return True

# short
return 1 > 0

import as Also shorten the module to import

# long
import math
math.sqrt(10)

# short
import math as m
m.sqrt(10)

Bulk equivalence check

Connect multiple string comparisons and perform at once

#Both the first and last characters match
# long
a[0] == b[0] and a[-1] == b[-1]

# short
a[0] + a[-1] == b[0] + b[-1]


#a and b,c and d have the same length(Or unique even when combined)If
# long
a == b and c == d

# short
a + c == b + d

Functions etc.

If it is a challenge of codefights, the answer in the form of a function will be tested.

lambda If possible, you can reduce ** 4 characters ** by converting the function to a lambda expression.

# long
def Hoge(n):
    return n * 2

# short
Hoge = lambda n: n * 2

Argument name

Argument names can be one character because there are many cases where they are not explicitly called.

# long
def Hoge(arg):
    return arg ** arg

# short
def Hoge(a):
    return a ** a

Recursive function

Assign to a one-character variable when using recursion

# long
def LongLongHoge(a):
    if a < 3:
        return a
    return LongLongHoge(a - 2) * LongLongHoge(a - 1)

# short
def LongLongHoge(a):
    if a < 3:
        return a
    return t(a - 2) * t(a - 1)
t = LongLongHoge

Even shorter with lambda and simultaneous assignment

# short
LongLongHoge = t = lambda a: a if a < 3 else t(a - 2) * t(a - 1)

application

Conditioning in a list

# long
t = a % 3
if t == 0:
    a = a * 2
elif t == 1:
    a = 0
elif t == 2:
    a = t * 2

# short
a = [a * 2, 0, (a % 3) * 2][a % 3]

# short
t = a % 3
a = [a * 2, 0, t * 2][t]

Use conditional expressions well

(a <b) returns True and False, but can be used for operations as 1, 0, respectively. There are many patterns that can be omitted by using it

# long
c = 4 if a < b else 2

# short
c = 2 + 2 * (a < b)

Create a function that contains other methods as they are

# long
myhex = lambda n: '%X' % n

# short
myhex = '{:X}'.format
myhex = '%X'.__mod__

Recommended Posts

Techniques often used in python short coding (Notepad)
[python] Frequently used techniques in machine learning
Code often used in Python / Django apps [prefectures]
A collection of code often used in personal Python
A collection of Excel operations often used in Python
Settings often used in Jupyter
Techniques for sorting in Python
Commands often used in the development environment during Python implementation
Tips for coding short and easy to read in Python
Can be used with AtCoder! A collection of techniques for drawing short code in Python!
8 Frequently Used Commands in Python Django
[Python] Basic knowledge used in AtCoder
Grammar summary often used in pandas
Summary of methods often used in pandas
Disk-related commands often used in Ubuntu (memories)
Write a short property definition in Python
Processing memos often used in pandas (beginners)
Python scikit-learn A collection of predictive model tips often used in the field
Python scikit-learn A collection of predictive model tips often used in the field
++ and-cannot be used for increment / decrement in python
Find prime numbers in Python as short as possible
Settings for Python coding in Visual Studio Code
Standard .py file used in Python trials (template)-2020
Astro: Python modules / functions often used for analysis
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
First Python ~ Coding 2 ~
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python