Ternary operator (Python)

By using the ternary operator, conditional branching of assignment can be described smartly in one line.

Usage ```plaintext (variable) = (Value when the condition is True) if (conditions) else (conditionsがFalseのときの値) ```

Example For example, when you want to divide the value of x according to the following conditions

python


if n == 10:
    x = "OK"
else :
    x = "NG"

This can be expressed in one line using the ternary operator as follows.

python


x = "OK" if n == 10 else "NG"

Recommended Posts

Ternary operator (Python)
Python 3 operator memo
Python operator overload precedence
Nesting Python ternary operators
Python notes using perl-ternary operator
Python
Python bitwise operator and OR
Python or and and operator trap
Nesting ternary operators in python
String format with Python% operator
Create a Kubernetes Operator in Python
Python in is also an operator
kafka python
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
Python service
python tips
[Road to intermediate Python] Use ternary operators
python function ①
Python basics
Python memo
ufo-> python (3)
Python comprehension
install python
Python Singleton
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Python increment
atCoder 173 Python
[Python] function
Python installation
python tips
[Python] Round up with just the operator
Try python
Python memo
Python iterative
Python2 + word2vec
Python functions
Python tutorial
python underscore
Python summary
Start python
[Python] Sort
Note: Python
Python basics ③
python log
Python basics
[Scraping] Python scraping
Python update (2.6-> 2.7)
python memo
Python memorandum