Learn How to Write Python Code with Whole Numbers: Absolute Beginners Tutorial

Whole numbers are the foundation of nearly every Python program you will write. Whether you are counting items in a list, tracking a player's score, or calculating the number of seconds in a day, you are working with integers. This tutorial walks through everything an absolute beginner needs to know about Python's int type, from creating your first integer variable to performing arithmetic and converting between types.

Every programming language has a way of representing whole numbers, but Python handles them in a way that is especially friendly to beginners. You never have to declare a type, you never have to worry about size limits, and the syntax reads almost like plain English. By the end of this tutorial, you will be comfortable creating integer variables, doing math with them, and avoiding the common mistakes that trip up newcomers.

What Is an Integer in Python?

An integer is a whole number with no decimal point. It can be positive, negative, or zero. In Python, integers are represented by the int type, and you create one simply by assigning a whole number to a variable.

python
age = 25
temperature = -7
zero = 0
large_number = 1_000_000

print(type(age))          # <class 'int'>
print(type(temperature))  # <class 'int'>
print(large_number)       # 1000000

Notice that 1_000_000 uses underscores as visual separators. Python ignores these underscores entirely, so the value is identical to 1000000. This is a readability feature introduced in Python 3.6 that makes large numbers much easier to scan at a glance.

Note

Python integers have arbitrary precision. Unlike C or Java where integers are limited to 32 or 64 bits, a Python int can grow as large as your system's memory allows. You can compute 2 ** 1000 and Python will return every single digit of the result without overflow.

Integers vs. Floats: The Decimal Point Matters

The presence of a decimal point is what separates an integer from a float. The number 10 is an int, but 10.0 is a float. This distinction matters because it affects the type of your variable, and mixing types can produce unexpected results if you are not paying attention.

python
whole = 10
decimal = 10.0

print(type(whole))    # <class 'int'>
print(type(decimal))  # <class 'float'>
print(whole == decimal)  # True (same value, different types)

Python considers 10 and 10.0 equal in value, but they are different types under the hood. This matters when you need strict type checking or when the result of an operation depends on the type of the input.

code builder click a token to place it

Build a statement that creates an integer variable called score with a value of 100:

your code will appear here...
100 int = score == 100.0

Arithmetic Operators for Whole Numbers

Python provides a full set of arithmetic operators that work naturally with integers. If you have ever used a calculator, you already know how addition, subtraction, and multiplication work. Python adds a few extras that are especially useful for programming.

Example
5 + 3 returns 8
Returns
int when both operands are int
Example
10 - 4 returns 6
Returns
int when both operands are int
Example
7 * 6 returns 42
Returns
int when both operands are int
Example
10 / 3 returns 3.3333...
Returns
Always returns a float, even if the result is a whole number (10 / 2 returns 5.0)
Example
7 // 2 returns 3
Returns
int when both operands are int. Rounds toward negative infinity.
Example
10 % 3 returns 1
Returns
int when both operands are int. Useful for checking even/odd.
Example
2 ** 10 returns 1024
Returns
int when both operands are non-negative int
Pro Tip

The biggest surprise for beginners is the / operator. In Python 3, 10 / 2 returns 5.0, not 5. True division always produces a float. Use // when you need an integer result.

Here is a practical example that combines several operators to calculate how many full weeks and leftover days fit into a given number of days.

python
total_days = 100

weeks = total_days // 7
leftover = total_days % 7

print(f"{total_days} days = {weeks} weeks and {leftover} days")
# 100 days = 14 weeks and 2 days
spot the bug click the line that contains the bug

This code should print the number of full hours in 150 minutes, but it gives the wrong type. Find the line with the bug.

1 minutes = 150
2
3 hours = minutes / 60
4
5 print(f"Full hours: {hours}")
6 # Output: Full hours: 2.5
The fix: Change / to // on line 3: hours = minutes // 60. The single slash performs true division and always returns a float. Floor division with // gives the whole-number result (2) that represents full hours.

Converting Between Types with int()

Python's built-in int() function converts other data types into integers. This is essential when reading user input (which arrives as a string) or when you need to strip the decimal portion from a float.

python
# Converting a string to an integer
user_input = "42"
number = int(user_input)
print(number + 8)  # 50

# Converting a float to an integer (truncates toward zero)
price = 9.99
whole_price = int(price)
print(whole_price)  # 9

# Negative float truncation
neg = -3.7
print(int(neg))  # -3 (truncates toward zero, not down)
Warning

