Whenever somebody talks about Operators, our mind always stuck at Addition (+), Multiplication (*) and other operators. But can anyone thinks that “What is Operator”, how can you define “Operator”? You also know that every language supports a lot of operators. So, in this article we discuss about different types of operator supported by Python.
Operators :
- Operators are required to perform various operation on data.
- They are special symbols that are required to trigger the operation/action on data.
- Operator’s behavior depends on data type, i.e. same operator can behave differently on different data types.
- The values on which the operator operates called operands.
Types of Operators:
Unary Operators :
- The operator that act one operand are referred to as Unary Operator.
- Basically, Unary operator contains 1 operand and 1 operator.
- Examples : + 4, – 5
Binary Operators:
- Operators that act upon two operands are refers to as Binary Operators.
- Binary operators further classified into many operators like : Arithmetic Operators, Logical Operators, Relational Operators etc.
Arithmetic Operators:
- Arithmetic operators are used to perform mathematical functions such as addition, subtraction, division, multiplications etc.
Different types of Arithmetic operators supported by python are :
Precedence of Arithmetic Operators :
When more than one arithmetic operator appears in an expression the operations will execute in a specific order. In python the arithmetic operator precedence follows as per the acronym PEMDAS is :
Relational Operators:
- Relational operators are also known as conditional or comparison operators.
- Relational operators are used for comparing two expression and result in either True or False.
- Basically, relational operators determine the relation among different operands.
- These operators are used in conditional programing with if, elif, while etc.
Different types of Relational Operators supported by Python are
Assignment Operators:
- Assignment operators are used to assigning values to a variables.
- You can use assignment operator with other operators then it is called Augmented Assignment Operator and Shorthand Assignment Operator.
Different types of assignment operators supported by Python are :
Logical Operators:
- Logical operators are used in control statements when you want to check more than one expression in a single statement.
- They return the result as True or False.
- Python supported 3 types of logical operators i.e. and, or, not.
Membership Operators:
- Membership operators are used to check if a value exists in a sequence or not.
- They return the result as True or False.
- There are 2 membership operators in python :
- in : returns True if a values is found in a sequence.
- not in : returns True if a value is not found in a sequence.
Examples of Membership Operators:
Identity Operators:
- Identity operators are used to check whether two values are on the same part of the memory or not.
- They return the result as True or False.
- There are 2 membership operators in python :
- is : returns True if two operands are identical.
- is not: returns True if two operands are not identical.
Examples of Identity Operators:
Bitwise Operators:
- Bitwise operators are work on bits and perform bit by bit operations.
- It firsts covert the number into binary (bits form) then working on bits.
- There are 6 bitwise operators in Python :
Examples of Bitwise Operators :
Operator Precedence :
When an expression or statement involves multiple operators then it will be resolve using the following operator precedence chart :
Expressions :
- Any logical sentence of code that we write during programming, is called expression.
- An expression is valid combination of operators and operands.
Examples of expressions:
Expressions Example