## Table of Contents

**Note: Python snippets were tested and run in Python 2.7. They may require some changes to be compatible with Python 3**

## Arithmetic Operators in Python

Addition, Subtraction, Division, Modulus, Power (number raised to some power)

#### Addition

Python knows that if you ask to add an `int`

to `float`

, it makes sense to answer in `float`

#### Subtraction

#### Multiplication

#### Division

What? That’s wrong!
Nope. Python will always return `int`

if both numerator and denominator are of type `int`

If one of them is in `float`

then it will return answer in `float`

#### Modulus

#### Finding integer raised to the power

### Complex expression, a mixture of multiple operators

Hey! I meant, add 3 to 2 and then multiply it with 4. How do I assign preference of operations? Answer is to surround them by `()`

.

### Order of precedence

`**`

has highest, then`*`

and`/`

(whichever comes first in your expression), then`+`

and`-`

. Key to not get confused by operator precedence is to use`()`

. This also helps in future when you come back to your code after months and have no clue what you did.

We can also perform operations on `str`

types. `+`

will join two strings.

## Logical Operators in Python

Let’s consider two variables `i`

and `j`

to understand the meaning of various comparison operators.

`i > j`

- returns
`True`

if`i`

is strictly greater than`j`

, else returns`False`

- returns
`i >= j`

- returns
`True`

if`i`

is greater than or equal to`j`

, else returns`False`

- returns
`i < j`

- returns
`True`

if`i`

is strictly less than`j`

, else returns`False`

- returns
`i <= j`

- returns
`True`

if`i`

is less than or equal to`j`

, else returns`False`

- returns
`i == j`

- returns
`True`

if`i`

is equal to`j`

, else returns`False`

- returns
`i != j`

- returns
`True`

if`i`

is not equal to`j`

, else returns`False`

- returns

## More logical operators in Python

Assume `i`

and `j`

are `bool`

variables

`i and j`

- returns
`True`

if both`i`

and`j`

are`True`

, else returns`False`

- returns
`i or j`

- returns
`True`

if either of`i`

or`j`

is`True`

, else returns`False`

- returns
`not i`

- returns
`True`

if`i`

is`False`

, returns`False`

if`i`

is`True`

- returns

Next post will discuss yet another widely used data-type in Python: Strings

Note: This is a part of what I learned in an online Open Course Ware offered by MIT on edX. Its for my personal reference & also for those who would like to revisit the course.