Skip to content

RESOURCES / BLOG

How Does Division Work in Python?

Hi all,

I’m learning Python and something confused me: the way division works. Sometimes I get a decimal, sometimes a whole number. Other times I see weird symbols like // and %. Can someone explain how Python division works, especially the different types and when to use each?

Bonus points if you can tie it into a practical use case like image sizing, resolution, or layout.

Thanks!

Awesome question! Python offers several ways to handle division, and it’s one of those things that can trip people up, especially if you’re coming from a language like JavaScript or C.

Let’s break it down by types of division in Python, then walk through some real-world examples (including some image and layout math you might find useful for web or Cloudinary tasks).

Python provides three primary ways to perform division:

OperatorNameDescriptionExample
/True divisionReturns a float (decimal)5 / 2 = 2.5
//Floor divisionReturns an integer (rounded down)5 // 2 = 2
%ModulusReturns the remainder5 % 2 = 1

This is the most common form. It always returns a float (even if the result is a whole number).

width = 1920

columns = 3

column_width = width / columns

print(column_width)  # 640.0Code language: PHP (php)

Perfect for layouts, responsive grids, or scaling images.

This is integer division; it rounds down to the nearest whole number.

pixels = 1000

tile_width = 300

tiles = pixels // tile_width

print(tiles)  # 3Code language: PHP (php)

Even though 1000 ÷ 300 is 3.33, Python // gives you 3 full tiles, useful when you only want complete units (no leftovers).

This gives you the remainder of a division. Think: what’s left over after using floor division?

remainder = 1000 % 300

print(remainder)  # 100Code language: PHP (php)

So you could say: 1000 pixels = 3 full tiles (of 300px) + 100px leftover. Perfect when handling cropping, overflow layouts, or image padding.

Let’s say you’re making a grid of square thumbnails, and you want to divide an image that’s 1200px wide into equal columns.

image_width = 1200

columns = 4

thumb_width = image_width // columns

print(thumb_width)  # 300Code language: PHP (php)

Want to check if the width divides evenly?

if image_width % columns == 0:

    print("Perfect fit!")

else:

    print("You'll have leftover space.")Code language: PHP (php)

Say you want to maintain aspect ratio when resizing:

original_width = 1600

original_height = 900

new_width = 800

scale = new_width / original_width

new_height = int(original_height * scale)

print(new_height)  # 450Code language: PHP (php)

This is common when resizing images before uploading them to Cloudinary or another image service.

Be aware: division with floats isn’t always exact due to how computers store decimals.

print(0.1 + 0.2)  # 0.30000000000000004

Python’s decimal module or rounding functions can help if you need higher precision:

result = round(0.1 + 0.2, 2)

print(result)  # 0.3Code language: PHP (php)
width = 1000

tile = 300

full_tiles = width // tile  # Floor division

leftover = width % tile     # Modulus

exact_ratio = width / tile  # True division

print(f"Tiles: {full_tiles}, Leftover: {leftover}, Ratio: {exact_ratio}")

# Output: Tiles: 3, Leftover: 100, Ratio: 3.3333333333333335Code language: PHP (php)

This kind of logic is super helpful when coding layout grids, pagination, or chunking large image collections.

SituationUseWhy
You need decimal output/Accurate scaling, aspect ratios
You need whole numbers//Count full units (e.g., tiles, pages)
You need the leftover amount%See what’s not evenly divided
Combining all three/, //, %For full layout logic or image slicing
# True division (always returns float)

5 / 2      # 2.5

# Floor division (returns int, rounded down)

5 // 2     # 2

# Modulus (remainder)

5 % 2      # 1Code language: PHP (php)
def layout_tiles(container_width, tile_width):

    full_tiles = container_width // tile_width

    leftover = container_width % tile_width

    return full_tiles, leftover

tiles, space = layout_tiles(1024, 300)

print(f"Full tiles: {tiles}, Leftover space: {space}")

# Output: Full tiles: 3, Leftover space: 124Code language: PHP (php)

Understanding how Python division works, especially the differences between /, //, and % will help you write better code for image processing, layout calculations, file chunking, and more. It may seem simple, but division is everywhere in programming, and Python gives you clean tools to work with both precision and logic.

Start Using Cloudinary

Sign up for our free plan and start creating stunning visual experiences in minutes.

Sign Up for Free