In this tutorial, you will all about the Python int() function to convert the specified value into an integer number.
int function in Python is the best built-in function to convert the specified value into an integer. In the previous tutorial, we have seen the input() function to ask value from the users.
Table of Contents
Python int Function
Python int() function in python is a built-in function that is used to convert the specified value into a number integer.
Syntax
The syntax of python int() function is:-
int(value, base)
Parameter
int function in python take two parameters.
- value:- A number or string to be converted into integer number.
- base:- A number representing the number format, Default value: 10.
Return Value
Return value of int function in python is converted integer number.
Python int() example
Here we will take some examples to understand int function.
Example 1:
print(int('120'))
print(int(12.20))
print(int(21))
Output
120
12
21
Example 2:
Python int function for decimal, octal and hexadecimal.
#Convert binary to int
print(int('101010', 2))
#Convert octal to int.
print(int('0o120', 8))
#Convet hexadecimal to int.
print(int('0xA', 16))
Output
42
80
10
Example 3:
Here we will ask a number from the user using python input() function and then print the table of that number.
num = int(input("Enter a number to print table:- "))
for i in range(1, 11):
print(f"{num} x {1}:- {i * num}")
Output
Enter a number to print table:- 10
10 x 1:- 10
10 x 1:- 20
10 x 1:- 30
10 x 1:- 40
10 x 1:- 50
10 x 1:- 60
10 x 1:- 70
10 x 1:- 80
10 x 1:- 90
10 x 1:- 100
Conclusion
In this tutorial, you have learned python int() function to convert number to specified value into integer.
If this article help you, please share and keep visiting for further python built-in functions tutorials.
Python built-in functions tutorials
For more information:- Click Here