In previous tutorial we have seen the basics of python that is the playing with numbers ,literals,constants etc. but now in this tutorial we will learn about what are operators , what are their types ,how they are operated in python .
What do you mean by operator :
In Python, we can defined operators as the special symbols that designate some sort of computation should be performed. The values that an operator acts on are called operands.
>>> a =1
>>> b = 2
>>> a + b
As it is cleared by its name that , Arithmetic Operators are used for Arithmetic operations..
>>>x = 5
>>>y = 2
here addition (+) or unary plus is used for arithmetic addition.
here subtraction (-) subtract right operand from left
here multiplication (*) multiply two numbers
here Division (/) Divides left operand by the right one>
here modulus (%) takes the remainder
here Floor division (//) Division that results into the whole number truncating digits after decimal point?>>>x**y
here Exponent (**) – left operand raised to the power of right
Relational Operator or Comparison Operator
Relational or Comparison Operators are used to compare the operands on the either side of them.
- Greater than (>):
Example : x > y will return true if x is greater than y
- Less than (<):
Example : x < y will return true if x is less than y
- Equal to (==):
Example: x == y will return true if x is equal to y
- Not equal to (!=):
Example x != y will return true if x is not equal to y
- Greater Than or Equal to (>=) :
Example : x >= y will return true if x is greater than or equal to y
- Less Than or Equal to (<=):
Example : x <= y will return true if x is less than or equal to y
In python , there are three logical operators:
- and : (x and y) will return true if x and y both are true
>>>(x>2 and y>1)
- or :(x or y) will return true if either of x or y is true
>>> (x<2 or y<1)
- not : (not x) will return true if x is false or will return false if x is true. not reverses or complements the operands
As simple as it sounds assignment operators are used for assigning values to variables.
In python we have many assignment operators such as =,+=,-=,,/=,%=,*=, **=,//=
Bitwise take binary digits as operands and perform bit by bit operations.
Following is the list of bitwise supported in Python.
- >>>a = 0101
- >>>b = 0111
there are so many bitwise here
- & (Binary AND)
- | (Binary OR)
- ^ (Binary XOR)
- ~ (Binary NOT)
- >> (Binary right shift)
- << (Binary left shift)
>>> print (a ^ b)
>> >print(a >> 1)
>>>print(b << 1)
In Python membership operator is used to check whether a variable is a member of a sequence such as String, List, Tuple and Dictionary. In the Python dictionary, we can check the membership of key only, not the value.
Here in Python we have in , not in
- in :- True if x is the member of y
- not in :- True if x is not the member of y
>>>print(y in x)
>>>print(x not in y)
Python identity operators are used to check if the operands have identical memory location. In simple language, it compares the memory location of two objects and returns True if both objects have identical or same memory location.
Here we have in Python is , not is
- is :- True if both variables point to same memory location .
- not is :-True if both variable point separate memory location .
>>>print (x is y)
>>>print ( x is not z)
Here is the list of Python Operators in descending order, listing from higher precedence to lower precedence.
- ( ) :- parenthesis
- ** :- exponents
- +, -, ~ :- Unary plus, Unary minus and Bitwise NOT
- *, /, %, // :- Multiplication, Division, Modulus and Floor Division
- +, – :- Addition and Subtraction
- >>, << :- bitwise right shift ant left shift
- & :- bitwise and
- ^, | :- :- bitwise xor and or
- <=, <, >, >= :- comparison operators
- ==, != :- Equality Operators
- =, %=, /=, //=, -=, +=, *=, **= :- Assignment Operators
- is, is not :- Identity Operators
- in, not in :- Membership Operators
- not, or, and :- Logical Operators