Advanced · Comprehensions · 186 of 264
Divisors table
For every number from 1 to n, print the number, a colon, and its divisors.
Input: n (1 to 30). Output: lines like 6: 1 2 3 6
Examples
Input
6
Output
1: 1 2: 1 2 3: 1 3 4: 1 2 4 5: 1 5 6: 1 2 3 6
Input
1
Output
1: 1
One more test checks other inputs when you press Check.
Stuck? The idea behind comprehensions
A comprehension builds a list in one line: [x * x for x in range(5)]. Add if to keep only some: [x for x in nums if x > 0].
The same works for sets {…} and dictionaries {key: value for …}.
nums = [int(x) for x in input().split()] print([x * x for x in nums if x % 2 == 0])
Watch for: Clear first, short second: when a comprehension needs two ifs and two fors, a loop reads better.