LoginSignup
0
0

More than 3 years have passed since last update.

If element in list with python3

Last updated at Posted at 2021-05-17

test.py

import sys

us = [1, 2, 3, 0, 4]
us2 = []

sa = sys.argv[1]
sai = int(sa)

print(us,us2)
print(sa,sai)

print("==no parenthese==")
if sa in us == True:
    print("aru")
else:
    print("nai")

if sai in us == True:
    print("aru2")
else:
    print("nai2")

if sai in us2 == False:
    print("nai3")
else:
    print("aru3")

if len(us2) > 0 and sai in us2 == True:
    print("aru4")
else:
    print("nai4")

print("==parenthese==")
if (sa in us) == True:
    print("aru")
else:
    print("nai")

if (sai in us) == True:
    print("aru2")
else:
    print("nai2")

if (sai in us2) == False:
    print("nai3")
else:
    print("aru3")

if len(us2) > 0 and (sai in us2) == True:
    print("aru4")
else:
    print("nai4")

execution

# python3 --version
Python 3.6.8
# python3 test.py 2
[1, 2, 3, 0, 4] []
2 2
==no parenthese==
nai
nai2
aru3
nai4
==parenthese==
nai
aru2
nai3
nai4

memo

  • 型が合っていないと意図した結果にならない
  • element in list の結果を判定するには括弧で括る必要がある
  • 最後のは蛇足
0
0
1

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