Get the App
SLTechnology News&Howtos  ›  Development  › 

How to assign a value to the ternary operator of python

Shulou Source: shulou.com Published: 2022-05-31 17:50:52 10月01日 Update

This article mainly introduces the Python ternary operator how to assign the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone will have a harvest after reading this Python ternary operator how to assign the article, let's take a look at it.

description

In Python, ternary operators are often called conditional expressions.

2, is based on the true (true)/false (not) conditions to judge, in Python 2.4 or above to perform ternary operations.

syntax

# X is conditional # Y is operational expression 1 #Z is operational expression 2 #If X is True, run Y, otherwise run ZY if X else Z

examples

Here's an example of how you can make code compact and concise. The following statement says,"If y is 9, assign x 10, otherwise assign x 20."

x = 10 if (y == 9) else 20 About "Python ternary operator how to assign" The content of this article is introduced here, thank you for reading! I believe everyone has a certain understanding of the knowledge of "how to assign values to python ternary operators." If you still want to learn more knowledge, please pay attention to the industry information channel.

Tags: Ternary operator condition knowledge expression operation content article run concise compact code value example example simple operation article easy to understand more to make Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Tech Info Huawei Redmi Linux