Pages

Wednesday, December 23, 2020

Operators in Python

Operators

Operator is a symbol that is used to perform specific operations like mathematical, relational or logical operations. 

There are different types of operators. Below are some the most common operators

  • Mathematical Operators.
  • Assignment Operators.
  • Relational Operators.
  • Logical Operators.

Apart from these, there are couple of more types. 

  • Identity Operators.
  • Membership Operators.

Let's have a look at each of these. 

Mathematical Operators

Mathematical operators are used to perform mathematical operations (as name states) with numeric values. 

These are operators like '+' Addition, '-' Subtraction, '*' Multiplication, '/' Division, '%' Modulus, '//' Floor Division and '**' Exponentiation.

a = 10

b = 5

c = a + b 
# Addition of two variables, Result would be c = 15

c = a - b 
# Subtraction of second variable from first variable, Result would be c = 5

c = a * b 
# Multiplication of two variables, Result would be c = 50

c = a / b 
# Division, Divides first variable with second variable, Result would be c = 2.0. Result for the division operation would always be float.

c = a % b 
# Modulus, Returns the reminder after dividing first variable with second variable. Result would be c = 0. If a = 11, b = 5, then Result would be c = 1

c = a // b 
# Floor Division, Divides first variable (a) with second variable (b) and returns the whole number only. Result would be c = 2

c = a ** b 
# Exponentiation, Calculating the first variable (a) with the power of second variable (b), Result would be c = 10000.

Assignment Operators

Assignment Operators are used to assign value to a variable. 

One of the assignment operators is '=' (equal to) to assign a value to a variable. In python, a variable is declared when a value is assigned. 

a = 10 
# This defines a integer variable 'a' and assigns a value '10'

Below are the few other assignment operators. 

*Considering the current value of 'a' as '10' for all of these examples.

'+=' Adds the value on the right side to the current value of the variable. 

a += 5 
# adds 5 to the current value of 'a' (i.e., 10) and assigns it to 'a'. This is same as 'a = a + 5'.

'-=' Subtracts the value on the right side from the current value of the variable. 

a -= 5 
# subtracts 5 from the current value of 'a' (i.e., 10) and assigns it to 'a'. This is same as 'a = a - 5'.
  
'*=' Multiplies the value on the right side with the current value of the variable. 

a *= 5 
# multiplies 5 with the current value of 'a' (i.e., 10) and assigns it to 'a'. This is same as 'a = a * 5'.

'/=' Divides the current value of the variable with the value on the right side. 

a /= 5
# divides the current value of 'a' (i.e., 10) with 5 and assigns to 'a'. This is same as 'a = a / 5'.

'%=' Divides the current value of the variable with the value on the right side to calculate the remainder. 

a %= 5 
# divides the current value of 'a' (i.e., 10) with 5 and assigns the remainder to 'a'. This is same as 'a = a % 5'.

'//=' Divides the current value of the variable with the value on the right side and assign the resulting whole number

a //= 5 
# (Floor) divide the current value of 'a' (i.e., 10) with '5' and assigns it to 'a'. This is same as 'a = a // 5'.

'**=' Calculate the exponent of the variable to the power of value on the right side. 

a **= 5 
# Calculate the current value of 'a' (i.e., 10) to the power of 5 and assigns it to 'a'. This is same as 'a = a ** 5'.

Relational Operators

Relational Operators are used to compare the relation between two variables (e.g. greater than, less than, equal to...).

Below are the Relational operators. 
  • Equal To (==), used to check if the variables on either sides are equal (a == b). Returns True if equal and False if not equal.
  • Not Equal To (!=), used to check if the variables on either sides are not equal (a != b). Returns True if not equal and False if equal.
  • Greater than (>), used to check if the left side variable is greater than the right side variable (a > b). Returns True if greater and False if not greater. 
  • Greater than or Equal to (>=), used to check if the left side variable is either greater or equal to right side variable (a >= b). Returns True if greater than or equal and False if not.
  • Less than (<), used to check if the left side variable is less than the right side variable (a < b). Returns True if less and False if not less. 
  • Less than or Equal to (<=), used to check if the left side variable is either less or equal to right side variable (a <= b). Returns True if less than or equal and False if not.

Logical Operators

Logical operators are used to combine multiple conditions and returns if the result is True or False. 

  • 'and', returns True if conditions on either sides are True and returns False if at least one of them is False.
(a > b) and (c > d)
# if both conditions (a > b) and (c > d) are True, then the result would be True. If at least one of them is False, then result would be False.

  • 'or', returns True if one of the condition is True and returns False if both of them are False. 
(a > b) or (c > d)
# if at least one of the condition (a > b) or (c > d) are True, then result would be True. If both conditions are False, then result would be False. 

  • 'not', returns the opposite value of the current condition. If the condition is True, not would return False and vice versa.
not (a > b) 
# if (a > b) is True, then the result would False. if (a > b) is False, then result would be True.

Identity Operators

Identity Operators are used to identify if a value belongs to particular class or type. 

  • 'is', returns True if the type (or value) on the left side matches with the type (or value) mentioned on the right side. 
E.g.:

a = 10
print(type(a) is int) # prints True, as the data type of 'a' is int and the condition is satisfied. 

a = 10
b = 11 
print(a is b) # prints False, as the values of 'a' and 'b' aren't same.

  • 'is not', returns False if the type (or value) on the left side doesn't match with the type (or value) mentioned on the right side.
E.g.:

a = 10
print(type(a) is not int) # prints False, as the data type of 'a' is int and the condition is not satisfied. 

a = 10
b = 11 
print(a is not b) # prints True, as the values of 'a' and 'b' aren't same.

Membership Operators

Membership operators are used to check if the value on the left side is part of the sequence (list or tuples). 

  • 'in', returns True if the left side value is part of sequence mentioned on the right side. returns False if not part of sequence mentioned on the right side.
E.g.:

a = [1, 2, 3]
b = 2

print(b in a) # prints True as '2' is part of list 'a'

  • 'not in', returns False if the left side value is part of sequence mentioned on the right side. returns True if not part of sequence mentioned on the right side.
E.g.:

a = [1, 2, 3]
b = 2

print(b not in a) # prints False as '2' is part of list 'a' 


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

No comments:

Post a Comment

Exception Handling in Python (try - except-else-finally)

Exception Handling in Python Exception handling is key for any programming language. When an exception occurs Python interpreter stops execu...