Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

special.comb

Computes the number of ways to choose k elements from N items, optionally allowing repetition. It can return a floating-point result or an exact integer for integer inputs.

How it works

comb computes the number of ways to select k elements from N items. It can use floating-point arithmetic or return an exact integer, and repetition=True switches to combinations where selections may repeat.

Syntax

scipy.special.comb(N, k, *, exact=False, repetition=False)

Parameters

NameTypeDescription
Nint or ndarrayNumber of things.
kint or ndarrayNumber of elements taken.
exactbool, optionalControls whether integer inputs use exact computation instead of floating-point precision.
repetitionbool, optionalControls whether combinations with repetition are counted.

Returns

val — int, float, or ndarray: The total number of combinations.

Key points

  • The function calculates an “N choose k” combination count.
  • N specifies the size of the source collection.
  • k specifies how many elements are selected.
  • Use exact=True when an exact integer result is required for integer inputs.
  • Enable repetition to allow the selected elements to repeat.
  • The return value is the computed combination total.

Common mistakes

  • Negative N or k produces zero rather than an exception.
  • Requesting more elements than available returns zero when repetition is disabled.

Example: Compute combinations

from scipy.special import comb

print(comb(10, 3))
print(comb(10, 3, exact=True))
print(comb(10, 3, exact=True, repetition=True))

Computes ordinary, exact-integer, and repetition-allowed combinations.

When to use it

Combination counting

Counting distinct fixed-size selections from a finite collection.

Exact combinatorics

Calculating exact integer counts where rounding is unacceptable.

Sampling with repetition

Counting selections in which an item may be chosen repeatedly.

Interview questions

QWhat does scipy.special.comb compute?

comb(N, k) returns the number of combinations of N items taken k at a time. By default it uses floating-point precision, while exact=True computes an exact result for integer inputs.

Follow-up: How does repetition change the calculation? With repetition=True, it counts selections in which an item can be chosen more than once.

QWhat inputs and output types does comb support?

N and k may be integers or ndarrays. The returned value can be an integer, float, or ndarray, matching the scalar or array-oriented computation.

Follow-up: What should be checked when using arrays? Ensure N and k have compatible shapes for the intended elementwise calculation.

QHow do exactness and repetition affect results?

Set repetition=True to count combinations with repetition. For example, comb(10, 3, exact=True, repetition=True) returns 220, whereas the ordinary exact result is 120.

Follow-up: What happens when k exceeds N without repetition? The function returns 0 rather than a combination count.

  • scipy.special.perm
  • scipy.special.factorial
  • scipy.special.binom

Source: scipy.special.comb — SciPy v1.18.0 Manual. Checked 2026-10-15.