简体   繁体   中英

Python Syntax, value if else

Saw this line in a Leetcode solution:

top_element = stack.pop() if stack else '#'

I understand what is happening, but what is the syntax called? Is this a ternary operator? Just want to understand to use it correctly.

It's officially called Conditional expression in python 3 documentation. Usually it's also called ternary operator, ternary conditional operator or something like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM