# Operators and expressions

### Contents

### Arithmetic Operators

### Addition

cats = 3 dogs = 2 pets = cats + dogs print ( pets )

5

There is another operator **+=** that can be used for addition as well. For example, you can do,

cats += dogs print ( cats )

5

### Substraction

total_distance = 100 distance_so_far = 25 distance_remaining = total_distance - distance_so_far print ( distance_remaining)

75

Similar to how we have used += before, you can use the -= operator for substraction.

### Division

distance = 100 # miles driven fuel = 5 # fuel consumed in gallons avg_consumption = distance / fuel print ( avg_consumption )

20.0

Similar to how we have used += before, you can use the /= operator for division.

### Multiplication

avg = 20 # average fuel consumption in miles/gallon fuel = 5 # total fuel available in gallons distance = avg * fuel print (distance)

100

Similar to how we have used += before, you can use the *= operator for multiplication.

### Modulo Division

This provides the remainder of the division operation.

dividend = 21 divisor = 10 remainder = dividend % divisor print ( remainder )

1

### Floor Division

dividend = 21 divisor = 10 quotient = dividend // divisor print ( quotient )

2

### Exponentiation

Also called the power function, you can do exponential multiplication of one variable with another using the ** operator.

For example, the square of a number is done in Python as

count = 10 count ** 2

100

### Comparision Operators

### == operator

While a single **=** assigns a value to a variable, a **==** is used to check if the value on the left is equal to the value on the right.

count = 10 # modulo division by 2 is 0. Meaning its an odd number. count%2

0

# Now you can use the == operator to do a comparion of one variable to another if count % 2 == 0 : print ( count, " is an even number")

10 is an even number

### != operator

If we want to compare for inequality, use the **!=** operator.

count = 9 if count % 2 != 0 : print ( count, " is an odd number")

9 is an odd number

### < or <= operator

These are used to compare one number with another. Of course, these can’t be used for characters with any meaningful effect.

age = 16 if age < 20 : print ( " teenager ")

teenager

An equivalent for this would be

if age <=19 : print ( " teenager ")

teenager

### > or >= operators

These work exactly opposite to the < or <= operators.

if age > 19 : print ( " not a teenager ")

if age >= 20 : print ( " not a teenager ")

### Logical operators

#### and operator

Sometimes you want to compare the result of two different comparisions and check if either of them are true or neither of them are true or both are True. For example, if the age is between 13 and 19, you can call them as a teenager, right ? How would you combine both these tests into one ?

if age >= 13 and age <=19 : print ( " teenager")

teenager

How does this work ? Essentially, **and** compares truth values across either of the expressions. In this case, we are trying to compare if both the conditions are True.

age >=13

True

age <=19

True

### or operator

Only if both the expressions are true will the **and** operator result in True. The **or** operator on the other hand evaluates to True if either of the expressions are True

qual = "Bachelors" # qualification exp = 6 # experience if qual == "Bachelors" or exp > 5 : print ( "Qualified for the job")

Qualified for the job

You could even chain these operators and still get the same effect. So, in the following case, if at least one of the 3 conditions are True, the result in a True.

if qual == "Bachelors" or qual == "Masters" or exp > 5 : print ( "Qualified")

Qualified

### not operator

There is another operator called the **not** operator. It basically negates the truth value. For example,

x = 5 x > 4

True

not x > 4

False

### Operator Precedence

What do you think is the result of the following operation ?

weight = 85 # kilos height = 1.8 # meters bmi = weight / height**2 print (bmi)

26.234567901234566

There are two ways in which this could be done

(weight/height)**2

2229.938271604938

weight / (height ** 2)

26.234567901234566

Why did Python choose the later and not the former ? The reason is **Operator Precedence**. In Python ( as with any other language) there is a precedence based on which operators are performed. For example,

2 + 3 / 5

2.6

Division is done first followed by the addition. What about multiplication vs division ?

2 * 3 / 5

1.2

Division is done first followed by multiplication.

There is a simple chart in Operator Predence from Python docs that gives the precedence in increasing order. We don’t need to learn these. Nobody remembers these anyway. So, usually, programmers get around knowing this by using paranthesis. For example, to do the same operation as above, instead of relying on Python’s operator precedence to take care of the calculation, you can explicitly ask Python to evaluate this a particular way.

2 + (3 / 5) # (division is done first followed by addition)

2.6

(2 + 3) / 5 # ( addition is done first followed by division)

1.0

This way, we control which operation should be done first.