Assigning and Arithmetic

Assigning Variables

You will usually want to give the variable a value. This is known as assigning a value to the variable and is usually done using the equals operator.  Some examples are shown below ( both in pseudocode and in Python.

Pseudocode

nameinput = “John”

Python

nameinput = "John"

Tutorial Video - Assigning Variables 

01 - Assignment and Variables.mp4

Prompting the User for Input

In programs you will often require the user to enter values for variables. You will often need to specify the type of data being entered.

If you do not specify the type of variable that is being received from the keyboard it will be treated as a string. This can cause issues when trying to perform arithmetical operations as when you are using a string 4 + 5 = 45.

Arithmetic Operators

The standard arithmetic operators are: Add, Subtract, Multiply, Divide and Exponent (Raising to the power). These operators instruct the computer system to perform a calculation with two or more values. An operator is the symbol that will instruct Python which operation to execute.

The maths operators are shown above.

Example Python Code

The Python code above can be executed below:

Tutorial Video - Maths Operations

SDD 2 Arithmetic Operators.mp4

Brackets

More complex calculations can be programmed through use of brackets for example:

variable3 = (variable1 + variable2) / 5

Adds the contents of variable1 and variable2 together then divides the result by 5. The final product is stored in variable3.

We will use the formula to convert fahrenheit to celsius as an example

The formula is:

Celsius = (Fahrenheit - 32) * 5/9

So for example -50℉ = -45.56 ℃

If we look at the line of code that carries out the calculation in the correct example:

celsius = (fahr-32) * 5/9

The use of the brackets make Python subtract the 32 from the variable  fahr BEFORE performing the multiplication and division.