Operators are characters (symbols) that represent a single action or a process.
Operator – a function that operate on a value (operand).
Operand – the value, the subject of the operation.
Arithmetic operators perform mathematical operations - addition, subtraction, multiplication, division, exponentiation, etc.
|+||Addition - add two operands||2 + 3|
|-||Subtraction - subtract the right operand from the left operand||100 - 98|
|*||Multiplication - multiply two operands||5 * 3|
|/||Division - the left operand is divided by the right operand||10 / 2|
|//||Floor Division - the left operand is divided by the right operand. An integer which is less than or equal to a specified expression is returned.||36 // 10|
|**||Exponentiation - the left operand is raised to the power of the right operand||2 ** 4|
|%||Modulo - the reminder of the divison of the left operand by the right operand||10 % 3|
Comparison operators compare the operands and return True or False.
|==||Equal - true if the operand are equal||x == y|
|!=||Not Equal - true if the operands are not equal||x != y|
|>||Greater than - true if the left operand is greater than the right one||x > y|
|>=||Greater than or equal - true if the left operand is greater than or equal to the right one||x >= y|
|<||Less than - true if the left operand is less than the right one||x < y|
|<=||Less than or equal - true if the left operand is less than or equal to the right one||x <= y|
Assignment operators store a value in the operand
|=||x = 3||x = 3|
|+=||x += 3||x = x + 3|
|-=||x -= 4||x = x - 4|
|*=||x *= 4||x = x * 4|
|/=||x /= 5||x = x / 5|
|//=||x //= 5||x = x // 5|
|**=||x **= 8||x = x ** 8|
|%=||x %=||x = x % 4|
There are 3 logical operators - and, or, not
The use of logical operators is very common in programming.
|and||Returns True if both statements are True. Otherwise it returns False|
|or||Returns True if one of the statements is True. Otherwise it returns False|
|not||Returns False if the result is True, and True if the result is False.|
Watch the example:
#Logical operators print(True and False) print(True or False) print(not True)
Order of evaluation of logical operators
Python evaluates logical operators from left to right.
Watch the example:
#Order of evaluation of logical operators def foo(a): print(a) foo(5) or foo(10) or foo(1)
Python does not evaluate redundant subexpressions. In many cases the truth of an expression can be determined without evaluating each of every subexpression. For instance python stops evaluating and expressions as soon as one of the subexpressions is False. The reason is clear - a single False evaluation causes the entire expression to be considered False. This method is called lazy evaluation (short circuit evaluation). There are two benefits to the short-circuit evaluation of logical operators. It is more efficient since not every expression has to be evaluated. Moreover it can be very useful as it skips part of the code and therefore can prevent errors.
Watch the example:
#Lazy Evaluation print(False and 5/0) print(True or 5 / 0)
Bitwise operators operate directly the binary level ("0" and "1").
They operate bit by bit.
||||OR - true if one of its operands are true||x | y|
|&||AND - true if and only if all of its operands are true||x & y|
|^||XOR - true if and only if its arguments differ||x ^ y|
|~||NOT - reverses the true/false outcome of the expression||x ~ y|
|>>||Right shift - shifts the bits of the operand to the right||x = x / 5|
|<<||Left shift - shifts the bits of the operand to the left||x = x // 5|
A truth table is a mathematical table used in logic which shows true/false values of bitwise operators.
Below you can find a truth table the summerize in a simple way the bitwise operators.
In Python the same operator can mean different things with different data types. For instance the + operator is used to add two operands (if they are numbers). However if lists are provided the operator concatenates them. This behaviour is known as operator overloading.
Here are some examples. Try to read understand what the code does. You can also modify and run it.
# Operators x = 5 x += 1 print(x) y = 6 print(x == y)
Create 'x' variable and assign a number. Create and assign 'y' variable. Try to use the different operators!
# Write your code here