## Definition

The modulus is what's left of a given number after subtracting another given number from it as many times as possible.

## 1. SPSS Modulus Syntax Example

- In SPSS, the modulus is implemented as
`mod(a,b)`

. It returns the remainder of`a`

after subtracting`b`

from it as many times as possible. - For instance,
`mod(1234,100)`

returns 34 since (12 * 100 =) 1200 can be extracted from 1234. - The syntax below shows how to extract the last two digits from a number using the modulus function.

***1. Create Test Data.**

data list free/number.

begin data

1

12

123

1234

12345

end data.

***2. Extract Last Two Digits.**

compute last_two_digits=mod(number,100).

exe.

## 2. Python Modulus Example

- In Python, the modulus is implemented as
`a % b`

. As in SPSS, it returns the remainder of`a`

after subtracting`b`

from it as many times as possible. - The syntax below shows how to identify odd and even numbers using the modulus in Python.

***Inspect whether numbers from random sample are odd or even.**

begin program.

import random

mySample = random.sample(range(10),5)

for i in mySample:

if i % 2 == 0:

print str(i) + " is an even number"

else:

print str(i) + " is an odd number"

end program.

## This tutorial has 1 comment!

## By Aviad on August 28th, 2016

What about mod(-16, 24) => 8?

I recommned that the negative usage, in this example calculating sleep hours between 23 (11pm) and 7am be mentioned as well.