Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python dict() Function
Next article icon

The Python int() function converts a given object to an integer or converts a decimal (floating-point) number to its integer part by truncating the fractional part.

Example:

In this example, we passed a string as an argument to the int() function and printed it.

Python
age="21"print("age =",int(age))

Output:

age = 21

Python int() Function Syntax

The syntax of the int() function inPython is as follows:

Syntax: int(x, base)

  • x [optional]: string representation of integer value, defaults to 0, if no value provided.
  • base [optional]: (integer value) base of the number.

Returns: Return decimal (base-10) representation of x

Python int() Function Examples

Let us see a few examples of int() in Python.

Python int() with different DataTypes

In this example, we passed a string, a float, and an integer value as a parameter to the int() function in Python to see how the input is affected by applying the int() function.

Python3
# int() on string representation of numbersprint("int('9')) =",int('9'))# int() on float valuesprint("int(9.9) =",int(9.9))# int() on Python integerprint("int(9) =",int(9))

Output : 

int('9')) = 9int(9.9) = 9int(9) = 9

Convert base using int() in Python

In this example, we used the int() function to convert the base of a number from Binary, Octal, and Hexadecimal to a Decimal integer.

Python3
# octal to decimal using int()print("int() on 0o12 =",int('0o12',8))# binary to decimal using int()print("int() on 0b110 =",int('0b110',2))# hexa-decimal to decimal using int()print("int() on 0x1A =",int('0x1A',16))

Output : 

int() on 0o12 = 10int() on 0b110 = 6int() on 0x1A = 26

Exception of int() in Python

TypeError: raises TypeError when any object that does not have __int__() or __index__()Python magic methods implemented.

ValueError: raises ValueError when any object cannot be converted to an integer.

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

In this example, we are trying to convert a binary number to a decimal number using the Python int() function. But this code will raise an error as the binary number passed is not in a single quote.

Python3
print(int(0b101,2))

Output : 

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

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

This example will generate a value error as we are passing aPython String to the int() function.

Python
print(int('geeks'))

Output:

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

Python int() Function for Custom Objects

The int() in Python can be used on custom objects. Let us see a few examples of the same.

int() with __int__() function

In this example, we created a class called 'Number' and defined a value and a constructor that returns the value. Then we created an object called 'data' of the Number class which invokes the __init__() method. The data object stores the value that is returned by that method and then using the int() function we printed the value.

Python
classNumber:value=7def__int__(self):returnself.valuedata=Number()print("number =",int(data))

Output:

number = 7

int() with __index__() function

In this example, we created a class called 'Number' and defined a value and a function that returns the value. Then we created an object called 'data' of the Number class which invokes the __index__() method. The data object stores the value that is returned by that method and then using the int() function we printed the value.

Python
classNumber:value=7def__index__(self):returnself.valuedata=Number()print("number =",int(data))

Output:

number = 7

Application

The int() function in Python is used in all thestandard conversions. For example, the conversion of binary to decimal, octal to decimal, and hexadecimal to decimal.


Improve

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp