Basic Operators in Python

In this tutorials we will read about what operations can be perform in the python. You may consider operators as worker who perform some task.

Types of Operator

Python language supports the following types of operators.

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison (Relational) Operators
  4. Bitwise Operators
  5. Logical Operators
  6. Membership Operators
  7. Identity Operators

Let us have a look on all operators one by one.

1) Arithmetic Operators:

Arithmetic operators are your maths operators. These are +, -, *, **, / , % and //.

Example:

a = 4 + 5 # + is an arithmetic operator

'**' is exponent operator. '//' operator returns the quotient after dividing the number. Example:

  1. >>> 2**3
  2. 8
  3. >>> 3**2
  4. 9
  5. >>> 5.0 // 2.0
  6. 2.0
  7. >>> 5 // 2
  8. 2

Note: there is a difference between ‘/’ and ‘%’ operator. ‘/’ is divide operator while % is modulus operator. '/' performs different work in python 2 and python 3.

Python 2:

Example: 8 / 5 = 1 ( 8 = 5*1 + 3) while 8 % 5 = 3 ( 3 is reminder)

Python 3:

Example: 8 / 5 = 1.6 while 8 % 5 = 3 ( 3 is reminder)

2) Assignment Operators:

Assignment operator assigns values to variables. Assignment operators are:

OperatorDescriptionExample
=Simple we are giving a value to variablex = y + z
+=add y into x and give result to xx += y is equivalent to x = x + y
-=subtract y from x and give result to xx -= y is equivalent to x = x - y
*=multiply y with x and give result to xx *= y is equivalent to x = x * y
/=divide x by y and give result to xx /= y is equivalent to x = x / y
%=take modulo of x and give result to xx %= y is equivalent to x = x % y
**=rise y power of x and give result to xx **= y is equivalent to x = x ** y
//=floor division of x by y and give result to xx //= y is equivalent to x = x // y

Examples:

  1. >>> a = 4
  2. >>> print(a)
  3. 4
  4. >>> a += 5
  5. >>> print(a)
  6. 9
  7. >>> a -= 2
  8. >>> print(a)
  9. 7
  10. >>> a *= 6
  11. >>> print(a)
  12. 42
  13. >>> a /= 5
  14. >>> print(a)
  15. 8.4
  16. >>> b = 14
  17. >>> b % 3
  18. 2
  19. >>> b %= 3
  20. >>> print(b)
  21. 2
  22. >>> b **= 3
  23. >>> print(b)
  24. 8
  25. >>> b //= 5
  26. >>> print(b)
  27. 1

NOTE: In python 2, you will get result equal to 8 instead of 8.4 in line 15.

3) Comparison Operators:

Comparison operators are used to compare one operand to another. These compares two operands and return true if comparison is right otherwise false.

OperatorsDescriptors
== both are equal
!=both are not equal
<left is smaller
>right is smaller
<=left is smaller or equal
>=right is smaller or equal
<> similar to !=

3) Bitwise Operators:

Bitwise operators performs operations on individual bits in an integer. Bitwise operators are & (and), | (or), << (left sift) , >> (right sift), ^ (XOR) and ~ (NOR).

Example: 14 & 3 = 2 (00001110 & 00000011 = 00000010 these numbers are in binary).

4) Logical Operators:

Logical operators are similar to 'and' and 'or' logical gates. You can use 'or' and 'and' for these gates.

  1. >>> 2 and 2
  2. 2
  3. >>> 2 and 3
  4. 3
  5. >>> 2 and 0
  6. 0
  7. >>> 0 and 3
  8. 0
  9. >>> 2 or 3
  10. 2
  11. >>> 3 or 2
  12. 3
  13. >>> 0 or 2
  14. 2
  15. >>> 2 or 0
  16. 2

5) Membership Operators:

There are two member operators 'in' and 'not in'. These are to check if a variable is present in a sequence, such as strings, lists, or tuples.

  1. >>> sequence = [2, 2.3, 'name', 'harish', 'number']
  2. >>> 2 in sequence
  3. True
  4. >>> 3 in sequence
  5. False
  6. >>> a = 'string'
  7. >>> 't' in a
  8. True
  9. >>> 'p' in a
  10. False
  11. >>> 'p' not in a
  12. True

6) Identity Operators:

There are two identity operators 'is' and 'is not'. These are to check if both objects are identical or not.

  1. >>> a = 12
  2. >>> b = 12
  3. >>> a is b
  4. True
  5. >>> b is a
  6. True
  7. >>> a is 12
  8. True
  9. >>> b = 13
  10. >>> a is b
  11. False
  12. >>> b is 13
  13. True
  14. >>> 12 is 12
  15. True
  16. >>> 12 is not 13
  17. True

In the next tutorial we will read about Decision Making Statements, i.e., if and if else statements.
Don't forget to Subscribe for getting latest Tutorials directly in inbox.
[000webhost_bottom_banner]



About Harish Kumar

Harish is an interested person in the field of web development and blogging. He works for the need of young web developers in learning various languages, latest technologies and other essential tips and tricks. If you need some help or you have some suggestion then you email him at harish@findalltogether.com without any hesitation. You can also suggest/demand for articles of your own choice.

Related Articles

In this tutorial, we will write our first python program. We will start from python interpreter and also learn how to...
First Python Program
Python is a general purpose, high-level, interpreted, interactive and object-oriented language. Easy to learn, read ...
Introduction to Python
Python has five standard data types: Numbers, Strings, Lists, Tuple and Dictionary.Every data type has its own proper...
Variable and Data Types in Python

Login or Sign up to leave comment.