int()

Python · Variables & Data Types

Theory

int() builds a whole-number int from another value. Given a float it truncates toward zero, simply chopping off the decimal part, so int(4.9) is 4 and int(-4.9) is -4; it never rounds (use round() for that). Given a string, the text must look like a whole number: surrounding spaces are allowed, so int(' 42 ') is 42, but int('4.5') and int('abc') raise ValueError. Booleans convert too: int(True) is 1.

An optional second argument sets the base of the text: int('ff', 16) is 255 and int('101', 2) is 5. The result is always a brand-new int object; the original value is unchanged. A very common use is converting user input or CSV fields, which always arrive as strings, into numbers you can calculate with.

Why we use it

Data from files, forms, APIs and input() arrives as text. You cannot add or compare '25' as a number until you convert it, and int() is the standard, strict way to do that conversion.

When to use it

Use int() for counts, IDs and quantities read as text. For a string like '4.5', convert with float() first and then int() if you really want to truncate. Wrap int() in try/except ValueError when the input might be dirty.

How it works

  1. int(value) is called
  2. Is value a float? Yes: Drop the decimal part (truncate toward zero). No: Go on to the next check.
  3. Is value a string? Yes: Strip spaces and parse the digits in the given base. No: Use its integer meaning (True -> 1).
  4. Was the text a valid whole number? Yes: Return the new int. No: Raise ValueError.
  5. e.g. int('25') returns 25 of type int

Syntax

int(value)

Example

age = int("25")
print(age, type(age))

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic