Operators and operands in Python

Operators and operands in Python

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.
For example
>>> 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 floating point result.
For example,
>>> minute = 59
>>> minute/60

Output: 0.9833333333333333

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

>>> minute/60

Output: 0

To obtain the same answer in Python 3.0 use floored ( // integer) division.
>>> minute = 59
>>> minute//60
Output: 0

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 first, 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.

+Additiona + b
Subtractiona – b
*Multiplicationa * b
/Divisiona / b
%Modulusa % b
**Exponentiationa ** b
//Floor divisiona // b

Assignment Operators in Python

Assignment operators in Python are used to assign values to variables.

OperatorExampleSame As
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x – 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
//=x //= 3x = x // 3
**=x **= 3x = x ** 3
&=x &= 3x = x & 3
|=x |= 3x = x | 3
^=x ^= 3x = x ^ 3
>>=x >>= 3x = x >> 3
<<=x <<= 3x = 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.

==Equalx == y
!=Not equalx != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= 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 truex < 5 and  x < 10
orIt returns True if one of the statements is truex < 5 or x < 4
notIt reverses the result, returns False if the result is truenot(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.

  1. is
  2. 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.

  1. in ( return True if the sequence is present in the specified object)
  2. 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

ANDreturns 1 if both a and b are 1 (see example above)
|ORreturns 1 if eighter a or b is 1 (see example above)
 ^XORSets each bit to 1 if only one of two bits is 1
NOTInverts all the bits ( 1 to zero and zero to 1)
<<left shiftShift left by pushing zeros in from the right and let the leftmost bits fall off
>>right shiftThe right shift operator ( >> ) shifts the first operand the specified number of bits to the right

Video tutorials to learn operators and operands in python