Passing a non-numeric string to int() raises a ValueError. The call int("hello") will crash your program. Strings containing decimal points also fail: int("3.14") raises a ValueError. Convert to float first, then to int: int(float("3.14")).

Checking a Value's Type

When you are unsure whether a variable holds an integer, use isinstance() or type() to inspect it.

python
value = 42

print(type(value))              # <class 'int'>
print(isinstance(value, int))   # True
print(isinstance(value, float)) # False

The isinstance() function is generally preferred over type() checks because it handles subclasses correctly. In Python, bool is a subclass of int, so isinstance(True, int) returns True. This reflects how Python treats booleans as a special case of integers, where True equals 1 and False equals 0.

How to Use Whole Numbers in Python

Follow these three steps to start writing Python code with integers. Each step builds on the previous one, taking you from variable creation to performing calculations and handling input.

  1. Create an integer variable

    Assign a whole number to a variable name using the equals sign. Python automatically assigns the int type. For example: score = 100. You can verify the type by calling type(score), which returns <class 'int'>.

  2. Perform arithmetic with integers

    Use Python's arithmetic operators to calculate with whole numbers. Addition (+), subtraction (-), multiplication (*), floor division (//), modulo (%), and exponentiation (**) all return integers when used with integer operands. Standard division (/) always returns a float.

  3. Convert other types to integers

    Use the built-in int() function to convert floats or strings to integers. int(3.9) truncates to 3, and int('42') parses the string into the integer 42. Always validate input before converting to avoid ValueError exceptions.

Python Learning Summary Points

  1. Integers in Python are whole numbers with no decimal point, represented by the int type. You create them by assigning a number like 42 or -7 to a variable.
  2. Python integers have arbitrary precision and no fixed size limit. A Python int can grow as large as available memory allows, unlike integers in C or Java.
  3. The / operator always returns a float, even when dividing two integers evenly. Use // for floor division when you need an integer result.
  4. The int() function converts strings and floats into integers. Float-to-int conversion truncates toward zero, and non-numeric strings will raise a ValueError.
  5. Use isinstance(value, int) to check whether a variable is an integer. This approach correctly handles subclasses like bool.

Whole numbers are one of the first building blocks you will use in Python, and they stay relevant at every skill level. From simple counters to complex algorithms, the int type underpins an enormous range of programming tasks. Practice the examples in this tutorial, try the interactive challenges, and experiment in your own Python environment to build confidence with integers before moving on to more advanced data types.

check your understanding question 1 of 5

Frequently Asked Questions

A whole number in Python is represented by the int type. It includes zero, positive numbers like 1, 42, and 1000, and negative numbers like -5 and -100. Unlike many other programming languages, Python integers have no fixed size limit and can grow as large as your computer's memory allows.

Assign a whole number to a variable name using the equals sign. For example: age = 25. Python automatically recognizes 25 as an integer. You do not need to declare a type like you would in C or Java.

An int represents a whole number with no decimal point, such as 10 or -3. A float represents a number with a decimal point, such as 10.0 or -3.5. Division with the / operator always returns a float, even when dividing two integers evenly. Use // for integer (floor) division.

No. Python integers have arbitrary precision, meaning they can grow as large as available memory allows. Languages like C and Java limit integers to 32 or 64 bits, but Python handles the expansion automatically behind the scenes.

Use the built-in int() function. For example: int('42') returns the integer 42. The string must contain only digits (with an optional leading minus sign). Passing a string like '3.14' or 'hello' to int() will raise a ValueError.

The // operator performs floor division, which divides two numbers and rounds the result down to the nearest whole number. For example, 7 // 2 returns 3 (not 3.5). This is useful when you need an integer result from division.

Use the isinstance() function: isinstance(value, int) returns True if the value is an integer. You can also use type(value) == int, but isinstance() is generally preferred because it also works correctly with subclasses of int, including booleans.

Using the / operator on two integers always returns a float in Python 3. For example, 10 / 2 returns 5.0, not 5. If you want an integer result, use the // floor division operator instead: 10 // 2 returns 5.

Yes. Python allows underscores as visual separators in numeric literals. For example, 1_000_000 is the same as 1000000. This makes large numbers easier to read. The underscores are ignored by the interpreter and do not affect the value.

The modulo operator % returns the remainder after division. For example, 10 % 3 returns 1 because 10 divided by 3 is 3 with a remainder of 1. It is commonly used to check if a number is even or odd: if number % 2 == 0, the number is even.