One liner in Python

In Python, one-liner is hard to do, but it's not impossible. I think that the reason why one-liner is difficult to do in Python is that there are few things that can be expressed and that compound sentences cannot be written in one line. Here, we will introduce a method for incorporating various processes into an expression.

Basic method

and, or, ternary operator

ʻIf a: f () can be written as ʻa and f (). ʻIf not a: f () can be written as ʻa or f (). (If you are not familiar with the short circuit of logical operators, please see http://qiita.com/gyu-don/items/a0aed0f94b8b35c43290.) Also, the ternary operator has a slightly unpleasant syntax. true_value if condition else false_value

Tuple and list

You can combine multiple expressions into one expression by creating tuples and lists. You can also retrieve the value at a specific location by referring to it with [0] or [-1] at the end.

Loop using list comprehension

Loops are written in list comprehensions.

generator

Especially in Python3, generator objects appear everywhere. The difference between a list and a generator is important, especially when you're expecting side effects. List comprehensions create a list up to the last element on the fly. On the other hand, the generator does not evaluate each element until it is called by next (). To evaluate each element, you can either call next yourself, do something like list (gen), or use the list comprehension for ... in.

This and that using lambda

lambda You can create anonymous functions with lambda, but in Python you can only create anonymous functions with a single expression.

Instead of let

You can use lambda to bind a variable without making a statement. Please use it in situations where it is difficult to use a semicolon. (lambda a, b: a + b) (f (), g ()) realizes ʻa = f (); b = g (); a + b` in the expression.

Keep a list

In Python, assignments are statements, but in list.append they are expressions. It may be useful to have a list as an argument. Example: An expression that returns the result of addition between 0 and 100 without using sum or reduce (lambda t: [t.append(t.pop()+x)or t[0] for x in range(101)][-1])([0])

Recursion

You don't have to make a complicated Y combinator, you just need the function to have an argument to receive itself. Example: Factorial 1-10 (lambda f: f(10,f))(lambda n,f: n*f(n-1,f) if n>0 else 1)

Then everyone, have a good Python life

end.

Recommended Posts

One liner in Python
python Zundko One Liner
One liner webServer (with CGI) in python
One liner that outputs multiplication tables in Python
Fizzbuzz in Python (in one line)
curl -I python one liner
One liner to 100% CPU1 core usage in Python
DMD in Python one dimension
One liner that outputs 1000000 digits of pi in Python
Make python segfault in one line
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
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
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
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Handle multiple python versions in one jupyter
CGI server (1) python edition in one line
Decompose command arguments in one line in Python
[Python] Invert bool value in one line
I wrote Project Euler 1 in one liner.
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python