Python Operators - Arithmetic, Logical, Comparison, Assignment, Bitwise & Precedence
Python Operators - Arithmetic, Logical, Comparison, Assignment, Bitwise & Precedence
Arithmetic Operators
Comparison Operators
Python Assignment Operators
Logical Operators or Bitwise Operators
Membership Operators
Identity Operators
Operator precedence
Arithmetic Operators
Arithmetic Operators perform various arithmetic calculations like addition, subtraction,
multiplication, division, %modulus, exponent, etc. There are various methods for
arithmetic calculation in Python like you can use the eval function, declare variable &
calculate, or call functions.
Example: For arithmetic operators we will take simple example of addition where we will
add two-digit 4+5=9
x= 4
y= 5
print(x + y)
Similarly, you can use other arithmetic operators like for multiplication(*), division (/),
substraction (-), etc.
Comparison Operators
/
These operators compare the values on either side of the operand and determine the
relation between them. It is also referred as relational operators. Various comparison
operators are ( ==, != , <>, >,<=, etc)
Example: For comparison operators we will compare the value of x to the value of y and
print the result in true or false. Here in example, our value of x = 4 which is smaller than y =
5, so when we print the value as x>y, it actually compares the value of x to y and since it is
not correct, it returns false.
x = 4
y = 5
print(('x > y is',x>y))
Likewise, you can try other comparison operators (x < y, x==y, x!=y, etc.)
Example: Python assignment operators is simply to assign the value, for example
num1 = 4
num2 = 5
print(("Line 1 - Value of num1 : ", num1))
print(("Line 2 - Value of num2 : ", num2))
We can also use a compound assignment operator, where you can add, subtract, multiply
right operand to left and assign addition (or any other arithmetic function) to the left
operand.
/
num1 = 4
num2 = 5
res = num1 + num2
res += num1
print(("Line 1 - Result of + is ", res))
Logical Operators
Logical operators in Python are used for conditional statements are true or false. Logical
operators in Python are AND, OR and NOT. For logical operators following condition are
applied.
For AND operator – It returns TRUE if both the operands (right side and left side) are
true
For OR operator- It returns TRUE if either of the operand (right side or left side) is
true
For NOT operator- returns TRUE if operand is false
Example: Here in example we get true or false based on the value of a and b
a = True
b = False
print(('a and b is',a and b))
print(('a or b is',a or b))
print(('not a is',not a))
Membership Operators
These operators test for membership in a sequence such as lists, strings or tuples. There
are two membership operators that are used in Python. (in, not in). It gives the result based
on the variable present in specified sequence or string
Example: For example here we check whether the value of x=4 and value of y=8 is available
in list or not, by using in and not in operators.
/
x = 4
y = 8
list = [1, 2, 3, 4, 5 ];
if ( x in list ):
print("Line 1 - x is available in the given list")
else:
print("Line 1 - x is not available in the given list")
if ( y not in list ):
print("Line 2 - y is not available in the given list")
else:
print("Line 2 - y is available in the given list")
Identity Operators
To compare the memory location of two objects, Identity Operators are used. The two
identify operators used in Python are (is, is not).
Operator is: It returns true if two variables point the same object and false otherwise
Operator is not: It returns false if two variables point the same object and true
otherwise
** Exponent
+, - Addition, Subtraction
/
= %= /= //= -= += *= **= Assignment Operators
Example:
x = 20
y = 20
if ( x is y ):
print("x & y SAME identity")
y=30
if ( x is not y ):
print("x & y have DIFFERENT identity")
Operator precedence
The operator precedence determines which operators need to be evaluated first. To avoid
ambiguity in values, precedence operators are necessary. Just like in normal multiplication
method, multiplication has a higher precedence than addition. For example in 3+ 4*5, the
answer is 23, to change the order of precedence we use a parentheses (3+4)*5, now the
answer is 35. Precedence operator used in Python are (unary + - ~, **, * / %, + - , &) etc.
v = 4
w = 5
x = 8
y = 2
z = 0
z = (v+w) * x / y;
print("Value of (v+w) * x/ y is ", z)
/
#Arithmetic Operators
x= 4
y= 5
print x + y
#Comparison Operators
x = 4
y = 5
print('x > y is',x>y)
#Assignment Operators
num1 = 4
num2 = 5
print ("Line 1 - Value of num1 : ", num1)
print ("Line 2 - Value of num2 : ", num2)
#Logical Operators
a = True
b = False
print('a and b is',a and b)
print('a or b is',a or b)
print('not a is',not a)
#Membership Operators
x = 4
y = 8
list = [1, 2, 3, 4, 5 ];
if ( x in list ):
print "Line 1 - x is available in the given list"
else:
print "Line 1 - x is not available in the given list"
if ( y not in list ):
print "Line 2 - y is not available in the given list"
else:
print "Line 2 - y is available in the given list"
#Identity Operators
x = 20
y = 20
if ( x is y ):
print "x & y SAME identity"
y=30
if ( x is not y ):
print "x & y have DIFFERENT identity"
#Operator precedence /
v = 4
w = 5
x = 8
y = 2
z = 0
z = (v+w) * x / y;
print "Value of (v+w) * x/ y is ", z
Summary:
Operators in a programming language are used to perform various operations on values
and variables. In Python, you can use operators like
There are various methods for arithmetic calculation in Python as you can use the eval
function, declare variable & calculate, or call functions
Comparison operators often referred as relational operators are used to compare the
values on either side of them and determine the relation between them
Python assignment operators are simply to assign the value to variable
Python also allows you to use a compound assignment operator, in a complicated
arithmetic calculation, where you can assign the result of one operand to the other
For AND operator – It returns TRUE if both the operands (right side and left side) are
true
For OR operator- It returns TRUE if either of the operand (right side or left side) is true
For NOT operator- returns TRUE if operand is false
There are two membership operators that are used in Python. (in, not in).
It gives the result based on the variable present in specified sequence or string
The two identify operators used in Python are (is, is not)
It returns true if two variables point the same object and false otherwise
Precedence operator can be useful when you have to set priority for which calculation
need to be done first in a complex calculation.
Next (/functions-in-python.html)
Python Tutorials
5) Learn Python Main Function (/learn-python-main-function-with-examples-understand-
main.html)
/
(https://www.facebook.com/guru99com/)
(https://twitter.com/guru99com)
(https://www.linkedin.com/company/guru99/)
(https://www.youtube.com/channel/UC19i1XD6k88KqHlET8atqFQ)
(https://forms.aweber.com/form/46/724807646.htm)
About
About Us (/about-us.html)
Advertise with Us (/advertise-us.html)
Write For Us (/become-an-instructor.html)
Contact Us (/contact-us.html)
Career Suggestion
SAP Career Suggestion Tool (/best-sap-module.html)
Software Testing as a Career (/software-testing-career-
complete-guide.html)
Interesting
Books to Read! (/books.html)
Blog (/blog/)
Quiz (/tests.html)
eBook (/ebook-pdf.html)
Execute online
Execute Java Online (/try-java-editor.html)
Execute Javascript (/execute-javascript-online.html)
Execute HTML (/execute-html-online.html)
Execute Python (/execute-python-online.html)
/
(/terms-of-service.html) | Address: 4023 Kennett Pike
#50286 Wilmington, Delaware 19807, USA