Operators in python.

Operators in Python are used for operations between two variables or values. The output varies depending on the type of operator used in the operation. We can call operators as special symbols or builds to control the values of the operands. Assume in the event that you need to perform addition of two variables or values, you can use the operator for this activity. The values in the operands can be either variable or any data type which are present in Python.

Depending upon the type of operations there are 7 types of operators in python programming language.

Types of Operators

  • Arithmetic operators
  • Assignment operators
  • Logical operators
  • Comparison operators
  • Membership operators
  • Identity operators
  • Bitwise operators

Arithmetic Operators

In actuality, we regularly perform arithmetic operations. They help us to figure the change from a buy, decide the area of a room, count the quantity of individuals in a line, etc. Similar activities are used in programs.

Basic arithmetic operations in Python are:

  • addition +
  • subtraction –
  • multiplication *
  • division /
  • integer division //

Let us understand with the help of examples:

print(50 + 10)   
print(100 - 50)  
print(10 * 10)   
print(89 / 10)   
print(89 // 10)

Output

There is a contrast between division/and integer division//. The / delivers a floating point number (like 8.9), while the // one creates a whole number value (like 8) overlooking the decimal part.

Python also raises an error if you try to divide anything by zero.

ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-3-221068dc2815> in <module>
----> 1 4/0

ZeroDivisionError: division by zero

Arithmetic operators can be used to write more complex operations

print(7 + 5 * 10)

The calculation request matches with the standards of arithmetic operations. Multiplication has a higher priority level than that of addition and subtraction, so the activity 5 * 10 is determined first.

To determine the order for execution, you can use brackets, as shown below:

print((7 + 5) * 10)

The remainder of a division. Python modulo operator % is used to get the remainder of a division. It might prove to be useful when you need to check if a number is even. Applied to 2, it returns 1 for odd numbers and 0 for the even ones.

print(15 % 2) 
print(80 % 2)

Operations Priority

To summarize, there is a list of priorities for every operations:

  • parenthesis
  • power
  • unary minus
  • multiplication, division, and remainder
  • addition and subtraction

As referenced over, the unary minus changes the sign of its argument. Sometimes operations have same priority.

Let’s understand from a complex example:

Write a program that takes a single integer number n and then performs the operations in the following order:

adds n to itself
multiplies the result by n
subtracts n from the result
exactly divides the result by n (that means, you need to carry out integer division).
Then print the result of the division. The example is given below:

20 + 20 = 40
16 * 20 = 320
128 – 20 = 108
120 // 20= 6

n = int(input())
print(((n + n) * n - n) // n)

Output

Assignment Operator

We use the assignment operators to assign any value or result of an expression to a variable. Following are the assignment operators that we have in Python.

Let us see with the help of an example, how we will assign a value to a variable a, and a result of an expression to variable b.

a = 50
b = 20 + 30

Shorthand Assignment Operator

Let’s say we want to assign 50 to a variable x, and then increment it by 5, so instead of doing

x = 50
x = x + 5

we can simply use shorthand assignment like

x = 50
x += 5

List of shorthand operators are:

Logical Operators

Logical Operators are used to combine two logical statements.

Let’s see the different logical operators in Python:

  • And : The and operator returns a bool value True if both the operands are true, else it returns False.

Syntax:

operand_1 and operand_2

The truth-table for And operator is as follows:

Here are some examples:

Output

In and operator, if the first operand is evaluated to False, then the second operand is not evaluated at all.

  • Or : The Or operator returns False when both the operands are False, else it returns True.

Syntax:

operand_1 or operand_2

The truth-table for Or operator is as follows:

Here are some examples:

Output

In or operator, if the first operand is evaluated to True, then the second operand is not evaluated at all.

  • Not : Reverses the result i.e. if the result is True it returns False and vice versa.

Syntax:

not operand

The truth-table for not operator is as follows:

Here are some examples:

Output

Comparison Operator

Comparison Operators are used to compare two values and return True or False as output. Let’s have a look at the comparison operators that we have in Python.

Syntax:

operand_1 operator operand_2

Here are some examples where x = 20, y= 10

Output

Membership Operator

Membership operator are used to check if a sequence is present in an object.

  • in : it checks if a value is a part of a sequence, such as a list.
  • not in : it checks if a value is not a part of a sequence.

Let us understand with the help of an example:

x = ["table", "chair", "book", "copies"]

print("copies" in x)

print("textbook" not in x)

Output

Identity Operator

We have two identity operators in python to be specific they are is and is not. These are also called as special operators and are used to discover if two identical values situated on a same part of the memory or not. In the event that two factors are equivalent, that doesn’t mean they are identical. We have two unique operators.

  • is : it returns True if two identical operands reside in the same memory, otherwise returns False.

Let’s understand with the help of an example:

x = ["tea", "coffee"]
y = ["tea", "coffee"]
z = x

print(x is z)

print(x is y)

print(x == y)

Output

  • is not : It will return False if either side of the operator pointing towards the same thing, otherwise it returns True.

Let’s understand with the help of an example:

x = ["tea", "coffee"]
y = ["tea", "coffee"]
z = x

print(x is not z)

print(x is not y)

print(x != y)

Output

Bitwise Operator

Bitwise operators are used in Python to perform the operations on binary numerals or bit patterns. It operates bit by bit.

Syntax:

operand_1 bitwiseoperator operand_2

Let us have a look at the Bitwise operators in Python.

Now let’s understand with the help of an example:

a = 250
b = 40

# Print bitwise AND operation   
print(a & b)

# Print bitwise OR operation
print(a | b)

# print bitwise XOR operation 
print(a ^ b)

# Print bitwise NOT operation 
print(~a)

# print bitwise left shift operation 
print(a << 2)

# print bitwise right shift operation 
print(a >> 2)

Output

Python Operators Precedence

 

In the above table, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom.

Let’s understand the operators from a simple example:

#Arithmetic Operators
a = 40
b = 90
print("a + b = ",a + b)

#Comparison Operators
x = 10
y = 20
print('x > y  is',x > y)

#Assignment Operators
num1 = 90
num2 = 5
print ("Value of num1 : ", num1)
print ("Value of num2 : ", num2)

#compound assignment operator
x = 14
y = 15
res = x + y
res += x
print ("Compound assignment + is ", res)

#Logical Operators
a = True
b = False
print('a and b:',a and b)
print('a or b:',a or b)
print('not b:',not b)

#Membership Operators
x = 40
y = 90
list = [10, 20, 30, 40, 50 ]
if( x in list ):
    print("x is available in the given list")
else:
    print("x is not available in the given list")
if( y not in list ):
    print("y is not available in the given list")
else:
    print("y is available in the given list")

#Identity Operators
x = 20
y = 20
if( x is y ):
    print("x & y same identity")
y=50
if( x is not y ):
    print("x & y have different identity")

#Operator precedence
a = 40
b = 50
c = 87
d = 152
e = 0
e = (a + b) * c % d 
print("Value of (a + b) * c % d: ", e)

Output