← Python for Beginners
0 of 27 lessons done0%

Collections · Lesson 18 of 27

Reading many numbers

In coding tests, input often comes as many numbers on one line, like 4 7 1 9.

input().split() breaks the line into a list of strings. map(int, ...) converts each one to an int:

nums = list(map(int, input().split()))
print(nums)        # [4, 7, 1, 9]
print(sum(nums))   # 21

Memorise this line, you'll use it in almost every coding round:

nums = list(map(int, input().split()))

If the first line tells you how many numbers there are, read it separately:

n = int(input())
nums = list(map(int, input().split()))

Your task

Read a line of numbers separated by spaces. Print how many of them are even.

For 4 7 1 8 10, print 3.