Operators and expressions
cats = 3 dogs = 2 pets = cats + dogs print ( pets )
There is another operator += that can be used for addition as well. For example, you can do,
cats += dogs print ( cats )
total_distance = 100 distance_so_far = 25 distance_remaining = total_distance - distance_so_far print ( distance_remaining)
Similar to how we have used += before, you can use the -= operator for substraction.
distance = 100 # miles driven fuel = 5 # fuel consumed in gallons avg_consumption = distance / fuel print ( avg_consumption )
Similar to how we have used += before, you can use the /= operator for division.
avg = 20 # average fuel consumption in miles/gallon fuel = 5 # total fuel available in gallons distance = avg * fuel print (distance)
Similar to how we have used += before, you can use the *= operator for multiplication.
This provides the remainder of the division operation.
dividend = 21 divisor = 10 remainder = dividend % divisor print ( remainder )
dividend = 21 divisor = 10 quotient = dividend // divisor print ( quotient )
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
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
# 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
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 ")
An equivalent for this would be
if age <=19 : print ( " 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 ")
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")
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.
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")
There is another operator called the not operator. It basically negates the truth value. For example,
x = 5 x > 4
not x > 4
What do you think is the result of the following operation ?
weight = 85 # kilos height = 1.8 # meters bmi = weight / height**2 print (bmi)
There are two ways in which this could be done
weight / (height ** 2)
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
Division is done first followed by the addition. What about multiplication vs division ?
2 * 3 / 5
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 + 3) / 5 # ( addition is done first followed by division)
This way, we control which operation should be done first.