Lesson 5
18 min
Arithmetic, comparison and logical operators to calculate and compare.
Definition
An operator is a symbol or a keyword that performs an operation on one or more values, called operands. In3 + 4, the+symbol is the operator, and3and4are the operands. Python offers arithmetic,assignment, comparison andlogical operators.
Python can do all the usual mathematical operations. Here are the operators to know:
+addition,-subtraction,*multiplication./division (the result is always a float).//integer division (only the whole part is kept).%modulo: the remainder of the division.**power.print(10 + 3) # 13
print(10 - 3) # 7
print(10 * 3) # 30
print(10 / 3) # 3.3333333333333335
print(10 // 3) # 3
print(10 % 3) # 1
print(2 ** 8) # 256The modulo is very useful: for examplen % 2 == 0tells you whether a number is even, andn % 5 == 0whether a number is a multiple of 5. It’s also used for anything that cycles around, like the hours on a clock or the days of the week.
As in mathematics, Python follows an order of precedence: first powers, then multiplication and division (including//and%), and finally addition and subtraction. Parentheses let you force the order you want, and improve readability: don’t hesitate to use them.
print(2 + 3 * 4) # 14 (multiplication comes first)
print((2 + 3) * 4) # 20 (parentheses change the order)
print(2 ** 3 * 2) # 16 (8 * 2)
print(10 - 4 - 3) # 3 (calculated from left to right)To modify a variable based on its current value, you use shortcuts. Each arithmetic operator has its assignment version:+=,-=,*=,/=,//=,%= and**=.
score = 10
score += 5 # equivalent to score = score + 5 -> 15
score -= 3 # -> 12
score *= 2 # -> 24
score //= 5 # -> 4
print(score)
They compare two values and return a boolean (TrueorFalse). This result will very soon be used to make decisions in a program, with conditions.
age = 18
print(age == 18) # True (equal to)
print(age != 18) # False (not equal to)
print(age > 20) # False (greater than)
print(age >= 18) # True (greater than or equal to)
print(age < 30) # True (less than)
print(age <= 17) # False (less than or equal to)You can also compare text:"python" == "python"isTrue, but the comparison is case-sensitive, so"Python" == "python"isFalse. Finally, Python lets you chain comparisons, as in mathematics:
grade = 14
print(0 <= grade <= 20) # True: the grade is between 0 and 20
print("Python" == "python") # False: case mattersWarning
Don’t confuse=(assigning a value to a variable) with==(testing equality). It’s one of the most common mistakes among beginners.
They combine several conditions:and(both must be true),or(at least one must be true) andnot(inverts the result).
age = 20
has_license = True
print(age >= 18 and has_license) # True
print(age < 18 or has_license) # True
print(not has_license) # False
When you mix several logical operators,notis evaluated first, thenand, thenor. When in doubt, add parentheses: the code will be easier to reread.
age = 16
accompanied = True
# Can enter if they are an adult, or if they are a minor but accompanied
can_enter = age >= 18 or (age >= 12 and accompanied)
print(can_enter) # True10 / 2gives5.0, not5. Use//to get an integer."5" == 5isFalsebecause the types are different.Exercise
int().Going further
Trueif it is even and greater than 10,Falseotherwise, in a single expression.