[자료구조] Tuple의 in과 not in

신은지·2024년 8월 1일

Data Structure

목록 보기
17/31
post-thumbnail

in과 not in

  • 아이템의 존재 有/無를 알 수 있다.
  • 문자열에서도 사용 가능하다.

💡Python으로 아이템의 존재 有/無 출력하기1

# 컴퓨터가 1부터 10까지 5개의 난수를 생성한 후, 사용자가 입력한 숫자가 있는지 또는 없는지 출력
import random

randomNum = random.sample(range(1, 11), 5)
userNum = int(input('숫자 입력: '))

if userNum in randomNum:
    print('randomNum: {}'.format(randomNum))
    print('정답~!!')
else:
    print('randomNum: {}'.format(randomNum))
    print('꽝~!!')

💡Python으로 아이템의 존재 有/無 출력하기2

# 문장에서 비속어가 있는지 알아내기
wrongWord = ["짭새", '담탱이', "꼰대", '찐따']
sentence = '꼰대 같은 담탱이가 짭새처럼 등장해 찐따를 구했다.'
for word in wrongWord:
    if word in sentence:
        print('비속어: {}'.format(word))





* 이 글은 제로베이스 데이터 스쿨의 강의 자료 일부를 발췌하여 작성되었습니다.

profile
I believe there is no best, only better

0개의 댓글