Python Integers

What Are Integers?

In Python, integers are whole numbers without a fractional part. They can be positive, negative, or zero.

Key Properties

Creating Integers

You can create integers simply by assigning whole numbers to variables:

a = 10
b = -25
c = 0

Common Integer Operations

# Addition
x = 15 + 5     # 20

# Subtraction
y = 20 - 4     # 16

# Multiplication
z = 3 * 7      # 21

# Division (returns float)
d = 10 / 3     # 3.3333333333333335

# Floor Division (integer division)
f = 10 // 3    # 3

# Modulus (remainder)
m = 10 % 3     # 1

# Exponentiation
e = 2 ** 4     # 16

Checking Type

type(a)   # <class 'int'>

Converting to Integer

You can convert other types to integers using the int() function:

int("42")      # 42
int(3.99)       # 3 (fractional part truncated)
int(True)       # 1
int(False)      # 0

Important Notes