PCEP Practice Question: A programmer needs to store the number of items in a shopping cart
A programmer needs to store the number of items in a shopping cart. The value will be a whole number and may be used in arithmetic operations. Which Python data type is most appropriate for this variable?
⚠ Common exam trap
The trap here is assuming any numeric type works, but float may cause rounding and str will concatenate instead of add.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
int
The number of items in a cart is a whole number used in arithmetic, so int is the correct type. Floats are for decimal values and can introduce rounding, strings are text and would break calculations, and booleans only represent truth values. Using int keeps the data accurate and operations predictable.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
bool
Why it's wrong here
A bool holds only True or False. It cannot represent arbitrary whole numbers like 1, 2, or 10. While True and False can act as 1 and 0 in arithmetic, that is a side effect and not intended for storing counts. It would lose information about the actual number of items.
- ✓
int
Why this is correct
An int represents whole numbers, positive, negative or zero, with no fractional part, which is exactly what an item count is. It supports all arithmetic, such as adding items or multiplying by a price, and Python integers have arbitrary precision, so they never overflow. For example, 'cart_count = 3' then 'cart_count += 2' gives 5. It is also the natural type for indexing, loops with range() and comparisons.
- ✗
str
Why it's wrong here
A str stores text, so a count stored as '5' cannot be used for arithmetic directly: '5' + '3' concatenates to '53' instead of adding to 8, and '5' + 3 raises a TypeError. You would have to convert with int() every time. Strings are the right choice for names or descriptions, and input() always returns a string, which is why converting user input to int is a common first step.
- ✗
float
Why it's wrong here
A float can hold whole numbers such as 3.0, but it uses binary floating-point representation, which can introduce rounding errors, for example 0.1 + 0.2 is not exactly 0.3, and it signals to readers that fractional values are possible. A count of items is always whole, so int is clearer and exact. Floats suit measurements such as weights or averages; money is best handled with decimal.Decimal.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCEP question from scratch — 482 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
You might also like
Similar PCEP questions
- Which TWO of the following are valid Python variable names? (Choose two.)
- What is the output of the following code? x = [1, 2, 3]; y = x; y.append(4); print(x)
- The exhibit shows a JSON configuration. Which Python data structure is best suited to represent this configuration?
- Consider the code: x = 10; def func(): x = 5; print(x); func(); print(x). What is the output?
- A developer writes a function that returns multiple values. How should they return these values?
Written and reviewed by Johnson Ajibi, MSc IT Security
Security Systems Architect · founder of Courseiva
Last reviewed September 2026 · checked against the official Python Institute exam blueprint
This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.