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 Code Copy

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 Code Copy

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 Code Copy

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 Code Copy

Output

7
-1
12
0
0.75

Summary

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

Frequently Asked Questions

1. What are the numeric datatypes in Python?

Answer:

int, float, and complex are the numeric datatypes in Python.

Related Tutorials

Quiz on

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

Not answered

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

Not answered

Q3. What is the output of following program?

x = int(12.3)
print(x)
Run Code Copy
Not answered

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

Not answered
Code copied to clipboard successfully 👍