Introduction to Python

Algorithm Basics

Lesson 3: Algorithms

What is an Algorithm?

Before writing any code, a good programmer thinks about the steps needed to solve a problem. This ordered set of steps is called an algorithm.

An algorithm is a step-by-step set of instructions designed to solve a problem or complete a task. Every program is essentially an algorithm written in a programming language.
Illustration showing a problem on the left, an arrow pointing to a numbered list of steps in the middle labeled algorithm, and the solution on the right

Properties of a Good Algorithm

  • Input — it takes zero or more inputs
  • Output — it produces at least one result
  • Definiteness — every step is clear and unambiguous
  • Finiteness — it terminates after a finite number of steps
  • Effectiveness — every step is simple enough to be carried out

A Real-Life Algorithm Example

Making a cup of tea is an algorithm! Here's how it looks as a step-by-step process:

Making Tea — Step by Step

A kettle with steam coming out representing boiling water
1. Boil Water
A tea bag being placed into an empty cup
2. Add Tea Bag
Hot water being poured from a kettle into a cup with a tea bag
3. Pour Water
A finished cup of tea with steam on a saucer ready to be served
4. Serve

Algorithms in Programming

In programming, algorithms are expressed as code. Before writing code, it is always a good idea to plan your algorithm in plain language or as a flowchart. This makes coding much easier and helps avoid mistakes.

Algorithm vs Code

Algorithm StepPython Code
Get the user's namename = input("Enter your name: ")
Display a greetingprint("Hello, " + name)
Ask for the user's ageage = int(input("Enter your age: "))
Check if old enough to voteif age >= 18:
Display result print("You can vote!")
A well-planned algorithm makes writing code much faster and less error-prone. Always plan before you code!

Subscribe to our newsletter.

Get updates to news and events.