HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
Storage Units and Compression
  1. 1Units of data storage
  2. 2Calculating file sizes
  3. 3Compression: lossy, lossless and run-length encoding
  1. Computer Science
  2. /Storage Units and Compression
  3. /Compression: lossy, lossless and run-length encoding

Course 1.3·Lesson 3 of 3

Compression: lossy, lossless and run-length encoding

Compression makes a file smaller, so it needs less storage and less time to send. You learn why we compress, the difference between lossy and lossless compression, and how run-length encoding squeezes repeated data without losing any of it.

about 25 min1 simulation1 practice question
Lesson 3 of 3Show all lessonsHide
Storage Units and Compression
  1. 1Units of data storage
  2. 2Calculating file sizes
  3. 3Compression: lossy, lossless and run-length encoding

Try the code and the simulations beside the lesson.

Why compress?

You send a holiday photo to a friend, and it arrives in a moment. The phone has made the file smaller first. Compression reduces the size of a file. That brings three benefits.

  • Less storage space is needed, so more files fit on a device.
  • Less bandwidth is needed to send the file over a network. Bandwidth is the amount of data that a connection can carry each second.
  • Shorter transmission time: a smaller file is sent more quickly.

There are two kinds of compression.

Lossy compression

Lossy compression reduces the file size by permanently removing data. The removed data can never be brought back. It is chosen so that a person hardly notices the loss.

  • For an image: reduce the resolution, or reduce the colour depth.
  • For a sound: reduce the sample rate, or reduce the sample resolution.
Two versions of the same picture of a circle that is shaded from light in the middle to dark at the edge. On the left it is 16 by 16 pixels and looks smooth. On the right the resolution is cut to 4 by 4 pixels, each a large block, and the circle is hard to see. Labels give 256 pixels and 16 pixels.

The picture shows an image of 16 by 16 pixels, and the same image after its resolution is cut to 4 by 4. The second file is one sixteenth of the size, but the fine detail has gone for good.

Lossy compression can make files very small. It suits photos, music and video, where a small loss is acceptable. It does not suit a text document or a program, where every bit must come back exactly.

Lossless compression

Lossless compression reduces the file size without permanently losing any data. When the file is decompressed, it is exactly the same as the original. It saves less space than lossy compression, but nothing is lost.

The method in this syllabus is run-length encoding (RLE).

Run-length encoding

Data often has runs: the same value many times in a row. A row of sky in a picture may be the same blue for many pixels. RLE stores each run once, as a pair: how many times the value repeats, and the value.

Look at this line of text, 12 characters long.

output
AAAAABBBCCCC

There are three runs: 5 A, 3 B and 4 C. RLE stores it as the pairs (5, A), (3, B), (4, C). If the count and the character take one byte each, that is 6 bytes instead of 12.

One row of 20 pixels: 9 white, then 4 black, then 7 white. Under the row are the three run-length pairs (9, W), (4, B) and (7, W). Labels say 20 bytes before encoding and 6 bytes after.

The picture shows one row of an image with 20 pixels: 9 white, 4 black and 7 white. Stored one byte a pixel, the row takes 20 bytes. With RLE it is three pairs, which is 6 bytes. Decoding writes each value out the number of times its count says, so the row comes back exactly. That is why RLE is lossless.

When RLE does not help. If the data has no runs, every pair has a count of 1. The text ABCD becomes (1, A), (1, B), (1, C), (1, D), which is 8 bytes instead of 4. RLE works best when the data has long runs.

The same idea in pseudocode

This algorithm reads a string and outputs each run as its count followed by its character. SUBSTRING(Text, Position, 1) gives one character, and LENGTH(Text) gives the number of characters. The site cannot run pseudocode, so it is shown in a plain box.

pseudocode
// Run-length encoding of a string
DECLARE Text : STRING
DECLARE Count : INTEGER
DECLARE Position : INTEGER
INPUT Text
Count ← 1
FOR Position ← 2 TO LENGTH(Text)
    IF SUBSTRING(Text, Position, 1) = SUBSTRING(Text, Position - 1, 1)
      THEN
        Count ← Count + 1
      ELSE
        OUTPUT Count, SUBSTRING(Text, Position - 1, 1)
        Count ← 1
    ENDIF
NEXT Position
OUTPUT Count, SUBSTRING(Text, LENGTH(Text), 1)

The Python program below follows the same steps, so you can run it.

Practise with new numbers

Try it in Python

The program encodes a string with run-length encoding, decodes it again, and checks that nothing was lost. It also prints the sizes before and after.

Read it first. Then run it. Is the decoded text the same as the original?

1. Change the text to "ABCDEF". What happens to the size?

2. Change it to forty copies of one letter. How many bytes does the encoded form need?

Common mistakes

  • Saying lossy compression can be undone. The removed data is gone for good.
  • Calling RLE lossy. RLE loses nothing: decoding gives back the exact data.
  • Forgetting the counts. Each run needs a count and a value.
  • Assuming RLE always saves space. With no runs, the file gets bigger.

Remember these

  • Compression reduces file size: less storage, less bandwidth, shorter transmission time.
  • Lossy: permanently removes data (lower resolution or colour depth, lower sample rate or resolution).
  • Lossless: no data is lost. Run-length encoding is an example.
  • RLE stores each run as a count and a value. It works well when there are long runs.

Check yourself

Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.

  1. Give two reasons for compressing a file before it is emailed.
  1. A sound file is compressed by reducing its sample rate. Is this lossy or lossless? Why?
  1. Write the run-length encoding of XXXXXXYYZZZ as count and value pairs.
  1. Decode the pairs (3, P), (1, Q), (4, R).
  1. Why is lossy compression not used for a computer program file?

Enroll free in Storage Units and Compression

Every lesson is open to read. Enrolling also lets you:

  • Mark lessons done and carry on where you stopped.
  • Take the tests of this lesson and its course.
  • Count the course towards the certificate for its chapter.
No payment. You can leave at any time.

Problems (3)

One row of a scanned page is 13 pixels: 6 white, then 3 black, then 4 white…Easy · High School (9–10) · by StemPy Lab
A line of an icon is stored with run-length encoding as these pairs of (count, colour code): (5…Medium · High School (9–10) · by StemPy Lab
A student must compress two files. File A is a text file holding the 7 characters ABCDEFG, one byte…Hard · High School (9–10) · by StemPy Lab
That was the last lessonBack to the course: take the test
← Storage Units and Compression
← Calculating file sizesNext →

Discussion (0)

Log in to join the discussion.

Loading…

1 Python program
5 questions to check yourself

This lesson is free to read. Enroll free to keep your place and take the tests.

Python Labs (1)

Run-length encodingPython Lab · by StemPy Lab

Question Sets (1)

Storage and Compression: Test Yourself10 questions · by StemPy Lab

Notes (1)

Storage Units and Compression: Quick RevisionNote · by StemPy Lab