Python - Operators
Operators are used to do action or process on variables and operands. The operator usually represents as symbol for doing an action. Python has different type of operators like Arithmetic, Assignment, Relational / Comparison , Logical, Bitwise, Identity and Membership operators. Every operators are using to do different kind of operations.
In this tutorial we will learn about Logical Operators, Bitwise Operators, Identity Operators, Membership Operators and Ternary operator with an exmple and an output. We will learn one by one.
Python Type of Operators
Python operators are classifying the following categories. We will learn one by one
- Arithmetic Operators
- Relational Operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Identity Operators
- Membership Operators
Python Arithmetic Operators
Python Arithmetic operators are used to perform the mathematical operations.
Operator | Description | Syntax | Example |
---|---|---|---|
+ | Adds the values of two operands | var1 + var2 | a + b |
- | subtracts the values of two operands | var1-var2 | a - b |
* | Multiplies the values of two operands | var1 * var2 | a * b |
/ | Divides the values of two operands | var1/var2 | a / b |
% | Modulus - Returns the remainder of first operand divided by second operand | var1%var2 | a % b |
** | Exponentiation - Returns the first operand power of second operand | var1 ** var2 | a ** b |
// | Floor Division - Divides the values of two operands and returns the integer value | var1//var2 | a//b |
Python Relational Operators
Python Relational operators are used to compare the values between two operand. it returns either true or false.
Operator | Description | Syntax | Example |
---|---|---|---|
== | If both operands are equal, It returns true otherwise false | var1 == var2 | a == b |
!= | If both operands are not equal, It returns true otherwise false | var1 != var2 | a != b |
> | If first operand greater than second operand, It returns true otherwise false | var1 > var2 | a > b |
< | If first operand less than second operand, It returns true otherwise false | var1 < var2 | a < b |
>= | If first operand greater than and equal to second operand, It returns true otherwise false | var1 >= var2 | a >= b |
<= | If first operand less than and equal second operand, It returns true otherwise false | var1 <= var2 | a <= b |
Python Assignment Operators
Python Assignment Operators are used to assign the values to the variables.
Operator | Description | Syntax | Example |
---|---|---|---|
= | assigns the value from one operand to another operand or mathematical operation |
result = var1 result = var1 (Arithmetic Symbol) var2 |
a = 5 c = a + b |
+= | Adds and assigns that value to the first operand | var1 += var2 | a += b is used for a = a + b |
-= | subtracts and assigns that value to the first operand | var1 -= var2 | a -= b is used for a = a - b |
*= | Multiplies and assigns that value to the first operand | var1 *= var2 | a *= b is used for a = a * b |
/= | Divides and assigns that value to the first operand | var1 /= var2 | a /= b is used for a = a / b |
%= | Modulus - Returns the remainder of first operand divided by second operand and assigns to first operand | var1 %= var2 | a %= b is used for a = a % b |
**= | Exponentiation - Returns the first operand power of second operand and assigns to first operand | var1 **= var2 | a **= b is used for a = a ** b |
//= | Floor Division - Divides the values of two operands and returns the integer value and assigns to first operand | var1 //= var2 | a //= b is used for a = a // b |