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.
Headings of Contents
Python int Function Introduction
Python int() function in Python is a built-in function which means we don’t need to install it by using the pip command or no need to import from anywhere, it by default comes with Python. To use the int() function, we need to call it by using the name means int(), and provides parameters.
Syntax
The syntax of Python int() function is:-
int(value, base)
int() Parameter
int function in python take two parameters.
- value:- A number or string to be converted into an integer number.
- base:- A number representing the number format, Default value: 10.
Return Value
The return value of the int function in python is converted integer number.
Python int() Examples
Here I am going to explore the int() function along with multiple examples. so that you can get more awareness about Python int.
Example: Converting string int to integer
print(int('120'))
The Output will be: 120
Example: Converting float to int
value = 150.40
print("Converting float to int")
print(int(value))
Output
Converting float to int
150
Example: Converting binary, Octal, and Hexadecimal to int
In this example, I am converting Binary, Octal, and Hexadecimal to an integer value by using the int() function.
#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: Asking for value from the user and printing table
Here we will ask for a number from the user using the 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 numbers to a specified value into an integer. It can be more useful especially when you are working on any real projects.
If this article helps you, please share and keep visiting for further python built-in functions tutorials.
Python built-in functions tutorials
For more information:- Click Here
Thanks for your valuable time… ❤️❤️🙏🙏