Integers

Data Types

Integers

In this tutorial, we'll use whole numbers in calculations and comparisons.

An integer is a whole number. It has no decimal point.

age = 14
score = 100
temperature = -3

Integers are used for counts, positions, scores, quantities, and anything that should be a whole number.

Basic Arithmetic

Python can do normal arithmetic with integers:

apples = 6
bananas = 4

print(apples + bananas)
print(apples - bananas)
print(apples * bananas)

Output:

10
2
24

Division

Regular division uses / and returns a float:

print(10 / 2)

Output:

5.0

Even though the answer is mathematically whole, Python gives 5.0 because / means decimal division.

For whole-number division, use //:

print(10 // 3)

Output:

3

For the remainder, use %:

print(10 % 3)

Output:

1

Updating Integer Variables

Integers are often used as counters:

score = 0
score = score + 10
score = score + 5

print(score)

Output:

15

You will see this pattern often in loops.

Common Mistake

Do not put quotes around numbers if you want maths:

print("10" + "5")

Output:

105

That is string joining, not addition.

Practice

Create variables for the number of students and the number of desks. Print how many students will not have a desk if there are not enough.