BlogsDope image BlogsDope

int() in Python

March 16, 2021 PYTHON FUNCTION 752

Python deals with various data-types. Every value in Python has a data-type. Some of the basic and important data-types are: int, float, str, bool, etc.

Let's start with a simple program to accept two numbers as input from the user and multiply them:

x = input("Enter first number:")
y = input("Enter second number:")
print(x * y)

Enter first number:5 

Enter second number:6 

TypeError: can't multiply sequence by non-int of type 'str'

What is this error? We have given both the inputs of integer type, haven't we? Oh, so here lies the problem. Python interprets all the user inputs as string types. So, if we try to multiply the two inputs, we get a TypeError indicating that the inputs taken are of str type that is non-int type. To avoid such a situation, the function int() can help us. Let's see how:

What is int() in Python?

 int() is a built-in function in Python that is used to return an integer type object. It converts a given value into an integer that is a number. It takes two arguments. Let's see the syntax of it:

Syntax of int()

int(number, base)
Argument
Description
Required/Optional
number
The number/string value that has to be converted into an integer type. Default is 0
Optional
base
This argument is to define the base of the numerical system of the first argument - Decimal, Binary, Hexadecimal, etc. Default is base-10(Decimal)
Optional

Return Type of int()

print(type(int()))

<class 'int'>

Examples of int()

a = int()
print(a)

b = "59"
print(type(b))
b1 = int(b)
print(type(b1))
print(b)

0

<class 'str'> 

<class 'int'> 

59

We can see in the first example, that with no arguments, the default value is 0. In the second example, the string value "59" is converted to an integer type value with int().

Now, let's take the first example where we started:

x = input("Enter first number:")
x1 = int(x)
y = input("Enter second number:")
y1 = int(y)
print(x1 * y1)

Enter first number:5 

Enter second number:6 

30

So, now we did not get any error as we converted the input value into integer type with int() which was initially a string type. You can check the data-type of all the four variables used in the example with the help of type() to understand better.

Let's take some examples with different number formats:

x = int("0o33", 8)     # 033 is octal format of decimal value 27
print(x)

y = int("0xff", 16)   # 0xff is hexadecimal format of deciaml value 255
print(y)

z = int("0b1010", base=2)   # 0b1010 is binary format of deciamal value 10 
print(z)

27 

255 

10

Note that the second argument can be written as base=16 or just 16. Both work fine. Also, 0o, 0x,0b are prefixes in Python for Octal, Hexadecimal, and Binary formats respectively. If we do not mention the base, we will end up with a. The int() function converts the specified value into an integer number ValueError.

Let's see what is the output for float type values:

x = int(18.59)
print(x)

y = int(49.5)
print(y)

​18 

49

So, we can see that the digits after the decimal point get truncated and the value just before the decimal point is printed without any rounding off.

Can we convert a string of letters into integer type using int(). I think we may land up with an error. Let's see:

x = int("CodesDope")
print(x)

ValueError: invalid literal for int() with base 10: 'CodesDope'

So, as predicted we got a ValueError as it is not a valid input.


Liked the post?
Rarely seen, always noticed.
Editor's Picks
0 COMMENT

Please login to view or add comment(s).