# Python int() Function

5/5 - (1 vote)

Python’s built-in int(value) function converts the argument value to an integer number. For example, int('42') converts the string value '42' into the integer number 42. The int() function on a float argument rounds down to the closest integer.

Input : int('42')
Output : 42

Input : int('-42')
Output : -42

Input : int(0)
Output : 0

Input : int(3.41)
Output : 3

Input : int(3.51)
Output : 3

Input : int('101', base=2)
Output : 5

Check out my new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

## Python int() Rounding

The int(x) function on a float value x rounds down to the closest integer.

>>> int(3.41)
3
>>> int(3.51)
3
>>> int(3.9999)
3

## Python int() Custom Object

To allow a custom object as an input to the int(object) function, the object must implement the __int__(self) dunder method that returns an integer value. In fact, the int(object) built-in function is semantically equivalent to the object.__int__() function call.

class Car:
def __int__(self):
return -1

porsche = Car()
print(int(porsche))
# -1

In the example, you create a class Car and implement the __init__(self) method that always returns the integer -1. Thus, you can pass a Car object porsche into the int() function and Python doesn’t throw an exception.

Speaking of which…

## Python int() Exception

If you pass an object into the int() function that doesn’t overwrite the __int__() method—for example, a list, tuple, or set—Python throws a TypeError:

class Car:
None

porsche = Car()
print(int(porsche))

This leads to the error message:

Traceback (most recent call last):
File "C:\Users\...\code.py", line 6, in <module>
print(int(porsche))
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Car'

To fix the error, either pass an object that is convertible to an integer or implement your own __int__(self) method as shown previously:

class Car:
def __int__(self):
return -1

porsche = Car()
print(int(porsche))
# -1

Note that the same TypeError appears if you try to convert lists, sets, dictionaries, or tuples to integer values using the int() function.

Lists:

>>> int([1, 2, 3])
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
int([1, 2, 3])
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

Sets:

>>> int({1, 2, 3})
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
int({1, 2, 3})
TypeError: int() argument must be a string, a bytes-like object or a number, not 'set'

Dictionaries:

>>> int({'Alice': 23, 'Bob': 17})
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
int({'Alice': 23, 'Bob': 17})
TypeError: int() argument must be a string, a bytes-like object or a number, not 'dict'

Tuples:

>>> int((1, 2, 3))
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
int((1, 2, 3))
TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'

## Python int() base Argument

The int(value, base=10) function has an optional argument base that allows you to define the base of the numerical system in the value argument. If you set the base, the input must be a string. The base argument determines how the string argument is interpreted. For example, int('10', base=2) converts the string '10' —representing a binary number—to the integer 2.

>>> int('10', base=2)
2

## Summary

Python’s built-in int(value) function converts the argument value to an integer number.

For example, int('42') converts the string value '42' into the integer number 42.

>>> int('42')
42

The int() function on a float argument rounds down to the closest integer.

>>> int(3.99)
3

Want to keep improving your Python skills? Check out our free Python cheat sheets:

## Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!