# Foundations of Python Programming: Functions First

## Section1.2Algorithms

If problem solving is a central part of computer science, then the solutions that you create through the problem solving process are also important. In computer science, we refer to these solutions as algorithms. An algorithm is a step by step list of instructions that if followed exactly will solve the problem under consideration.
For example, an algorithm to compute the length of a triangle’s hypotenuse might look like this:

### Note1.2.1.Algorithm Example 1 (English).

• Ask for the lengths of the two sides
• Use Pythagorean Theorem to find hypotenuse
• Display the hypotenuse
Notice that this algorithm consists of a set of steps. It is written in English, for ease of understanding but that it assumes the reader knows the required sub-steps. Although simple algorithms are easily understood when written in English, more complicated algorithms need more precise notation. For improved precision, algorithms are often written in pseudocode. Pseudocode is a notation that is more precise than English but generally not as precise as a programming language. The same algorithm expressed in pseudocode might look something like this:

### Note1.2.2.Algorithm Example 2 (Pseudocode).

1. Ask for length of non-hypotenuse sides:
1. Ask for first side’s length. Call this side $$a$$
2. Ask for second side’s length. Call this side $$b$$
2. Let
\begin{equation*} hypotenuse = \sqrt{a^2 + b^2} \end{equation*}
3. Display hypotenuse length
Notice how the pseudocode example is more structured - using numbered steps and a hierarchy of sub-steps as needed; and how it uses variables and a formula to express the mathematics more precisely.
Our goal in computer science is to take a problem and develop an algorithm that can serve as a general solution: a solution that can work in (most) circumstances. Once we have such a solution, we can use our computer to automate its execution. Programming is a skill that allows a computer scientist to take an algorithm and represent it in a notation (a program) that can be followed by a computer. A program is written in a programming language such as Python, the language you will learn in this book.
To help you understand the differences amongst an algorithm, psuedocode and a program, consider this program that finds the hypothenuse of a right-angled triangle:
A program is an algorithm expressed in a programming language. We might also say that a program is an implementation of an algorithm. In this example, both the algorithm and the program have three major steps. The first step gets some input from the user and the input into something the computer can do math with; the second step performs a calculation using the information obtained in the first step; and the final step displays the result to the user. Even though we haven’t covered any details of Python, hopefully you can see the correspondence between the steps of the algorithm, which could be followed by a human (but not executed by a computer), and the steps of the program, which can be executed by a computer (try executing this one using the Run button).
Algorithms are important because the process of solving a problem through programming begins with designing an algorithm. The programmer often expresses the algorithm in pseudocode to better understand the sub-steps, then converts the algorithm to a program for the computer to execute. In the next section, you will learn how to execute Python programs on a computer.