# Arithmetic Operators and Built-in Functions¶

## Using Python as a calculator¶

The following are examples of the kinds of Python expressions that we can use.

In :
4 + 5

Out:
9
In :
15 - 12.5

Out:
2.5
In :
3.4 * 6

Out:
20.4

Each Python value has a type. 4, 5, 15, and 6 have type int. 12.5 and 3.4 have type float. Float stands for floating point number. Floats are approximations to the real numbers.

Python has two different division operators: / produces a float and // is integer division (it produces an int).

In :
8 / 4

Out:
2.0
In :
8 // 4

Out:
2
In :
# When writing programs, we can put in English language explanations called comments.
# Whatever comes after the # symbol is not executed.

In :
2 ** 5  # 2 to the power of 5

Out:
32
In :
# The % operator gives the remainder of a division.
# 10 % 3 gives the remainder of 10 // 3
10 % 3

Out:
1

In other words, the fraction 10 / 3 can be rewritten as 3 1/3, so the remainder is 1.

Operator order of precedence (from highest to lowest)

**

- (negation)

*, /, //, % (left to right)

+ (addition), - (subtraction) (left to right)

## Encountering Errors¶

When programming, it is common to encounter errors. You will learn how to interpret the error messages, so that you can fix your code. Here are a few examples:

In :
3 +

  File "<ipython-input-9-4715fb1d5390>", line 1
3 +
^
SyntaxError: invalid syntax

In :
4 + 5 ) * 2

  File "<ipython-input-10-9753380d8def>", line 1
4 + 5 ) * 2
^
SyntaxError: invalid syntax

In :
2 * * 5

  File "<ipython-input-11-b1206b14b483>", line 1
2 * * 5
^
SyntaxError: invalid syntax

In :
9 / 0

---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-12-263dd5d0c9f7> in <module>()
----> 1 9 / 0

ZeroDivisionError: division by zero

## Practice Exercise: Writing expressions¶

1. A patient just turned 80 years old. Write a Python expression to calculate her age in months.
2. A father reports that his son's temperature was 40.1 degrees Celsius when measured at home. Write an expression to covert the temperature from Celsius to Fahreneheit. (Formula: Multiply by 9, divide by 5, and then add 32.)

## Built-in Functions¶

In addition to the operators shown above, the Python language comes with a set of functions that we can use.

In :
min(45, 23)

Out:
23
In :
max(5.5, 5.52)

Out:
5.52
In :
abs(-10)

Out:
10
In :
abs(-4 - 8)

Out:
12
In :
type(4)

Out:
int
In :
type(8.25)

Out:
float

## Practice Exercise: Calling on round¶

Python has a built-in function named round. In the Python shell, run each function call below and record the result:

1. round(24.2)
2. round(24.5)
3. round(24.9)
4. round(1.234567, 2)
5. round(1.234567, 3)