Python - 문제 미리보기
문제 1977
medium
다음 코드의 실행 결과는?
```python
data = {"apple", "123", "banana", "456"}
count_alpha = 0
count_digit = 0
for item in data:
if item.isalpha():
count_alpha += 1
elif item.isdigit():
count_digit += 1
print(f"문자: {count_alpha}, 숫자: {count_digit}")
```
```python
data = {"apple", "123", "banana", "456"}
count_alpha = 0
count_digit = 0
for item in data:
if item.isalpha():
count_alpha += 1
elif item.isdigit():
count_digit += 1
print(f"문자: {count_alpha}, 숫자: {count_digit}")
```
정답: A
⦁ 집합의 모든 요소를 순회하며 문자열 타입 검사
⦁ `isalpha()`: 모든 문자가 알파벳인지 확인
⦁ `isdigit()`: 모든 문자가 숫자인지 확인
각 요소별 분석:
⦁ `"apple"`: `isalpha()` → True (문자 카운트 +1)
⦁ `"123"`: `isdigit()` → True (숫자 카운트 +1)
⦁ `"banana"`: `isalpha()` → True (문자 카운트 +1)
⦁ `"456"`: `isdigit()` → True (숫자 카운트 +1)
최종 결과:
⦁ `count_alpha = 2` ("apple", "banana")
⦁ `count_digit = 2` ("123", "456")
💡 학습 팁
이 문제를 포함한 Python 과목의 모든 문제를 순차적으로 풀어보세요. 진행상황이 자동으로 저장되어 언제든지 이어서 학습할 수 있습니다.