목록에서 문자열을 찾아야 하는 경우 'in' 연산자와 함께 간단한 'if' 조건을 사용할 수 있습니다.
예
아래는 동일한 데모입니다.
my_list = [4, 3.0, 'python', 'is', 'fun']
print("The list is :")
print(my_list)
key = 'fun'
print("The key is :")
print(key)
print("The result is :")
if key in my_list:
print("The key is present in the list")
else:
print("The key is not present in the list") 출력
The list is : [4, 3.0, 'python', 'is', 'fun'] The key is : fun The result is : The key is present in the list
설명
- 정수 및 문자열 목록이 정의되어 콘솔에 표시됩니다.
- 키 값이 정의되어 콘솔에 표시됩니다.
- 'if' 루프는 키가 목록에 있는지 확인하는 데 사용됩니다.
- 예인 경우 콘솔에 결과가 표시됩니다.