Skip to content

Int function in Python

Int function in Python

Int function in Python converts a number or string to an integer , or return 0 if no arguments are given .

Syntax of Int function in Python

int(value , base)

Parameters of Int function in Python

  • value = Optional , A number or string that can be converted to an integer number . Default value is 0 .
  • base = Optional , A value representing the number format . Default value is 10 .

Return type of Int function in Python

It returns a integer number .

Int function in Python Examples

Example 1: Int function without any argument in Python

x =int()
print(x)
0

In the above code as we did not give anything to int function so , Python treated int() as int(0,10) and printed 0 in the output screen .

Example 2: Convert float number to integer number in Python

# float to int
x=8.5
print(int(x))
8

In the above code we have given a float number 8.5 to the int function . then int function has trimmed the float part from the number and printed only integer part 8 in the output .

Note :

We can not convert complex number to integer number .

Example : Convert complex number to integer number in Python
# complex to int
x=5.3+8j
print(int(x))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
C:\Users\RAJKUM~1\AppData\Local\Temp/ipykernel_10416/3398177152.py in <module>
      1 # complex to int
      2 x=5.3+8j
----> 3 print(int(x))

TypeError: can't convert complex to int

Example 3: Convert convertable text to integer number in Python

# convertable text to int
x ="105"
print(int(x))
105
  • In the above code we have tried to convert a text into a integer
  • As "105" can be converted into a integer 105 so int function has converted it to 105 and printed it .

Example 4: Convert unconvertable text to integer number in Python

# unconvertable text to int
x ="Rajkumar"
print(int(x))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
C:\Users\RAJKUM~1\AppData\Local\Temp/ipykernel_10416/2862408046.py in <module>
      1 # convertable text to int
      2 x ="Rajkumar"
----> 3 print(int(x))

ValueError: invalid literal for int() with base 10: 'Rajkumar'
  • In the above code we have tried to convert a text into a integer
  • As "Rajkumar" can not be converted into a integer so int function has given a error .

Example 5: Using base parameter of int function with non-string value

To use base parameter in int function we need to provide string in value parameter otherwise it will give error .

int(10,10)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
C:\Users\RAJKUM~1\AppData\Local\Temp/ipykernel_10416/2384769210.py in <module>
----> 1 int(10,10)

TypeError: int() can't convert non-string with explicit base

In the above code as we have used base parameter in int function with value as an integer so it has raised an error.

How to convert other number system to Decimal number ?

We can convert any number from other number system to decimal number using int function just changing the base parameter .

Binary number to Decimal number

# binary to decimal
x= "101"
print("Decimal of binary no",x,"is",int(x,2))
Decimal of binary no 101 is 5

In the above code we have converted a Binary number to a Decimal number just using base parameter as 2 .

Octal number to Decimal number

# octal to decimal
x= "175"
print("Decimal of octal no",x,"is",int(x,8))
Decimal of octal no 175 is 125

In the above code we have converted a Octal number to a Decimal number just using base parameter as 8 .

Hexa-decimal number to Decimal number

# hexa-decimal to decimal
x= "1f2"
print("Decimal of hexa-decimal no",x,"is",int(x,16))
Decimal of hexa-decimal no 1f2 is 498

In the above code we have converted a Hexa-decimal number to a Decimal number just using base parameter as 16 .

How to take Integer input from a user ?

We already know that to take input from a user we have input function . But the problem with input function is that input function always take input in string format . So , to take integer input from user we will use int function .

To know about input() function click here .

Example 1:

# taking input from user
myInput=input("Enter a number :")
print("type of myInput is",type(myInput))

# taking integer input from user
myIntegerInput=int(input("Enter a number :"))
print("type of myIntegerInput is",type(myIntegerInput))
Enter a number :10
type of myInput is <class 'str'>
Enter a number :10
type of myIntegerInput is <class 'int'>
  • In the above code first we have taken a input from user without int function . After taking input when we have checked the type of input it was 'str' .
  • After that we have taken a input from user using int function . After taking 2nd input when we have checked the type of input it was 'int' .

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

Leave a Reply