Operations can be performed between one or more operands provided with an operator. Lets discuss different types of operator that present in Python.
Arithmetic Operators are a bit similar to those that are used in mathematics. Following tables explains the operators that can be used in Python:
|+||plus operator is used for addition||a + b + ...||5 + 6 outputs 11|
|-||minus operator is used for subtraction||a - b - ...||5 - 6 outputs -1|
|*||* operator is used for multiplication||a * b * ...||5 * 6 outputs 30|
|/||/ operator is used for division||a / b / ...||6 / 3 outputs 2|
|%||modulus operator is used for getting the remainder||a % b||5 % 3 outputs 2|
|//||// performs floor division. Floor division means it divides the two numbers and then gives the floor value of the result.||a // b||5 // 3 outputs 1|
|**||** operator is used for finding the power. The expression a**b represents a to the power b.||a ** b||5 ** 2 outputs 25|
>>> a = 5 >>> b = 6 >>> a + b output: 11 >>> a - b output: -1 >>> a * b output: 30 >>> a / b output: 0.8333333333333334 >>> a % b output: 5 >>> a // b output: 0 >>> 5 ** 2 output: 25
'=' is known as assignment operator and is used to assign values.
The use of assignment operator is quite easy.
>>> a = 5 >>> a output: 5
Shorthand Operators are shortcut methods for performing operations and assigning it to a variable. Following table explains the shorthand operators that can be used in Python:
|+=||This operator adds two values and assign it to the first one.||a += b||a = a + b|
|-=||This operator subtract two values and assign it to the first one.||a -= b||a = a - b|
|*=||This operator multiplies two values and assign it to the first one.||a *= b||a = a * b|
|/=||This operator divides the two values and assign it to the first one.||a /= b||a = a / b|
>>> a = 5 >>> b = 6 >>> a += b >>> a output: 11
Comparison Operators are used for comparing values and is helpful in conditional statements. They always result in true or false. We will discuss conditional statements in later lessons. Lets see the available comparison operators:
|<||returns true if first operand is less than second||a < b||5 < 6 outputs True|
|>||returns true if first operand is greater than second||a > b||5 > 6 outputs False|
|==||returns true if both operands are equal||a == b||5 == 5 outputs True|
|<=||returns true if first operand is less than or equal to second||a <= b||5 <= 6 outputs True|
|>=||returns true if first operand is greater than or equal to second||a >= b||5 >= 6 outputs True|
|!=||returns true if first operand is not equal to second||a != b||5 != 6 outputs True|
Remember that number 0 is treated as False and 1 is treated as True. Actually any number other than 0 is treated as True.
>>> 5 < 6 output: True >>> 5 > 6 output: False >>> 5 == 5 output: True >>> 5 <= 6 output: True >>> 5 != 6 output: True
Logical Operators are used with conditional statements and can be used to combine two or more conditions.
|and||returns True if both the operands are true||a and b||True and True outputs True|
|or||returns True if any of the operands is true||a or b||True or False outputs True|
|not||its works on one operator and returns it compliment.||not a||not True outputs False|
>>> a = 1 >>> b = 0 >>> a and b output: 0 >>> a or b output: 1 >>> not a output: False
Bitwise Operators works on bits of the operands and perform bit by bit operation to get the desired result.
|&||bitwise and (&) operator returns 1 if both the bits of operands are 1||a & b||1 & 1 outputs 1|
||||bitwise or (|) operator returns 1 if any one bit of the operands is 1||a | b||1 | 0 outputs 1|
|~||bitwise not (~) operator is a unary operator(that works on single bit) and returns compliment of a bit||~ a||~1 outputs 0|
|^||bitwise xor (~) operator returns 1 if both the bits of the operand are different||a ~ b||1 ~ 0 outputs 1|
|<<||left shift operator shifts the bits of an operand to the left by a specified value. No bits are dropped.||a<<2||7<<2 outputs 28|
|>>||right shift operator shifts the bits of an operand to the right by a specified value. Values shifted right are dropped off.||a>>2||7>>2 outputs 1|
Lets discuss how bitwise operators work. Before performing bitwise operations, operands are first converted into binary values and then comparing bits of both the operands the result is obtained. Suppose we assign a=1 and b=3. Binary value of 1 is 01 and that of 3 is 11. Now suppose we apply bitwise and operator (&) between a and b that is a & b, the it will compare both left bits of a and b, 1 & 1 which results in 1. After that it will compare rightmost bits of a and b that is, 0 & 1 which results in 0. So final result is 01 that is 1.
a = 0 1 b = 1 1 --------------- output = 0 1 ---------------
>>> 1 & 3 output: 1 >>> 1 | 3 output: 3 >>> ~ 1 output: -2 >>> 1 ^ 3 output: 2 >>> 7 << 2 output: 28 >>> 7 >> 2 output: 1
Membership operator works with sequences like strings, lists or tuples. It checks whether a value is present in a sequence or not.
|in||returns True if a value/variable is present in the specified sequence||var in str|
|not in||returns True if a value/variable is not present in the specified sequence||var not in str|
>>> str = "hello" >>> 'h' in str output: True >>> 'a' not in str output: True >>> list = [1,2,3] >>> 1 in list output: True >>> 5 in list output: False
Identity operators check whether two operands are identical or not by comparing their memory locations.
|is||returns True if operands are identical else returns false||var1 is var2|
|not is||returns True if operands are not identical else returns false||var1 is not var2|
>>> a = 5 >>> b = 5 >>> a is b output: True >>> a is not b output: False