Python hand play (one line notation of if)

What is this article?

I'm still not used to writing Python. .. .. The one-line notation of the title was clogged with "???", so I made a few memorandums. .. ..

So what?

You can write an if statement in one line in Python. It's like this.

# (Like this)
>>> print('True') if True else print('False')
True

I wanted to write this using it.

# (this)
>>> if True:
...     para = 'True'
... else:
...     para = 'False'
...
>>> para
'True'
>>>                                                                                                    

So, when I wrote it, I got an error.

>>> para = 'True' if True else para = 'False'
  File "<stdin>", line 1
SyntaxError: can't assign to conditional expression
>>>

It seems to write correctly like this.

>>> para = 'True' if True else 'False'
>>> para
'True'

I see.

Substitute the right side for the variable "para" on the left side. The story of what to write as the right side. With that in mind, the value "'True' if True else'False'" holds, but The value "'True' if True else para ='False'" is unnatural. I was a little convinced.

>>> 'True' if True else 'False'
'True'
>>> 'True' if True else para = 'False'
  File "<stdin>", line 1
SyntaxError: can't assign to conditional expression

Referenced site

■ Write an if statement in one line with Python's ternary operator (conditional operator) https://note.nkmk.me/python-if-conditional-expressions/

■Why is this simple conditional expression not working? [duplicate] https://stackoverflow.com/questions/32954481/why-is-this-simple-conditional-expression-not-working

A little deep digging ...

No, I was wondering if I wouldn't sleep because it's a weekday, but I was curious.

I thought this was the first misunderstanding.

(Instruction executed when True) if (Condition) else (Instruction executed when False)

But this is a little different. (Maybe it's true in a sense ...) To the last, the if statement returns a value based on a condition. Such.

(Value returned when True) if (Condition) else (Value returned when False)

Then I was wondering what the first print statement was.

>>> a = print(3)
3
>>> a
>>> type(a)
<class 'NoneType'>
>>> a
>>> a is None
True

I see. Is print a void type in C #? No. I wonder if it returns None in Object type.

So, the characters are printed in the process of being executed to get the value "print ('True')".

I'm convinced.

Recommended Posts

Python hand play (one line notation of if)
Python hand play (calculated full of mordred)
Python hand play (division)
Python hand play (two-dimensional list)
Fizzbuzz in Python (in one line)
Decrypt one line of code in Python lambda, map, list
Python hand play (argparse minimum code)
Python hand play (Pandas / DataFrame beginning)
Make python segfault in one line
[python] Correct usage of if statement
Create another version of Python conda environment with one command line
CGI server (1) python edition in one line
Python hand play (descriptor calculation: serious version)
Decompose command arguments in one line in Python
[Python] Invert bool value in one line
Basic grammar of Python3 system (included notation)
Python hand play (let's get started with AtCoder?)
Python hand play (interoperability between CSV and PostgreSQL)
Make a rock-paper-scissors game in one line (python)
Format one line of json for easy viewing
Python if statement
Introduction of Python
Combine multiple csv files into one csv file with python (assuming only one line of header)
Play Python async
Play with 2016-Python
Basics of Python ①
Basics of python ①
Copy of python
LINE heroku python
How to check in Python if one of the elements of a list is in another list
[Python] if statement
Introduction of Python
Python text reading for multiple lines and one line
[Python beginner] If __name__ == Move your hand to understand'__main__'.
Python that merges a lot of excel into one excel
One liner that outputs 1000000 digits of pi in Python
[Python] Using Line API [1st Creation of Beauty Bot]
[For play] Let's make Yubaba a LINE Bot (Python)
Make Python segfault on one line without using ctypes
Python hand play (get column names from CSV file)