[Road to intermediate Python] Use ternary operators

Link to summary

https://qiita.com/ganariya/items/fb3f38c2f4a35d1ee2e8

Introduction

In order to study Python, I copied a swarm intelligence library called acopy.

In acopy, many interesting Python grammars and idioms are used, and it is summarized that it is convenient among them.

This time, we will learn Python's ternary operator.

Ternary operator

The ternary operator is used in C ++ etc. An operator that ends a statement that can be expressed by if-else with one line, such as conditional expression: true value: false value.

Of course, the amount of description will be much shorter, but nesting the ternary operator will significantly reduce the visibility.

If you want to nest, be aware of reviewing the algorithm and writing it normally with an if statement.

Python ternary operator

Python's ternary operator True value if conditional expression else False value and The order is different from C ++ and C language.

For me who use C ++ or C, this is a notation that feels quite strange. In C ++

  1. Write the conditional expression for the time being!
  2. Write the True value
  3. Write a False value

I'm glad that it can flow naturally, but in the case of Python

  1. Conditional expression ... If it is True, this value is correct.
  2. Conditional expression
  3. False

I feel that the order is reversed and it is very difficult to do. I wonder if this is even possible with Vimmer.

Even and odd

For the time being, I will write an even-odd number judgment.


a = int(input())

s = "odd" if a % 2 else "even"
print(s)

I feel that the way of writing here is like English. I want to remember the order of true conditional expression and false first.

Even odd (excluding negative)

If there is a negative, it is troublesome, so if it is negative, output minus.

a = int(input())

s = "minus" if a < 0 else "even" if a % 2 == 0 else "odd"
print(s)

First, check if it is negative, and if it is negative, end with minus. If not, check even, odd again with the nested ternary operator.

Finally

Stop using the ternary operator! (moderately)

Recommended Posts

[Road to intermediate Python] Use ternary operators
[Road to intermediate Python] Use lambda expressions
A road to intermediate Python
[Road to Intermediate] Understanding Python Properties
[Road to intermediate Python] Use if statement in list comprehension
[Road to intermediate Python] Article link summary
Nesting Python ternary operators
[Road to Intermediate] Python seems to be all objects
python3: How to use bottle (2)
[Python] How to use list 1
[Road to Python Intermediate] Define __getattr__ function in class
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
[Road to intermediate Python] Define in in your own class
Easy to use E-Cell 4 Intermediate
[Python] How to use input ()
Nesting ternary operators in python
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
[Road to intermediate Python] Install packages in bulk with pip
[Road to intermediate Python] Enables comparison operations with original classes
Python: How to use async with
[Python] How to use Pandas Series
How to use Requests (Python Library)
How to use SQLite in Python
[Python] Road to snake charmer (3) Python class
Road to Linux Intermediate: Network Edition
[Introduction to Python] Let's use pandas
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
Easy to use Jupyter notebook (Python3.5)
[Introduction to Python] Let's use pandas
How to use python zip function
[Introduction to Python] Let's use pandas
[Python] How to use Typetalk API
[Road to Python Intermediate] Call a class instance like a function with __call__
[Road to Python intermediate] Dynamically specify execution method by variable name
[Python] Summary of how to use pandas
[Introduction to Python] How to use class in Python?
[Python] Use pandas to extract △△ that maximizes ○○
How to install and use pandas_datareader [Python]
[Python] Road to snake charmer (1) Environment construction
[python] How to use __command__, function explanation
I want to use jar from python
[Introduction to Python] Let's use foreach with Python
[Python] How to use import sys sys.argv
Easy way to use Wikipedia in Python
[Python] Organizing how to use for statements
Memorandum on how to use gremlin python
[Python2.7] Summary of how to use unittest
python: How to use locals () and globals ()
How to use __slots__ in Python class