Posted by Marta on August 6, 2020 Viewed 2264 times
In this tutorial I will share how the int() function works in python, the purpose of the function, different parameter types the function accepts. I will also share example which make the function fails and some use cases where this function is really useful.
The int() function will return an integer constructed from a number or a string. If you pass no arguments, the function will return 0
This function accepts two argument, first the number to convert and second the base of the previous number. If no base is passed, the default base is 10.
number_text="4" number = int(number_text) print(type(number)) print(number)
<class 'int'> 4
If you like to use a custom base, the first parameter should be a string, bytes or byte array type. See the example below:
number_base16 = int('17',16) print(number_base16) number_base3 = int('3',4) print(number_base3)
Otherwise, if you passed a float as first parameter and try to convert it to an integer with an specific base for instance base 16, you will get the following error:
number_base16 = int(17.5,16) print(number_base16)
Traceback (most recent call last): File line 1, in <module> number_base16 = int(17.5,16) TypeError: int() can't convert non-string with explicit base
number_text="1234" # number saved in text number = int(number_text) print(type(number)) print(number)
<class 'int'> 1234
float_var = 4.3 int_num = int(float_var) print(int_num)
Keep in mind the int function doesn’t do any rounding, it will just remove the decimal part.
Since Python 3 there is no limit to the value of integers( Check out the Documentation) . However there is the constant
sys.maxsize, value 9223372036854775807 (9,223,372,036,854,775,807) which can be used as an integer, larger than any practical list or string index. That means that
sys.maxsize is the maximum a collection index can handle, therefore the maximum number of items the collection can hold.a list See the example below:
import sys print(sys.maxsize)
So far we have seen how to use int function and the different parameters the function accepts. Next I will share some exercises where the int function is necessary.
First exercise is reading a number entered by the user, and return the absolute number. See the code below:
num = input('Enter a number:') abs_value = abs(int(num)) print('Absolute Value: '+ str(abs_value))
In line 1 will read the number entered by the user. This number is a string. Next line will convert the string to an absolute value. Since the
abs() function only accepts numbers, I need to use the
int() function and convert the
num variable to number.
Another exercise exercise is reading two numbers from the user and return the result of adding the two numbers. The code below illustrates how to achieve that:
num1 = input('Enter a number:') num1 = int(num1) num2 = input('Enter a number:') num2 = int(num2) print('Result: ' + str(num1 + num2))
In this example above the
int() function is used to convert a string to an integer in line 2 and line 4. The
num2 variables need to be converted to integers before the addition can be calculated.
The int function will round the number to the whole part toward zero. It will leave the whole part and truncate the decimal part
float1_var = 4.6 print(int(float1_var))
What if you pass to the function something that is not convertible to a number, for instance a character. The function will return the ValueError: invalid literal for int() with base 10, which indicates the parameter passed to the function is not a base 10 number. See an example below:
number1 = int('q')
Traceback (most recent call last): File line 1, in <module> number1 = int('q') ValueError: invalid literal for int() with base 10: 'q'
To summarise, this article covers how to use the int() function, a few examples of how to use it and frequent errors to avoid. I hope this tutorial was useful and thank you for reading and supporting the this blog.
How to Fix Typeerror: ‘int’ object is not subscriptable
Learn How to convert XML to JSON in Python – Ultimate guide
How to Create Tkinter Progress Bar and DropDown
How to Fix Typeerror a bytes-like object is required not ‘str’
Steady pace book with lots of worked examples. Starting with the basics, and moving to projects, data visualisation, and web applications
Unique lay-out and teaching programming style helping new concepts stick in your memory
Great guide for those who want to improve their skills when writing python code. Easy to understand. Many practical examples
Perfect Boook for anyone who has an alright knowledge of Java and wants to take it to the next level.
Excellent read for anyone who already know how to program and want to learn Best Practices
Perfect book for anyone transitioning into the mid/mid-senior developer level
Great book and probably the best way to practice for interview. Some really good information on how to perform an interview. Code Example in Java