# python三元运算

Python三元表达式和其他语言java类似：

```python
result = x if condition else y
```

也可以写成（这种方式可能更清晰）

```python
result = (x, y)[condition]
```

> 方法二有个不太方便的地方是，对于列表引用，由于列表的索引需要使用`[]`可能会造成困扰。我通常使用方法一。

## 参考

* [Python三元表达式](http://www.jianshu.com/p/fcad6f70feec)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://huataihuang.gitbook.io/cloud-atlas-draft/develop/python/startup/ternary_conditional_operator.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
