Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use lambda Anonymous function and Ternary operator in Python

Shulou Source: shulou.com Published: 2022-05-31 21:15:35 10月03日 Update

This article mainly explains "how to use lambda anonymous functions and ternary operators in Python". The explanation in this article is simple and clear and easy to learn and understand. Please follow Xiaobian's train of thought to study and learn how to use lambda anonymous functions and ternary operators in Python.

Anonymous function what is an anonymous function

A function that only returns a value in one sentence is an anonymous function.

Anonymous functions are only used to implement some simple functions, so the pursuit of code simplicity and efficiency.

It is defined using the keyword lambda, so anonymous functions are also called lambda expressions.

classification

Lambda expression with no parameter

# ordinary function def func (): return 'hello motherland'# calls res = func () print (res) # hello motherland# lambda expression func = lambda:' hello motherland'# calls res = func () print (res) # hello motherland

Lambda expressions with parameters

# ordinary function def func (val): return val# lambda expression func = lambda val: val

Lambda expression with judgment condition

Def func (val): if val = = 0:return 'yes'return' no'# lambda expression func = lambda val: 'yes' if val = = 0 else' no' ternary operator

Ternary operators are generally called ternary operators. It is a fixed format in software programming and an indispensable logic judgment algorithm in many programming languages. the logic concept is a simple if conditional judgment statement, which can be filtered step by step when using this algorithm to call data.

Syntax: conditional expression? Conditional judgment: conditional trans

It is commonly used in many languages. ?. :. Symbols are used as ternary operators, but this is not the case with python, which is used in python. If... Else... As the use of ternary operators, this also reflects the advantages of python over other languages, simpler syntax and higher statement visualization.

Syntax: lambda [parameter]: true value if conditional expression else false value

If the conditional expression is true, it returns a true value, and vice versa.

Res = 'yes' if 1 = = 1 else' no'print (res) # yes thank you for reading, this is the content of "how to use lambda anonymous functions and ternary operators in Python". After the study of this article, I believe you have a deeper understanding of how to use lambda anonymous functions and ternary operators in Python. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Functions expressions operators operations ternary conditions parameters that is grammar language learning ordinary content truth algorithms statements logic programming simplicity indispensable Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Linux Redmi MySQL Shulou Information