Table of Contents
The int() method returns an integer object from any number or string.
The syntax of int() method is:
int(x=0, base=10)
1. int() Parameters
int() method takes two arguments:
- x – Number or string to be converted to integer object.
The default argument iszero. - base – Base of the number in x.
Can be 0 (code literal) or 2-36.
2. Return value from int()
int() method returns:
- an integer object from the given number or string treats default base as 10
- (No parameters) returns 0
- (If base given) treats the string in the given base (0, 2, 8, 10, 16)
3. Example 1: How int() works in Python?
# integer
print("int(123) is:", int(123))
# float
print("int(123.23) is:", int(123.23))
# string
print("int('123') is:", int('123'))
Output
int(123) is: 123
int(123.23) is: 123
int('123') is: 123
4. Example 2: How int() works for decimal, octal and hexadecimal?
# binary 0b or 0B
print("For 1010, int is:", int('1010', 2))
print("For 0b1010, int is:", int('0b1010', 2))
# octal 0o or 0O
print("For 12, int is:", int('12', 8))
print("For 0o12, int is:", int('0o12', 8))
# hexadecimal
print("For A, int is:", int('A', 16))
print("For 0xA, int is:", int('0xA', 16))
Output
For 1010, int is: 10 For 0b1010, int is: 10 For 12, int is: 10 For 0o12, int is: 10 For A, int is: 10 For 0xA, int is: 10
5. Example 3: int() for custom objects
Internally, int() method calls an object’s __int__() method.
So, even if an object isn’t a number, you can convert the object into an integer object.
You can do this by overriding __index__() and __int__() methods of the class to return a number.
These two methods should return the same value as older versions of Python uses __int__(), while newer uses __index__() method.
class Person:
age = 23
def __index__(self):
return self.age
def __int__(self):
return self.age
person = Person()
print('int(person) is:', int(person))
Output
int(person) is: 23
Related posts:
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Dictionary keys()
Python Program to Delete an Element From a Dictionary
Python Program to Merge Mails
Deep Learning in Python - LazyProgrammer
Python String lower()
Python Program to Compute the Power of a Number
Python Program to Solve Quadratic Equation
Python Program to Display Powers of 2 Using Anonymous Function
Python Closures
Python Program to Print all Prime Numbers in an Interval
Python Program to Iterate Over Dictionaries Using for Loop
Python Program to Differentiate Between type() and isinstance()
Python input()
Python memoryview()
Python String isalpha()
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python Program to Find HCF or GCD
Python String translate()
Python String strip()
Python Program to Get the File Name From the File Path
Python Program to Convert Bytes to a String
Python String isupper()
Python Program to Display the multiplication Table
Python Global Keyword
Python Program to Capitalize the First Character of a String
Python bytearray()
Python Anonymous / Lambda Function
Python Program to Check Armstrong Number
Python Dictionary update()
Python Program to Access Index of a List Using for Loop
Python Program to Check Leap Year