Python
python 기초 문법 - 반복문
chillcoder
2023. 3. 21. 00:46
문제)
리스트에서 짝수의 개수 출력하기
해답)
num_list = [1, 2, 3, 6, 3, 2, 4, 5, 6, 2, 4]
count = 0
for num in num_list:
if num % 2 == 0:
count += 1
print(count)
num_list = [1, 2, 3, 6, 3, 2, 4, 5, 6, 2, 4] 에서
하나씩 출력하는데 (for 변수 in num_list:)
짝수인 것만 출력 (if num % 2 == 0:)
여기서, 개수를 구하려는 것이기 때문에
변수 count를 0으로 두고
짝수가 나올 때마다
count를 하나씩 높여주기 (count = count +1)
그런 다음 print(count)