Python Coding Exercise #12: Students in Groups
Part of the series Python Practice Exercise — part 12
Before you start: Know input() and int() · Know arithmetic operators
This is the first exercise in the series that uses an if statement, so we’ll look at how one works along the way. The next exercise, Exercise #13, practises if statements further.
The question
Please write a program which asks for the number of students on a course and the desired group size. The program will then print out the number of groups formed from the students on the course. If the division is not even, one of the groups may have fewer members than specified.
How many students on the course? 8
Desired group size? 4
Number of groups formed: 2
Another example:
How many students on the course? 11
Desired group size? 3
Number of groups formed: 4
Hint: the integer division operator
//could come in handy here.
The exercise also mentions that it can be solved using a conditional construction, which means an if statement.
My Solution
students = int(input("How many students on the course? "))
group_size = int(input("Desired group size? "))
if students % group_size == 0:
print(f"Number of groups formed: {students//group_size}")
else:
print(f"Number of groups formed: {students//group_size + 1}")
How My Solution Works
1. Operators used
Two arithmetic operators are the key to this exercise:
| Operator | Name | What it gives you | 11 and 3 |
|---|---|---|---|
// | integer division | how many whole times one number fits into another | 11 // 3 is 3 |
% | remainder (modulo) | what is left over after that division | 11 % 3 is 2 |
Eleven students in groups of three make 3 full groups, with 2 students left.
2. The if statement decides what to do with the remaining members
The remaining students still need a group, and that’s where if and else come in:
if students % group_size == 0:
...
else:
...
Here is how to read it:
students % group_size == 0is a question: “is the remainder zero?” Python answersTrueorFalse. (==means “is equal to”.)- If the answer is True, the indented lines under
ifrun. The students divide perfectly, so the number of groups is juststudents // group_size. - If the answer is False, the indented lines under
elserun. There are students remaining, so they form one extra group, and the number of groups isstudents // group_size + 1.
Two details of the syntax: the if and else lines end with a colon, and the lines that belong to them are indented. Python uses the indentation to know which lines belong to which branch.
3. Following the two examples
| Students | Group size | students % group_size | Which branch runs? | Groups |
|---|---|---|---|---|
| 8 | 4 | 0 | if (no remainder) | 8 // 4 = 2 |
| 11 | 3 | 2 | else (has a remainder) | 11 // 3 + 1 = 3 + 1 = 4 |
In the second case the last group has only 2 students instead of 3, which is exactly what the exercise allows: “one of the groups may have fewer members than specified.”
A note on wording
It’s easy to say “even” and “odd” when describing this, but those words mean something different in maths. An even number is divisible by 2. Here, the question is whether the students divide evenly into groups of the chosen size. Eleven students is an odd number, but they would divide evenly into groups of 11. So the check is about the remainder being zero, not about the number being even or odd.
Solving it without an if
The exercise says it can be solved with a conditional, which hints that it doesn’t have to be. What we really want is the division rounded up: 11 / 3 is about 3.67, and rounding up gives 4. Python has a function for this:
import math
students = int(input("How many students on the course? "))
group_size = int(input("Desired group size? "))
print(f"Number of groups formed: {math.ceil(students / group_size)}")
math.ceil() rounds a number up to the next whole number (ceil is short for “ceiling”). It gives the same answers as the if version: math.ceil(8 / 4) is 2 and math.ceil(11 / 3) is 4.
There is also a classic trick that needs no import, using only integer division:
print(f"Number of groups formed: {(students + group_size - 1) // group_size}")
Adding group_size - 1 before dividing pushes any leftover students over the edge into a new group, while an exact fit stays the same. For 11 students in groups of 3: (11 + 2) // 3 = 13 // 3 = 4.
The if version is the most readable for a beginner, because each branch says what it does. The shorter versions are useful to recognise when you meet them in other people’s code.
What if the group size is zero?
If the user enters 0 as the group size, the program stops with an error, because you can’t divide by zero:
ZeroDivisionError: division by zero
The exercise doesn’t ask you to handle this. Later in the course you’ll learn how to check for it with a condition before dividing.
Key takeaways
//is integer division. It tells you how many whole times one number fits into another.%gives the remainder, what is left over after that division.if/elselets a program choose between two actions. The lines end with a colon, and the lines inside are indented.students % group_size == 0asks “does this divide evenly?” This is about the remainder, not about even or odd numbers.- Rounding a division up gives the number of groups. You can do it with
if/else, withmath.ceil(), or with the(a + b - 1) // btrick. - Dividing by zero raises a
ZeroDivisionError.
✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦
Practice Source: This exercise is taken from the Python Programming MOOC 2026 by the University of Helsinki. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.