In this article, I am going to discuss Operators in Python with Examples. In programming languages, an operator is a symbol that is applied to some operands i.e. variables, to perform certain operations on it.
Types Of Operators in Python:
Following is the summary of Python operators:
- Arithmetic Operators: The operators which are used to perform arithmetic operations like addition, subtraction, division etc. They are: +, -, *, /, %, **, //
- Relational Operators: The operators which are used to check for some relation like greater than or less than etc. between operands are called relational operators. They are: <, >, <=, >=, ==, !=
- Logical Operators: The operators which do some logical operation on the operands and return True or False are called logical operators. The logical operations can be ‘AND’, ‘OR’ etc.
- Assignment Operators: The operators which are used for assigning values to variables. ‘=’ is the assignment operator.
- Unary Minus Operator: The operator ‘-’ is called the Unary minus operator. It is used to negate the number.
- Membership Operators: The operators that are used to check whether a particular variable is part of another variable or not. They are ‘in’ and ‘not in’
- Identity Operators: The operators which are used to check for identity. They are ‘is’ and ‘is not’
Examples of Python Operators:
Arithmetic operators are used with numeric values to perform common mathematical operations:
x + y # Addition
x - y # Subtraction
x * y # Multiplication
x / y # Division
x % y # Modulus
x ** y # Exponentiation
x // y # Floor division
x == y # Equal
x != y # Not equal
x > y # Greater than
x < y # Less than
x >= y # Greater than or equal to
x <= y # Less than or equal to
x < 5 and x < 10 # Returns True if both statements are true
x < 5 or x < 4 # Returns True if one of the statements is true
not(x < 5 and x < 10) # Reverse the result, returns False if the result is true
x is y # Returns True if both variables are the same object
x is not y # Returns True if both variables are not the same object
x in y # Returns True if a sequence with the specified value is present in the object
not in y #Returns True if a sequence with the specified value is not present in the object
x & y # Sets each bit to 1 if both bits are 1
x | y # Sets each bit to 1 if one of two bits is 1
x ^ y # Sets each bit to 1 if only one of two bits is 1
~x # Inverts all the bits
x << 2 # Shift left by pushing zeros in from the right and let the leftmost bits fall off.
x >> 2 # Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off.
Operator Precedence
Operator precedence describes the order in which operations are performed. Parentheses has the highest precedence, meaning that expressions inside parentheses must be evaluated first and then outer.
print((2 + 3) - (2 + 3))
- Python tutorials
- Input and Output in Python
- f-strings in Python
- end and sep in Python
- Python QuickStart
- Variables in Python
- Data Types in Python
- Operators in Python
- Python program to compute net run rate
- Python program to get biggest number
- Python program to count the frequency of every element
- Python program of list and swap its content
- Python program to get max numbers
- Python program to generate patterns
- Python Program to Check ArmstrongÂ
- Python program to print multiplication table
- Python program for domestic electricity billÂ
- Python program to find maximum numberÂ
- Python program to check uppercase or lowercase letterÂ