Operators in Python

Operators in Python

Operate the expression

Namaste Coders!

In this article, we will discuss different Python operators and how to use them. Many of them you must be using in your life.

Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand.

Arithmetic Operators

It performs all the mathematical computation.

33.PNG

These are binary operators as it requires two operands to perform the computation.

'''
Arithmetic operator
+,-,/,* 
// -> floor divison
% -> modulus
** -> exponentation
'''

num1 = 5
num2 = 2

add = num1+ num2
print("Addition res is: ",add)  #Addition res is:  7

sub = num1 - num2
print("Subtraction res is: ",sub)  #Subtraction res is:  3   


mul = num1*num2
print("Multiplication res is: ",mul)  #Multiplication res is:  10

div = num1/num2
print("Quotient res is: ",div)  #Quotient res is:  2.5     

# NOTE: floor division(//) gives quotient without decimal part
print(num1 // num2)  #2

# NOTE: modulus (%) is used to get the remainder
print(num1%num2)  #1

# NOTE: **  is used to get the exponent
print(5 ** 2)  #25

Assignment Operator

Assignment operators are used to assigning values to the variables. Assign the value of right side of expression to the left side operand.

34.PNG

It assigns computation of a and b in x.

Shorthand assignment operator:

35.PNG

36.PNG

Example

37.PNG

Relational Operator

It provides True or False on comparing the values. It helps in decision making.

38.PNG

39.PNG

Logical Operator

It performs logical and, logical or and logical not on the basis of the truth table.

40.PNG

41.PNG

Bitwise Operator

These operators perform operations on the bits of numbers.

42.PNG

Example

43.PNG

Membership Operator

in and not in are the membership operators, used to test whether a value or variable is in a sequence. These are used in sequence datatypes like strings, list, tuple, dictionary.

  • in: True if value is found in the sequence
  • not in: True if value is not found in the sequence

Example

44.PNG

Identity Operator

is and is not are the identity operators both are used to check if two values are located on the same part of the memory. Two variables that are equal do not imply that they are identical.

  • is - True if the operands are identical
  • is not - True if the operands are not identical

Example

45.PNG

The End

I hope you enjoyed the article and had a good learning experience.

Follow for more articles and keep sharing👩

Keep coding

Linkedin

hashnode blogs

Did you find this article valuable?

Support akshita garg by becoming a sponsor. Any amount is appreciated!