Python Operators

Python Operators



In Python, an operator is a symbol used to perform arithmetical and logical operations. In other words, an operator can be defined as a symbol used to manipulate the value of an operand. Here, an operand is a value or variable on which the operator performs its task. For example, '+' is a symbol used to perform mathematical addition operation. 

Consider the expression a = 10 + 30 . Here, variable 'a', values '10' and '30' are known as Operands and the symbols '=' and '+' are known as Operators.


Types of Operators in Python

In Python, there is a rich set of operators, and they are classified as follows.
  1. Arithmetic Operators ( +, -, *, /, %, **, // )
  2. Assignment Operators ( =, +=, -=, *=, /=, %=, **=, //= )
  3. Comparison Operators ( <, <=, >, >=, ==, != )
  4. Logical Operators ( and, or, not )
  5. Identity Operators ( is, is not )
  6. Membership Operators ( in, not in )
  7. Bitwise Operators ( &, |, ^, ~, <<, >> )
Let's look at each type of operators in Python.

Arithmetic Operators in Python

In Python, the arithmetic operators are the operators used to perform a basic arithmetic operation between two variables or two values. The following table presents the list of arithmetic operations in Python along with their description.
To understand the example let's consider two variables a with value 10 and b with value 3.
Operator Meaning Description Example
+ Addition Performs mathematical addition on both side of the operator a + b = 13
- Subtraction Subtracts the right-hand operand from the left-hand operand a - b = 7
* Multiplication Multiplies values on both sides of the operator a * b = 30
/ Division Performs division by Dividing left-hand operand by right-hand operand a / b = 3.3333333333333335
% Modulus Performs division by Dividing left-hand operand by right-hand operand and returns remainder a % b = 1
** Exponent Performs exponential (power) calculation on operators a ** b = 1000
// Floor Division Performs division which the digits after the decimal point are removed. But if one of the operands is negative, the result is rounded away from zero (towards negative infinity) a // b = 3
Let's look at the following example code to illustration the arithmetic operators in Python.

Assignment Operators in Python

In Python, the assignment operators are the operators used to assign the right-hand side value to the left-hand side variable. The following table presents the list of assignment operations in Python along with their description.
To understand the example let's consider two variables a with value 10 and b with value 3.
Operator Meaning Description Example
= Assignment Assigns right-hand side value to left-hand side variable a = 10
+= Add and Assign Adds right operand to the left operand and assign the result to left operand a += b
≈ ( a = a + b )
-= Subtract and Assign Subtracts right operand from the left operand and assign the result to left operand a -= b
≈ ( a = a - b )
*= Multiply and Assign Multiplies right operand with the left operand and assign the result to left operand a *= b
≈ ( a = a * b )
/= Division and Assign Divides left operand with the right operand and assign the result to left operand a /= b
≈ ( a = a / b )
%= Modulus and Assign Takes modulus using two operands and assign the result to left operand a %= b
≈ ( a = a % b )
**= Exponent and Assign Performs exponential calculation on operators and assign value to the left operand a **= b
≈ ( a = a ** b )
//= Floor Division and Assign Performs floor division on operators and assign value to the left operand a //= b
≈ ( a = a // b )

Let's look at the following example code to illustration the assignment operators in Python.


Comparison Operators in Python

In Python, the comparison operators are used to compare two values. In other words, comparison operators are used to checking the relationship between two variables or values. The comparison operators are also known as Relational Operators. The following table presents the list of comparison operations in Python along with their description.
To understand the example let's consider two variables a with value 10 and b with value 3.
Operator Meaning Description Example
< Less than Returns True if left-hand side value is smaller than right-hand side value, otherwise returns False. a < b
(False)
<= Less than or Equal to Returns True if left-hand side value is smaller than or equal to right-hand side value, otherwise returns False. a <= b
( False )
> Greater than Returns True if left-hand side value is larger than right-hand side value, otherwise returns False. a > b
( True )
>= Graeter than or Equal to Returns True if left-hand side value is larger than or equal to right-hand side value, otherwise returns False. a >= b
( True )
== Equal to Returns True if left-hand side value is equal to right-hand side value, otherwise returns False. a == b
( False )
!= Not equal to Returns True if left-hand side value is not equal to right-hand side value, otherwise returns False. a != b
( True )
Let's look at the following example code to illustration the comparison operators in Python.

Logical Operators in Python

In Python, the logical operators are used to merge multiple conditions into a single condition. The following table presents the list of logical operations in Python along with their description.
To understand the example let's consider two variables a with value 10 and b with value 3.
Operator Meaning Description Example
and Logical AND Returns True if all the conditions are True, otherwise returns False. a < b and a > c
or Logical OR Returns False if all the conditions are False, otherwise returns True. a < b or a > c
not Logical NOT Returns True if the condition is False, otherwise returns False. not a > b
Let's look at the following example code to illustration the logical operators in Python.

Identity Operators in Python

In Python, the identity operators are used to compare the memory locations of two objects or variables. There are two identity operators in Python. The following table presents the list of identity operations in Python along with their description.
To understand the example let's consider two variables a with value 10 and b with value 3.
Operator Meaning Description Example
is is identical Returns True if the variables on either side of the operator point to the same object, otherwise returns False. a is b
is not is not identical Returns False if the variables on either side of the operator point to the same object, otherwise returns True. a is not b
Let's look at the following example code to illustration the identity operators in Python.

Membership Operators in Python

In Python, the membership operators are used to test whether a value is present in a sequence. Here the sequence may be String, List, or Tuple. There are two membership operators in Python. The following table presents the list of membership operations in Python along with their description.
To understand the example let's consider two variables a with value 10, b with value 3, and a list [1, 4, 10, 40, 5].
Operator Meaning Description Example
in in Returns True if it finds a variable in the specified sequence, otherwise returns False. a in list
not in not in Returns True if it does not finds a variable in the specified sequence, otherwise returns False. a not in list
Let's look at the following example code to illustration the membership operators in Python.

Bitwise Operators in Python

In Python, the bitwise operators are used to performs bit by bit operation. There are six bitwise operators in Python. The following table presents the list of bitwise operations in Python along with their description.
To understand the example let's consider two variables a with value 10 equalent binary value is 1010, b with value 15 equalent binary value is 1111.
Operator Meaning Description Example
& Bitwise AND Returns True if it finds a variable in the specified sequence, otherwise returns False. a in list
| Bitwise OR Returns True if it does not finds a variable in the specified sequence, otherwise returns False. a not in list
^ Bitwise Exclusive OR Returns True if it does not finds a variable in the specified sequence, otherwise returns False. a not in list
~ Bitwise Ones Complement Returns True if it does not finds a variable in the specified sequence, otherwise returns False. a not in list
<< Bitwise Left Shift Returns True if it does not finds a variable in the specified sequence, otherwise returns False. a not in list
>> Bitwise Right Shift Returns True if it does not finds a variable in the specified sequence, otherwise returns False. a not in list
Let's look at the following example code to illustration the bitwise operators in Python.

1 comment

File Uploading in PHP

  File Uploading in PHP PHP allow you to upload any type of a file i.e. image, binary or text files.etc..,PHP has one in built global variab...