I'm still not used to writing Python. .. .. The one-line notation of the title was clogged with "???", so I made a few memorandums. .. ..
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
■ 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
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