Python Reference Library →

Python int() Function – Convert to Integer

Let’s look at int() function – convert to integer

  1. Notice

    int() converts supported values to integers. Converting a floating-point number truncates toward zero.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
number = 3.8
print(int(number))
Live state

Top-level scope

number→Object 1
Object 1 · float3.8
This step

int() converts supported values to integers. Converting a floating-point number truncates toward zero.

Picture int() function – convert to integer

int() converts supported values to integers. Converting a floating-point number truncates toward zero.

Image brief

Alt text
A diagram for Python int() Function – Convert to Integer. Instructions: number = 3.8 print(int(number)). Displayed result: 3. The final example bindings are number --> object 1 (float). object 1: 3.8
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python int() Function – Convert to Integer". Teaching focus: int() converts supported values to integers. Converting a floating-point number truncates toward zero. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS number = 3.8 print(int(number)) | v DISPLAYED RESULT 3 FINAL BINDINGS number --> object 1 (float) object 1: 3.8 Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python int() Function – Convert to Integer. Instructions: number = 3.8 print(int(number)). Displayed result: 3. The final example bindings are number --> object 1 (float). object 1: 3.8

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
number = 3.8
print(int(number))
Live state

Top-level scope

number→Object 1
Object 1 · float3.8
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
3

What do you expect this program to print?

What does this example display?

number = 3.8
print(int(number))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

3

Follow the printed values in order. The output is: 3

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

int() converts supported values to integers. Converting a floating-point number truncates toward zero.

int() converts supported values to integers. Converting a floating-point number truncates toward zero.

More examples and reference

Python int() Function

The int() function in Python converts a number or a string into an integer. It's one of the most commonly used functions in Python programming.

Syntax

int(x=0, base=10)

Parameters:

  • x – A number (like float or string) to convert to an integer. Default is 0.
  • base – (Optional) The base of the number (used only if x is a string). Default is 10.

Returns:

  • An integer representation of the input.

Example 1: Convert Float to Integer

print(int(5.9))
5

Note: It truncates the decimal part (does not round).

Example 2: Convert String to Integer

print(int("42"))
42

Example 3: Convert Binary String to Integer

print(int("101", 2))
5

Example 4: Convert Hexadecimal String to Integer

print(int("1A", 16))
26

Use Cases

  • Converting user input (strings) into usable numeric values
  • Truncating float values into whole numbers
  • Converting from binary, octal, or hexadecimal to decimal

Common Mistakes

  • Passing a string with letters in base 10: int("abc") → ValueError
  • Using invalid bases (must be between 2 and 36)
  • Passing None or an empty string: causes TypeError or ValueError

Interview Tip

The int() function is frequently used in string parsing, input handling, and base conversions — all common in coding interviews.

Summary

  • int() converts numbers and strings to integers.
  • If a float is passed, it truncates the decimal part.
  • If a string is passed with a base, it converts from that base.

Practice Problem

Write a program that asks the user to enter a number and prints the number as an integer.

user_input = input("Enter a number: ")
try:
    print("As integer:", int(user_input))
except ValueError:
    print("That's not a valid number!")

What to remember

Connect what you noticed in the example with the idea below.

Remember

int() converts supported values to integers. Converting a floating-point number truncates toward zero.

Watch out for this idea

int() always rounds to the nearest integer.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →