Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 02

int() — Convert to Integer

Learn which data types can be converted with int(), why some fail, and the difference between int(12.34) and int("12.34").

Beginner20 min

int() converts a possible value into an integer (int).

In this lesson you'll learn which data types can be converted, why some fail, and the difference between int(12.34) and int("12.34").

int() truncates the float 12.34 to the integer 12

What you'll learn

  • The purpose of int()
  • Convert float, bool, and integer-looking strings
  • Understand TypeError vs ValueError
  • Know why identical digits can produce different results

What does int() do?

int() creates a new integer object from another value, but only if the conversion is valid.

python
number = int(value)

The original value is never modified.

Example 1 — Float → Integer (Possible)

The decimal part is truncated toward zero. It is not rounded.

python
a = 12.34
print(a, type(a))

b = int(a)
print(b, type(b))

print(int(0.345))
text
12.34 <class 'float'>
12 <class 'int'>
0

Key concept: int(12.99) becomes 12, not 13. Python simply removes the fractional part.

Example 2 — Boolean → Integer (Possible)

Python treats booleans as numeric values internally.

Boolean int() result
True 1
False 0
python
print(int(True))    # 1
print(int(False))   # 0

Example 3 — Complex → Integer (Not Possible)

Complex numbers contain a real and imaginary part, so int() rejects them.

python
a = 2 + 3j

# int(a)
text
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'complex'

This is a TypeError because the type itself is unsupported.

Example 4 — String → Integer

A string works only if its contents represent a valid base-10 integer.

Valid integer strings

python
print(int("12"))
print(int("123"))
print(int("-45"))
text
12
123
-45

Invalid string examples

Input Why it fails
"12.34" Decimal point → ValueError
"True" Text, not boolean → ValueError
"2+3j" Complex literal text → ValueError
"PYTHON" Not a number → ValueError

Interactive playground

Select a source type and see how int() behaves.

Choose the source type

Python simulation

int(12.34)

Conversion successful

12 int

int conversion playground

The most important difference

These two values look similar, but one succeeds and the other fails.

Call Result Input type
Works int(12.34) 12 float
Fails int("12.34") ValueError string

Why?

  • 12.34 is already a float, so Python truncates it.
  • "12.34" is text, and it is not a valid integer literal.

Complete conversion map

Source Example Result
float int(12.34) 12
bool int(True) 1
bool int(False) 0
complex int(2+3j) TypeError
str int("12") 12
str int("12.34") ValueError
str int("True") ValueError
str int("2+3j") ValueError
str int("PYTHON") ValueError

Practice exercises

Try predicting the output before running the code.

Exercise 1 — What will these return?

python
int(0.345)
int(-3.9)

Answer: 0 and -3

Exercise 2 — Which one succeeds?

python
int("12")
int("12.34")

Answer: Only int("12") works.

Exercise 3 — Compare these:

python
int(True)
int("True")

Answer: The first returns 1. The second raises ValueError.

Quick knowledge check

  1. What does int(12.34) return?
    12 — the fraction is removed, not rounded.

  2. Why does int("12.34") fail?
    The string is not a valid base-10 integer literal.

  3. Why does int(2+3j) fail?
    It raises TypeError because complex is not a supported real-number type for int().

Summary

  • int() converts a possible value into an integer.
  • float and bool convert successfully.
  • complex raises TypeError.
  • Only integer-looking strings convert.
  • Golden rule: int(12.34) works. int("12.34") is a ValueError.

Loading playground…