# [PyTUT 2] NUMBERS AND OPERATORS

Have you installed Python on your computer?

Following to the previous tutorial, today I would like to introduce you the numbers and operators used in Python. Before beginning this tutorial, I want to notice again that all codes in the series of ‘Basic Python tutorials’ are written in Python 2.7.11. Thus, may be you have to change a little bit with your code if you use another version, especially  version 3.

Let’s take a look at the numbers and operators used in Python.

## Numbers and operators

Python treats the numbers in two main types: integer and float. The others are long and complex. This tutorial focuses on the first two number types. However, you can do the same tasks with the long and complex.

Just like the other programming language, integer and float numbers can be used with the operators such as add ‘+’, subtract ‘-‘, multiply ‘*’ and divide ‘/’. However, there are some different between these two types of numbers (you can see it soon).

Now, let’s open IDLE (or your text editor, or even Command Prompt) and do basic math with Python:

#### Example 1: The operators with two integers

```>>> 9+4
13```
```>>> 9-4
5```
```>>> 9*4
36```
```>>> 9/4
2```

#### Example 2: The operators with two floats

```>>> 9.0+4.0
13.0```
```>>> 9.0-4.0
5.0```
```>>> 9.0*4.0
36.0```
```>>> 9.0/4.0
2.25```

After the examples, we can see that the significant different is showed when you used the division operator ‘/’. When two integer are divided, the result is always an integer (see Ex. 1). If you want to determine the remainder of 9/4, you will need the remainder operator ‘%’ (this operator is useful in some cases that I will mention it later in another tutorial).

```>>> 9%4
1```

Note: If you use the operators with an integer and a float, Python will understand that you want to use the operators with two floats. For example:

```>>> 9/2.0
4.5```

## Number type conversion

Sometimes, you expect to change the type of number to satisfy requirements of an operator or an function parameter. Here are some ways:

To convert a number x to integer, we use the following structure:

`>>> int(x)`

#### Example 3: Convert a complex number 4.0 to integer

```>>> int(4.0)
4```

Similar to this, Python supports the structures that help you to convert a number to

float type

`>>> float(x)`

long type

`>>> long(x)`

complex type ( where x is real part and y is imaginary part)

`>>> complex(x)`
`>>> complex(x, y)`

Note: You cannot convert complex to int

That is all about basic math with Python. It is simple, isn’t it?

In the next tutorial, I will talk about string in Python and some ways you can do to treat strings.

Hope you enjoy it,

Curious Chick 