Operators and Operands in python, a brief description
We will briefly explain the Operators and operands in Python. Operators are special symbols in programming. They represent computations like addition, subtraction, division, and multiplication.
>>> A = 10
>>> B = 10
>>> C = a+ b
Here a and b are operands. The values the operator is applied to are called operands.
The operators +, -, *, /, and ** are addition, subtraction, multiplication, division, and exponentiation respectively.
The division operator is changed from python 2 to python 3. In Python 3.x, the result of this division is a ﬂoating point result.
>>> minute = 59
The division operator in Python 2.0 would divide two integers. It truncates the result to an integer as in the following example variable:
>>> minute = 59
To obtain the same answer in Python 3.0 use ﬂoored ( // integer) division.
>>> minute = 59
Order of operations
When more than one operator appears in the python expression, the order of solution depends on the rules of precedence.
For mathematical operators, Python follows mathematical convention. The acronym PEMDAS is a useful way to remember the rules:
What does PEMDAS Means?
It is a mathematical convention. It stands for parentheses, Exponentiation, Multiplication, Division addition, and subtraction.
Parentheses have the highest precedence in mathematics. Since expressions in parentheses are evaluated ﬁrst, for example,
>>> 4 * (4-3) is 4
>>> (4+1)**(8-2) is 15625
The parentheses also make an expression simple and easier to read and write, as in (minute * 100) / 60, even if it doesn’t change the result.
Exponentiation has the next highest precedence, for example,
>>> (3**1+1) is 4, not 9.
The precedence goes lower from right to left. after exponentiation, the next precedence of operators in python will be multiplication, division, and so on.
Python operators in detail
Operators in Python are divided into the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
Arithmetic Operators in Python
Arithmetic operators in python are used to perform mathematical operations. The operations include addition, subtraction, multiplication, and division. Here is the list of arithmetic operators in python.
|+||Addition||a + b|
|–||Subtraction||a – b|
|*||Multiplication||a * b|
|/||Division||a / b|
|%||Modulus||a % b|
|**||Exponentiation||a ** b|
|//||Floor division||a // b|
Assignment Operators in Python
Assignment operators in Python are used to assign values to variables.
|=||x = 5||x = 5|
|+=||x += 3||x = x + 3|
|-=||x -= 3||x = x – 3|
|*=||x *= 3||x = x * 3|
|/=||x /= 3||x = x / 3|
|%=||x %= 3||x = x % 3|
|//=||x //= 3||x = x // 3|
|**=||x **= 3||x = x ** 3|
|&=||x &= 3||x = x & 3|
||=||x |= 3||x = x | 3|
|^=||x ^= 3||x = x ^ 3|
|>>=||x >>= 3||x = x >> 3|
|<<=||x <<= 3||x = x << 3|
Comparison Operators in Python
Comparison operators are also called python relational operators. They are used to compare two values. These operators are used in conditions that compare one expression with another.
The result of a comparison can be TRUE or FALSE. Here is list of Python comparison operators.
|==||Equal||x == y|
|!=||Not equal||x != y|
|>||Greater than||x > y|
|<||Less than||x < y|
|>=||Greater than or equal to||x >= y|
|<=||Less than or equal to||x <= y|
Logical operators in Python
Logical operators in Python are used for conditional statements and return true or false. These operators are and, or, and not and are defined as follows:
|and||It returns True if both statements are true||x < 5 and x < 10|
|or||It returns True if one of the statements is true||x < 5 or x < 4|
|not||It reverses the result, returns False if the result is true||not(x < 5 and x < 10)|
Identity operators in python
The identity operators in python are used to compare objects. These operators compare the memory locations of objects.
There are two identity operators.
- is not
where the ‘ Is ‘ operator returns true if the location of objects is the same. for example, consider the example:
>>> a = 10
>>> b = 20
>>> a = b ( will returns False)
We can check the location using id(a) or id(b)
If we set ( a = 20 ) or ( a = b ) then ( a is b ) will return True.
The second identity operator ‘ is not ‘ returns the result opposite to the ‘is’ operator.
The above example ( a is not b) will return True. If we set ( a = 20 ) or ( a = b ) then ( a is not b ) will return False. This means the id(a) is equal to id(b).
Python Membership Operators
The membership operators are used to check the presence of a sequence in an object or list etc.
consider the following example:
>>> colors = [“red”, “green”, “blue”]
>>> print(“blue” in colors) will return true because the ‘blue’ is present in colors.
There are two types of membership operators in python.
- in ( return True if the sequence is present in the specified object)
- not in ( returns True if the sequence is not present in the specified object)
Bitwise Operators in Python
There are bitwise operators in python to compare the binary numbers. we can use bitwise operators to perform Boolean logic on individual bits.
Here are the bitwise operators and their descriptions in the given table.
let us consider two variables.
>>> a = 1
>>> b = 1
|&||AND||returns 1 if both a and b are 1 (see example above)|
||||OR||returns 1 if eighter a or b is 1 (see example above)|
|^||XOR||Sets each bit to 1 if only one of two bits is 1|
|~||NOT||Inverts all the bits ( 1 to zero and zero to 1)|
|<<||left shift||Shift left by pushing zeros in from the right and let the leftmost bits fall off|
|>>||right shift||The right shift operator ( >> ) shifts the first operand the specified number of bits to the right.|