Python operators allow us to do common processing on variables. We will look into different types of python operators with examples and also operator precedence. Previously we learnt about Python Input, Output and Import. You can find it from
Python Input, Output and Imports.
Python Operators
Python Operators are the special symbols that can manipulate values of one or more operands.
Python Operator Types
Python operators can be classified into several categories such as arithmetic, logical, comparison, bitwise and assignment. Let’s look into these with simple python programs.
Python Arithmetic Operators
#create two variable having two value a=100 b=200 #addition (+) operator print(a+b) #subtraction () operator print(ab) #multiplication (*) operator print(a*b) #division (/) operator print(b/a) #modulus (%) operator print(a%b) # prints the remainder of a/b #exponent (**) operator print(a**b) #prints a^b
Python Comparison Operators
#create two variable having two value a=100 b=200 # (==) operator, checks if two operands are equal or not print(a==b) # (!=) operator, checks if two operands are not equal print(a!=b) # (>) operator, checks left operand is greater than right operand or not print(a>b) # (<) operator, checks left operand is less than right operand or not print(a<b) #(>=) operator, checks left operand is greater than or equal to right operand or not print(a>=b) # (<=) operator, checks left operand is less than or equal to right operand or not print(a<=b)
Python Bitwise Operators
#create two variable having two value a=10 # binary 1010 b=7 # binary 0111 # Binary AND (&) operator, done binary AND operation print(a&b) # Binary OR () operator, done binary OR operation print(ab) # Binary XOR (^) operator, done binary XOR operation print(a^b) # Binary ONEs Compliment (~) operator, done binary Ones Compliment operation print(~a) # Binary Left Shift (<<) operator, done binary Left Shift operation print(a<<) # Binary Right Shift (>>) operator, done binary Right Shift operation print(a>>1)
Python Logical Operators
#take a variable a=int(input()) # logical AND operation if a%4==0 and a%3==0: print("divided by both 4 and 3") # logical OR operation if a%4==0 or a%3==0: print("either divided by 4 or 3") # logical NOT operation if not(a%4==0 or a%3==0): print("neither divided by 4 nor 3")
This particular code shows the below output when we give input 24
Also this code shows the below output when we give input 17
Python Assignment Operators
# take two variable, assign values with assignment operators a=3 b=4 print("a: "+str(a)) print("b: "+str(b)) # it is equivalent to a=a+b a+=b print("a: "+str(a)) print("b: "+str(b)) # it is equivalent to a=a*b a*=b print("a: "+str(a)) print("b: "+str(b)) # it is equivalent to a=a/b a/=b print("a: "+str(a)) print("b: "+str(b)) # it is equivalent to a=a%b a%=b print("a: "+str(a)) print("b: "+str(b)) # it is equivalent to a=a**b ( exponent operator) a**=b print("a: "+str(a)) print("b: "+str(b)) # it is equivalent to a=a//b ( floor division) a//=b print("a: "+str(a)) print("b: "+str(b))
Python Operator Precedence
Precedence of python operators means the priority level of operators. This becomes vital when an expression has multiple operators in it. For example consider the following expression
>>> 2+3*4
Now, what do you think the series of operation would be? We can add 2 and 3, then multiply the result by 4. Also we can multiply 3 and 4 first, then add 2 with it. Here we can see precedence is important.
So below is a list of operators indicating the precedence level. It’s in descending order. That means upper group has more precedence than that of lower group

Parenthesis –
()

Exponentiation –
**

Compliment, unary plus and minus –
~
,+
,

Multiply, Divide, modulo –
*
,/
,%

Addition and Subtraction –
+
,

Right and Left Shift –
>>
,<<

Bitwise AND –
&

Bitwise OR and XOR –

,^

Comparison Operators –
==
,!=
,>
,<
,>=
,<=

Assignment Operator
=
Well, that’s brief about the python operators and precedence. You can find more about the operators from the Official Python Documentation
#happy_coding 🙂
Leave a Reply