In Python, integers are whole numbers without a fractional part. They can be positive, negative, or zero.
You can create integers simply by assigning whole numbers to variables:
a = 10
b = -25
c = 0
# 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
type(a) # <class 'int'>
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
/
returns a float
. Use //
for floor division if you want an integer result.