PickupBrain

Operators & Operands in Python

As you know, the basic building blocks of any programming language are variables and constants, using which we create a series of expressions. In order to, write an expression, we require operators and operands. Operators are the symbols that are used to perform a different computational task, while operands are the values (variables, constants, Python objects, etc.) on which we are performing operations.
An operator is used to perform an operation between two values or variables. Now we can call operators as constructs or special symbol that are used to manipulate the values of the operands.
Now you must wonder what is an operand, so the value that we use in operation are known as operands. These operands can be variables or any data types that we have in python.

Depending on the type of operations they do, operators are classified as –

  • Assignment operators
  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Bitwise operators

Lets see them one-by-one.

Assignment Operator

In programming, = sign is known as an assignment sign. It means we are assigning the value on the right side of the = sign to the variable on the left.

The statements x = y and y = x have very different meanings in programming.

Confused? Don’t worry have a look on below example we are here to clear your confusion 😉

Write the following code into your IDLE editor, and save it.

x = 5

y = 10

x = y

print(“x =”), x)

print(“y =”), y)

now run this program and you will get this output.

x = 10

y = 10

Here, x has an initial value of 5 (declared on the first line), the third line x = y assigns the value of y to x, hence changing the value of y remains unchanged.

Now, change the third line from x = y to y = x.

Mathematically, x = y and y = x mean the same thing, but not so in programming.

Run this modified program and you will get below output

x = 5

y = 5

here, the x value remains as 5, but it changes the value of y to 5, because the statement y = x assigns the value of x to y and y becomes 5 while x remains unchanged as 5.

There are a few more assignment operators in python. Operators like += , -= and *=.

For example, we have the variable x, with an initial value of 10. If we want to increase the value of x by 2, we can write

x = x+2

This program will firstly evaluate the expression on the right (x+2) and assign the value to the left. So the statement becomes x= 12.

Instead of writing above statement, we can also write

x += 2

this combines the assignment sign with addition operator.

Similarly, if we want to do a substraction, we can write

x -= 2

this works for all the arithmetic operators.

Arithmetic Operator

These operators are used to perform mathematical operations like addition, substraction, multiplication, etc.

OperatorMeaningExample
+Add two operands or unary plusx + y
+2
Subtract right operand from the left or unary minusx – y
-2
*Multiply two operandsx * y
/Divide left operand by the right one (always results into float)x / y
%Modulus – remainder of the division of left operand by the rightx % y (remainder of x/y)
//Floor division – division that results into whole number adjusted to the left in the number linex // y
**Exponent – left operand raised to the power of right

 

Suppose

x = 5, y = 2

Addition:
x + y = 7

Substraction:
x – y = 3

Multiplication:
x * y = 10

Division:
x/y = 2.5

Floor Division:
x // y = 2 (rounds down the answer to the nearest whole number)

Modulus:
x % y = 1 (gives the remainder when 5 is divided by 2)

Exponent:
x ** y = 25 (5 to the power of 2).

Relational Operators

Relational operators are symbols that perform operations on data and return a result as true or false depending on the comparison conditions. Thus, they are certain functionalities that do something with your variables.

Let’s take a look at the different types of relational operators in python. The following table gives you a list of all the relational operators:

Relational OperatorMeaning
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
= =Equals
! =Not equal to

Relational operators can be used to compare numbers as well as strings.

#Less than

> print 50 < 60

True
# Less or equals to

> print 50 <= 60

False

# Greater than

> print 50 > 60

True

# Greater or equals to

> print 50 >= 60

False

# Equals to

>print 50 == 60

False

> print 50 == 50

True

# Does not equal to

> print 50 != 50

False

> print 50 != 60

True

 

Logical Operators

There are three kinds of logical operations in Python. Each of them operates on one or more bool type and returns a bool type. They are listed as below:

  • and – Output is True, if all the operands are True, else False
  • or – Output is True, if any one of the operands is True, else False
  • not – Output is True if operand is False, else False.

Example:

#Less than

print 50 < 60

#Less or equals to

print 50 <= 60

False

# Greater than

> print 50 > 60

True

# Greater or equals to

> print 50 >= 60

False

# Equals to

>print 50 == 60

False

> print 50 == 50

True

# Does not equal to

> print 50 != 50

False

> print 50 != 60

True

 

Bitwise Operators

We have come across different kinds of operands and operators, but these operators are special ones. Bitwise operators process bits or bit patterns, unlike other operators. Basic bitwise operations are listed as below:

OperatorNameDescription
&ANDSets each bit to 1 if both bits are 1
|ORSets each bit to 1 if one of two bits is 1
 ^XORSets each bit to 1 if only one of two bits is 1
~NOTInverts all the bits
<<Zero fill left shiftShift left by pushing zeros in from the right and let the leftmost bits fall off
>>Signed right shiftShift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off

 

a = 60     # 60 = 0011 1100 
b = 13     # 13 = 0000 1101 
c = 0 

c = a & b;   # 12 = 0000 1100 
print "Value of c is ", c c = a | b;    # 61 = 0011 1101 
print "Value of c is ", c c = a ^ b;    # 49 = 0011 0001 
print "Value of c is ", c c = ~a;       # -61 = 1100 0011 
print "Value of c is ", c c = a << 2;   # 240 = 1111 0000 
print "Value of c is ", c c = a >> 2;   # 15 = 0000 1111 
print "Value of c is ", c

 

When you execute the above program, it produces the following result −

 Value of c is 12
 Value of c is 61
 Value of c is 49
 Value of c is -61
 Value of c is 240
 Value of c is 15

In this article, we learned about various operators in Python. Please let us know about your views and opinions in the comment section below and stay tuned. Thank you.😊

Leave a Reply