## Python 3b - Simple Calculations

## Simple Calculations in Python

You can perform calculations on numbers in Python using the four main operators:

print(89 + 47)

print(89 - 47)

print(89 * 47)

print(89 / 47)

=

136

42

4183

1.8936170212765957

For addition, use the plus sign +

â€‹

To subtract numbers, use the dash symbol – (but not an underscore _ )

â€‹

For multiplication, use an asterisk * which can be made by pressing Shift and 8 on a typical keyboard.

â€‹

To divide numbers, use a forward slash / (but not a backslash \ )

Use a string and the calculation to make the output user friendly.

print("53 x 7 =" , 53 * 7)

=

53 x 7 = 371

Simple Calculations Task 1 (+ - * /)

Print four different simple calculations, using a different operator ( + - * / ) for each.

â€‹

Make the output user friendly by also showing the calculation (not just the answer).

Copy the divide symbol here using Ctrl and C: ÷

Example solution:

18 + 35 = 53

18 - 35 = -17

18 x 35 = 630

18 ÷ 35 = 0.5142857142857142

## Using Variables in Calculations

You can also perform calculations on variables. The example below has the values of the variables pre-written.

â€‹

You need to store the result in a variable. The total variable has been used to store the result of the multiplication.

num1 = 12

num2 = 20

total = num1 * num2

print("The total is" , total)

=

The total is 240

The example below allows the user to input values.

num1 = int(input("Enter number one: "))

num2 = int(input("Enter number two: "))

total = num1 + num2

print("The total is" , total)

Enter number one: 21

Enter number two: 82

The total is 103

=

Don't leave the user in the dark, better user interfaces are clear and explain what outputted values mean:

num1 = int(input("Enter number one: "))

num2 = int(input("Enter number two: "))

answer = num1 - num2

print(num1 , "-" , num2 , "=" , answer)

Enter number one: 83

Enter number two: 29

83 - 29 = 54

=

Simple Calculations Task 2 (Divide by 3)

Use an input line with int to ask the user to enter a number.

â€‹

Divide the number by 3 and output the result.

Example solution:

Enter a number: 11

11 divided by 3 is 3.6666666666666665

Simple Calculations Task 3 (Add 3 Numbers)

Make three input lines using int to ask the user to enter three numbers.

â€‹

Add the numbers together and output the total.

Example solution:

Enter the first number: 45

Enter the second number: 32

Enter the third number: 19

The total is 96