What is an Integer?
An integer is a whole number. It can be either positive or negative, and it does not have any decimal point. Here are some examples of integers:
- Positive integers: 1, 2, 3, 100
- Negative integers: -1, -5, -100
- Zero is also an integer: 0
In Python, integers are just numbers that you can work with when you’re doing math or calculations.
How to Write Integers in Python?
In Python, you don’t need to do anything special to write integers. You just type them directly. For example:
x = 10 # A positive integer
y = -3 # A negative integer
z = 0 # Zero is also an integer
Here, x
, y
, and z
are variables that hold integer values. You can use any name for your variables, as long as you follow Python’s naming rules.
Basic Operations with Integers
Python makes it easy to do math with integers. You can use the usual math operations like addition, subtraction, multiplication, and division.
1. Addition (+
)
This adds two numbers together.
x = 10
y = 5
z = x + y # This will give 15
print(z) # Output: 15
2. Subtraction (-
)
This subtracts one number from another.
x = 10
y = 5
z = x - y # This will give 5
print(z) # Output: 5
3. Multiplication (*
)
This multiplies two numbers.
x = 10
y = 5
z = x * y # This will give 50
print(z) # Output: 50
4. Division (/
)
This divides one number by another. The result will always be a float (a number with a decimal).
x = 10
y = 5
z = x / y # This will give 2.0 (not 2)
print(z) # Output: 2.0
5. Integer Division (//
)
If you want the result of a division to be a whole number (no decimal), use the //
operator. It rounds down to the nearest integer.
x = 10
y = 3
z = x // y # This will give 3 (because 10 divided by 3 is 3.333, but it rounds down)
print(z) # Output: 3
6. Modulo (%
)
The modulo operator gives the remainder when one number is divided by another.
x = 10
y = 3
z = x % y # This will give 1 (because 10 divided by 3 gives a remainder of 1)
print(z) # Output: 1
Working with Large Integers
Python can handle very large integers, much bigger than many other programming languages. You can write any size of integer without worrying about it being too big.
x = 1234567890123456789012345678901234567890 # A very large number
print(x)
Python will store and work with large numbers without any problems.
Converting Between Integers and Other Types
Sometimes, you may want to convert other types of data (like a string or float) into integers.
Convert from String to Integer
If you have a number written as text (like "10"
), you can convert it into an integer using the int()
function.
x = "10" # This is a string
y = int(x) # Now y is an integer (10)
print(y) # Output: 10
Convert from Float to Integer
If you have a float (a number with a decimal point) and want to turn it into an integer, you can use the int()
function again. This will remove the decimal part.
x = 10.5
y = int(x) # This will turn 10.5 into 10
print(y) # Output: 10
Integer Limits
In most programming languages, integers have a limit (the largest or smallest number they can handle). But in Python, integers are unlimited. This means you can work with very, very large numbers without any issues.