[Python 3.8 ~] How to define a recursive function smartly with a lambda expression

Introduction

You may want to use recursion in a lambda expression (mainly in chords). If it is Python 3.8 or later, it will be refreshing. Let's compare with the function that finds the xth of the Fibonacci sequence.

Method

〜Python3.7

print((lambda x: (lambda f, x: f(f, x))(lambda f, x: f(f, x-1)+f(f, x-2) if x > 2 else 1, x))(10))
# >> 55

Python3.8〜

print((self:=lambda x:self(x-1) + self(x-2) if x > 2 else 1)(10))
# >> 55

** Sugoku Sukiri **

Recommended Posts

[Python 3.8 ~] How to define a recursive function smartly with a lambda expression
How to make a recursive function
[Introduction to Python] How to split a character string with the split function
How to read a CSV file with Python 2/3
[Python] Explains how to use the range function with a concrete example
[Introduction to Python] How to write a character string with the format function
How to convert / restore a string with [] in python
[Python] How to draw a line graph with Matplotlib
[Introduction to Python] How to iterate with the range function?
How to call a function
Consider a conversion from a Python recursive function to a non-recursive function
[Python] How to create a 2D histogram with Matplotlib
How to use Python lambda
[Python] How to call a c function from python (ctypes)
[Python] How to draw a scatter plot with Matplotlib
How to install NPI + send a message to line with python
How to convert an array to a dictionary with Python [Application]
Create a Mastodon bot with a function to automatically reply with Python
How to print characters as a table with Python's print function
How to define multiple variables in a python for statement
How to build a python2.7 series development environment with Vagrant
[Introduction to Python] How to get data with the listdir function
How to write a Python class
Python: How to use async with
How to get started with Python
[Python] Make the function a lambda function
How to use FTP with Python
How to calculate date with python
How to use python zip function
How to batch start a python program created with Jupyter notebook
How to make a surveillance camera (Security Camera) with Opencv and Python
How to create a heatmap with an arbitrary domain in Python
[Python3] Define a decorator to measure the execution time of a function
[Python] Explains how to use the format function with an example
Slack --APIGateway --Lambda (Python) --How to make a RedShift interactive app
[Python] How to get a value with a key other than value with Enum
[ROS2] How to play a bag file with python format launch
How to send a request to the DMM (FANZA) API with python
How to create a serverless machine learning API with AWS Lambda
Create a Python function decorator with Class
Use print in a Python2 lambda expression
How to dynamically define variables in Python
How to add a package with PyCharm
[Python] How to make a class iterable
[Python] How to convert a 2D list to a 1D list
[python] How to use __command__, function explanation
How to work with BigQuery in Python
[Python] How to invert a character string
How to get a stacktrace in python
How to do portmanteau test with python
How to display python Japanese with lolipop
How to access RDS from Lambda (python)
Lambda function to take AMI backup (python)
Connect to s3 with AWS Lambda Python
How to enter Japanese with Python curses
[Python] Understand how to use recursive functions
[Python] How to deal with module errors
How to run a Maya Python script
How to install python3 with docker centos
How to generate a new loggroup in CloudWatch using python within Lambda
[Introduction to Python] How to sort the contents of a list efficiently with list sort