# Operators

Python has the following major operators:

`<, >, =, ==, >=, <=, !=, *, +, -, **, +=, -=, /=, *=, in, and, not, is.`

# Operator priority

When expressions containing more than one operators are evaluated, the operator priority is followed, it is just like the BODMAS (PEMDAS for Americans) rule of maths. Usage of `()` can override priority.

`>>> 3 * 2 + 20 - 46-20>>> 3 * (2 + 20 - 46)-72`

# Assignment

`=` is the assignment operator.

`>>> a = 1 # creates a new integer object of value 1      # and stores the address in variable a.>>> a = 2 # creates a new integer object of value 2      # and stores the address in variable a.>>> b = a # stores the address of variable a inside       # variable b, they point to the same object.`

# Multiply

`>>> a = 12>>> a * 12144>>> a = "ab_">>> a * 2 # when * is used with strings,          # it returns a new string twice.ab_ab_`

# Exercise:

1. Take two numbers from the user and print their multiplication.

2. Take three numbers from the user and print their multiplication.

`>>> a = 1>>> a + 12>>> a = 'py'>>> a + 'thon' # concatenates 'thon' to 'py' and creates a new string.python>>> a # we did not reassign a, so it's value is unchanged.py`

# Exercise:

1. Take two strings from a variable and print their concatenation.

2. Take two numbers from the user and add them (you need to use the int() to convert the input to integer)

3. Take three numbers from the user and print their addition.

# Equality

`==` is the equality operator, it returns true if both operands have the same value.

`>>> a = 1 # creates variable a with value 1.>>> b = 1 # creates variable a with value 1>>> a == b # checks if a and b are equal.True`

Note: It is a classic mistake to use `==` when you really want to use `=` or vice versa.

# Division

`27/7` divides 27 by 7 and returns a floating point result

`27//7` divides 27 by 7 and returns an integer result.

`>>> 27/7 3.85714>>> 27//73>>> 27%7 1`

# Power

`**` is the operator for calculating power.

`>>> a = 2>>> a**38`

# Shortcut operators

Consider that you have to create a variable `a = 3`. If you want to add `4` to the variable `a`.

You can do the following:

1. `a = a + 4`. But this tends to be verbose.

2. `a += 4`: Another way to do exactly the same calculation.

`+=` is a shortcut operator. There are other shortcut operators like: `+=, -=, /=, *=`. No spaces are allowed between -=, +=.

In other languages, you can use `++` or `--`, but they are not available in Python.

`>>> a = 10>>> a += 10>>> a = a + 10 # same as a += 10>>> a *= 10>>> a /= 10`

# Membership test

The `in` operator tests if the element on the left hand side is present in the right hand side sequence (list, tuple, set).

`>>> a = [1,2,3] # also works on set and tuples.>>> 3 in aTrue`

# Boolean operators

## not

Read the docs `not` converts True to False and vice versa.

`>>> not TrueFalse>>> not FalseTrue`

### False like values

Variables of any data type when they are null or have no value, they are False like values. The negation of a False like value is True

`>>> not '' # empty string is False like.True>>> not 0 # 0 is False like.True>>> not dict() # empty dict is False like.True>>> not list() # empty list is False like.True`

### True like

Variables of any data type when have some value, any value, they are True like values. The negation of a True like value is False

`>>> not 'dd'False>>> not 1 # non zero is True like.False>>> not -1 # non zero is True like.False>>> not [1,2,3] # list having any value is True like.False`

## or

OR is true when either of the operand is true.

`>>> True or FalseTrue>>> False or FalseFalse`

## and

AND is true when both the operands are true.

`>>> True and FalseFalse>>> False and FalseFalse>>> True and TrueTrue`