Quick answerPractise Python lists with six auto-checked tasks: total and average, second largest number, removing duplicates while keeping order, squaring even numbers with a list comprehension, rotating a list and merging two sorted lists. Each task reads numbers from input so you can test it like a real program.
Reading a list from input
Each exercise gives you a line of numbers separated by spaces, like 4 8 15 16 23 42. Turn it into a list of integers with:
nums = [int(x) for x in input().split()]
Then use len(nums), sum(nums), sorted(nums), slicing and loops. Print a list directly with print(nums), or as space-separated values with print(*nums).
Exercises
1. Total, smallest, largesteasy
Read a line of numbers and print three lines: the total, the smallest and the largest.
💡 Python has built-ins for all three: sum, min, max.
Show solution
nums = [int(x) for x in input().split()]
print(sum(nums))
print(min(nums))
print(max(nums))
The built-ins loop over the list for you; writing the loops by hand is a good extra exercise.
2. Second largestmedium
Print the second largest distinct number. For 5 9 9 3 the answer is 5.
💡 Remove duplicates with set(), sort, and take index -2.
Show solution
nums = [int(x) for x in input().split()]
print(sorted(set(nums))[-2])
set drops the repeated 9 so the second-to-last item is genuinely the second largest value.
3. Remove duplicates, keep ordermedium
Print the numbers with duplicates removed, keeping the order of first appearance, separated by spaces. 3 1 3 2 1 → 3 1 2.
💡 Keep a seen set and a result list; only append numbers you haven't seen.
Show solution
nums = [int(x) for x in input().split()]
seen = set()
result = []
for n in nums:
if n not in seen:
seen.add(n)
result.append(n)
print(*result)
A set can't keep order, which is why we also build a list. Shortcut: list(dict.fromkeys(nums)).
4. Squares of the even numberseasy
Use a list comprehension to print a list of the squares of the even numbers only. 1 2 3 4 → [4, 16].
💡 [n * n for n in nums if n % 2 == 0]
Show solution
nums = [int(x) for x in input().split()]
print([n * n for n in nums if n % 2 == 0])
A comprehension reads like English: n squared, for each n in nums, if n is even.
5. Rotate a listmedium
The first line has the numbers, the second line a number k. Rotate the list k places to the right and print it space-separated. 1 2 3 4 5 with k = 2 → 4 5 1 2 3. k may be bigger than the list.
💡 Reduce k with k % len(nums), then join two slices: nums[-k:] + nums[:-k] (careful when k is 0).
Show solution
nums = [int(x) for x in input().split()]
k = int(input()) % len(nums)
print(*(nums[-k:] + nums[:-k] if k else nums))
When k is 0, nums[-0:] is the whole list, so that case is handled separately.
6. Merge two sorted listshard
Two lines, each a sorted list. Print one sorted list containing all the numbers — without calling sort() or sorted().
💡 Use two indexes, i and j. Repeatedly take the smaller of a[i] and b[j]; when one list runs out, add the rest of the other.
Show solution
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
i = j = 0
out = []
while i < len(a) and j < len(b):
if a[i] <= b[j]:
out.append(a[i])
i += 1
else:
out.append(b[j])
j += 1
out += a[i:] + b[j:]
print(*out)
This is the merge step of merge sort. It touches each number once, which is faster than sorting the combined list.
Python Coding - Learn Python for Android: a real Python 3 interpreter, code editor, 26 example programs and 12 marked exercises.
Frequently asked questions
How do I read a list of numbers from input in Python?
nums = [int(x) for x in input().split()] splits the line on spaces and converts each piece to an int.
What is a list comprehension?
A compact way to build a list: [expression for item in iterable if condition].
How do I print a list without brackets?
Unpack it into print: print(*nums) prints the items separated by spaces.