Python int

Python int – Integer Datatype

int in Python is one of the basic built-in datatype among numeric datatypes, the others being float and complex.

In this tutorial, we shall learn how to initialize an integer, what range of values an integer can hold, what arithmetic operations we can perform on integer operands, etc.

Python Int – Initialize

To initialize a variable with integer value, use assign operator and assign the integer value to the variable. Variable name has to be on the left hand side and the integer value has to be on the right side.

In the following program, we have initialized variables x and y with integer values.

Python Program

x = 20
y = 45

print(x)
print(y)
Run

Python Int – Print to Standard Console Output

To print an integer to standard console output, we can use built-in function print(). print() accepts integer as argument.

In the following example, we shall initialize an integer and print it to console.

Python Program

x = 10
print(x)
Run

Output

10

Python Int – Range or Bounds

In Python 3, there is no bound on the maximum or minimum value an integer variable can hold.

In the following example, we will take a very large number and check if the type of the value is int.

Python Program

x = 922337203685899999999999999999999998542114775897
print(x)
print(type(x))
Run

Output

922337203685899999999999999999999998542114775897
<class 'int'>

Python Int – Arithmetic Operations

We can perform all arithmetic operations with integers as operands.

In the following example, we shall take two integers in a and b, and perform arithmetic operations with these integers as operands for arithmetic operators.

Python Program

a, b = 3, 4

#addition
print(a+b)

#subtraction
print(a-b)

#multiplication
print(a*b)

#integer division
print(a//b)

#float division
print(a/b)
Run

Output

7
-1
12
0
0.75

Summary

In this tutorial of Python Examples, we learned about different aspects of an integer in Python.

Quiz - Let's see if you can answer these questions

Q1: Which of the following is an integer value in Python?

Q2: Which function is used to define an integer in Python?

Q3: What is the output of following program?

x = int(12.3)
print(x)
Run

Q4: Which of the following statement does not throw an error?