0
0

More than 1 year has passed since last update.

Python3 YesかNoを返す (例:ある要素が含まれているかでYesかNoを返す)真偽判定

Posted at

学習のアウトプットです

まずYesかNoを返す式

print("NYoe s"[1::2])
# Yes
print("NYoe s"[::2])
# No

となるので
1を入れている箇所に真偽判定で1か0を返す値を入れてあげると
YesかNoを返すようになる
そこで下記

print(False)
# False
print(False+0)
# 0
print(True)
# True
print(True+0)
# 1

TrueやFalseはintと混ぜると1や0を返してくれることを利用する
そしてまた下記

print("NYoe s"[judge::2])

judgeの部分に下記の要素を代入すると

# 文字の配列を用意
sampleList=['a', 'i', 'u', 'e', 'o']
# sampleListの配列は文字iを含んでいるか?
print("i" in sampleList) 
# True
print("NYoe s"["i" in sampleList::2])
# Yes

この場合
("i" in sampleList)は単体ではTrueかFalseを返すが
judgeの箇所に求められているのはintになるので勝手にint(1か0)に変換される

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0