Python Operators

In this tutorial you will learn about the Python Operators and its application with practical example.

Python Operators

In Python programming, an operator is a special symbol that is used to carry out some specific operation on its operand. In Python, we have rich set of built in operators to carry out different type of operations. There are operators for assignment, arithmetic operations, logical operations and comparison operations etc. Python operators can be used with many types of variables or constants, but some of the operators are restricted to work on specific data types. Most operators are binary, meaning they take two operands, but a few are unary and only take one operand.

Type of operators in Python

Python supports the following types of operators –

  • Arithmetic Operators
  • Assignment Operators
  • Comparison (Relational) Operators
  • Logical Operators
  • Identity Operators
  • Bitwise Operators
  • Membership Operators

Arithmetic Operators In Python

Arithmetic Operators are used to perform arithmetic operations like addition, subtraction, multiplication, division, %modulus, exponent, etc. For example assume a=5 and b=10, then –

Arithmetic operators in Python
Symbol Name Description Example
+ Addition It perform the addition of both the operands. a + b = 15
Subtraction It subtract right operand from the left a – b = -5
* Multiplication It performs the multiplication of the given operands a * b = 50
/ Division It performs the division of the left operand by the right operand b / a = 2
% Modulus It returns the remainder of the division of left operand by the right b % a = 0(remainder of b/a)
// Floor Division It performs the division of operands where it returns the quotient in which the digits after the decimal point are removed. 15 // 4 = 3, 19//5=3
** Exponent Left operand raised to the power of right a**b = (a to the power b), 3**4=81

Assignment Operators In Python

Assignment operators are used to assign value to a variable, you can assign a variable value or the result of an arithmetical expression.

Assignment operators in Python
Symbol Name Description Example
= Assignment Used to assigns a value to variable(s). c = a + b assigns value of a + b into c
+= Add and assign It is shorthand to add right operand to the left operand and assign the result to left operand c += a is equivalent to c = c + a
-= Subtract and assign It is shorthand to subtracts right operand from the left operand and assign the result to left operand c -= a is equivalent to c = c – a
*= Multiply and assign It is shorthand to multiply right operand with the left operand and assign the result to left operand c *= a is equivalent to c = c * a
/= Divide and assign It is shorthand to divide left operand with the right operand and assign the result to left operand c /= a is equivalent to c = c / ac /= a is equivalent to c = c / a
%= Modulus and assign It is shorthand to take modulus using two operands and assign the result to left operand c %= a is equivalent to c = c % a
**= Exponent and assign It is shorthand to perform exponential (power) calculation on operators and assign value to the left operand c **= a is equivalent to c = c ** a
//= Floor Division and assign It is shorthand to performs floor division on operators and assign value to the left operand c //= a is equivalent to c = c // a

Comparison (Relational) Operators In Python

Comparison Operators are used evaluate a comparison between two operands. The result of a comparison operation is a Boolean value that can only be true or false. Comparison Operators are also referred as relational operators.

Here is full list of Relational operators available in Python Programming Language –

Relational operators in Python
Operator Meaning Example Description
== Equal to a == b 1 if a equal to b; 0 otherwise
!= Not equal to a != b 1 if a not equal to b; 0 otherwise
> greater than a > b 1 if a > b; 0 otherwise
< less than a < b 1 if a < b; 0 otherwise
>= greater than or equal to a >= b 1 if a >= b; 0 otherwise
<= less than or equal to a <= b 1 if a <= b; 0 otherwise

Logical Operators In Python

Logical operators are used to combine expressions with conditional statements using logical (AND,OR,NOT) operators, which results in true or false.

There are 3 logical operators available in Python Programming Language

Logical operators in Python
Operator Meaning Example Description
and Logical AND a and b Logical AND of a and b
or Logical OR a or b Logical OR of a and b
not Logical NOT not a Logical NOT of a

Identity Operators In Python

Identity Operators are used to compare the memory location of two objects.
There are two identify operators available in Python (is, is not).

Identity operators in Python
Operator Name Meaning Example
is Name It returns True if both the operands are identical and refers to the same object x is True
is not Name It returns True if both the operands are not identical or they refer to different objects x is not True

Bitwise Operators In Python

Bitwise operator are used to perform bit level operation over its operand.

Let A = 60; and B = 13;

Binary equivalent

A = 0011 1100

B = 0000 1101

Bitwise operators in Python
Operator Meaning Example Description
& Binary AND (A & B) It returns 12 which is 0000 1100
| Binary OR (A | B) It returns 12 which is 0000 1100
^ Binary XOR (A ^ B) It returns 49 which is 0011 0001
~ Ones Complement (~A ) It returns -60 which is 1100 0011
<< shift left A << 2 It returns 240 which is 1111 0000
>> shift right A >> 2 It returns 15 which is 0000 1111

 

Membership Operators In Python

Membership operators are used to test membership in a sequence, such as strings, lists, or tuples.
There are two membership operators available in python.

Membership operators in Python
Operator Name Meaning Example
in Name It returns True if value/variable is found in the given sequence, FALSE otherwise 5 in x
not in Name It returns True if value/variable is not found in the given sequence, FALSE otherwise 5 not in x

Python Operators Precedence

Highest precedence at top, lowest at bottom.
Operators in the same box evaluate left to right.

Operator precedence rule in Python
Operators Meaning
() Parentheses
** Exponent
+x, -x, ~x Unary plus, Unary minus, Bitwise NOT
*, /, //, % Multiplication, Division, Floor division, Modulus
+, – Addition, Subtraction
<<, >> Bitwise shift operators
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
==, !=, >, >=, <, <=, is, is not, in, not in Comparisions, Identity, Membership operators
not Logical NOT
and Logical AND
or Logical OR

In this tutorial we have learn about the Python Operators and its application with practical example. I hope you will like this tutorial.