0% found this document useful (0 votes)
2 views9 pages

Python Operators Precedence

The document provides a comprehensive overview of Python operators, detailing their precedence and types, including arithmetic, comparison, assignment, logical, bitwise, membership, and identity operators. It includes examples demonstrating how operator precedence affects expression evaluation and showcases various operators with corresponding code snippets. The document serves as a reference for understanding how to effectively use operators in Python programming.

Uploaded by

myindiansun2024
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Python Operators Precedence

The document provides a comprehensive overview of Python operators, detailing their precedence and types, including arithmetic, comparison, assignment, logical, bitwise, membership, and identity operators. It includes examples demonstrating how operator precedence affects expression evaluation and showcases various operators with corresponding code snippets. The document serves as a reference for understanding how to effectively use operators in Python programming.

Uploaded by

myindiansun2024
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python Operators Precedence

The following table lists all Python Operators from highest precedence to lowest.

Operator Description

** Exponentiation (raise to the power)

Complement, unary plus and minus (method names for the


~+- last two are +@ and -@)

* / % // Multiply, divide, modulo and floor division

+- Addition and subtraction

>> << Right and left bitwise shift

& Bitwise 'AND'

^| Bitwise exclusive `OR' and regular `OR'

<= < > >= Comparison operators

<> == != Equality operators

= %= /= //= -= +=
Assignment operators
*= **=

is is not Identity operators

in not in Membership operators

not or and Logical operators

Operator precedence affects how an expression is evaluated.

For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher
precedence than +, so it first multiplies 3*2 and then adds into 7.

Here, operators with the highest precedence appear at the top of the table, those with the
lowest appear at the bottom.

Example

#!/usr/bin/python
a = 20
b = 10
c = 15
d=5
e=0

e = (a + b) * c / d #( 30 * 15 ) / 5
print ("Value of (a + b) * c / d is ", e)

e = ((a + b) * c) / d # (30 * 15 ) / 5
print ("Value of ((a + b) * c) / d is ", e)

e = (a + b) * (c / d); # (30) * (15/5)


print ("Value of (a + b) * (c / d) is ", e)

e = a + (b * c) / d; # 20 + (150/5)
print ("Value of a + (b * c) / d is ", e)

When you execute the above program, it produces the following result −

Value of (a + b) * c / d is 90
Value of ((a + b) * c) / d is 90
Value of (a + b) * (c / d) is 90
Value of a + (b * c) / d is 50

Python - Operators

Python language supports the following types of operators.

 Arithmetic Operators
 Comparison (Relational) Operators
 Assignment Operators
 Logical Operators
 Bitwise Operators
 Membership Operators
 Identity Operators

Python - Arithmetic Operators

Following is the table which lists down all the arithmetic operators available in Python:

Operator Name Example

+ Addition a + b = 30

- Subtraction a – b = -10

* Multiplication a * b = 200

/ Division b/a=2
% Modulus b%a=0

** Exponent a**b =10**20

// Floor Division 9//2 = 4

Program
a=10
b=20
print ("Add of two integers")
print ("a =",a,"b =",b,"addition =",a+b)
print ("Sub of two integers")
print ("a =",a,"b =",b,"Sub =",a-b)
print ("Mul of two integers")
print ("a =",a,"b =",b,"Mul =",a*b)
print ("Div of two integers")
print ("a =",a,"b =",b,"Dvi =",a/b)
print ("Mod of two integers")
print ("a =",a,"b =",b,"Mod =",a%b)
print ("Exp of two integers")
print ("a =",a,"b =",b,"Exp =",a**b)
print ("Floor of two integers")
print ("a =",a,"b =",b,"Floor =",a//b)

'''output
Add of two integers
a = 10 b = 20 addition = 30
Sub of two integers
a = 10 b = 20 Sub = -10
Mul of two integers
a = 10 b = 20 Mul = 200
Div of two integers
a = 10 b = 20 Dvi = 0.5
Mod of two integers
a = 10 b = 20 Mod = 10
Exp of two integers
a = 10 b = 20 Exp = 100000000000000000000
Floor of two integers
a = 10 b = 20 Floor = 0
'''
Python - Comparison Operators

Comparison operators in Python are very important in Python's conditional statements (if,
else and elif) and looping statements (while and for loops). The comparison operators also
called relational operators. Some of the well known operators are "<" stands for less than, and
">" stands for greater than operator.

< Less than a<b

> Greater than a>b

<= Less than or equal to a<=b

>= Greater than or equal to a>=b

== Is equal to a==b

!= Is not equal to a!=b


Program
a=5
b=7
print ("a=",a, "b=",b, "a>b is", a>b)
print ("a=",a, "b=",b,"a<b is",a<-b)
print ("a=",a, "b=",b, "a>b is", a>=b)
print ("a=",a, "b=",b,"a<b is",a<b)
print ("a=",a, "b=",b,"a==b is",a==b)
print ("a=",a, "b=",b,"a!=b is",a!=b)

'''
Output
a= 5 b= 7 a>b is False
a= 5 b= 7 a<b is False
a= 5 b= 7 a>b is False
a= 5 b= 7 a<b is True
a= 5 b= 7 a==b is False
a= 5 b= 7 a!=b is True
'''

Python - Assignment Operators

The = (equal to) symbol is defined as assignment operator in Python. The value of Python
expression on its right is assigned to a single variable on its left. The = symbol as in
programming in general (and Python in particular) should not be confused with its usage in
Mathematics, where it states that the expressions on the either side of the symbol are equal.

Operator Description Example

Assigns values from right side operands c = a + b assigns


=
to left side operand value of a + b into c
It adds right operand to the left operand c += a is equivalent
+= Add AND
and assign the result to left operand to c = c + a

It subtracts right operand from the left


c -= a is equivalent
-= Subtract AND operand and assign the result to left
to c = c - a
operand

It multiplies right operand with the left


c *= a is equivalent
*= Multiply AND operand and assign the result to left
to c = c * a
operand

It divides left operand with the right


c /= a is equivalent
/= Divide AND operand and assign the result to left
to c = c / a
operand

It takes modulus using two operands and c %= a is equivalent


%= Modulus AND
assign the result to left operand to c = c % a

Performs exponential (power) calculation


**= Exponent c **= a is equivalent
on operators and assign value to the left
AND to c = c ** a
operand

It performs floor division on operators c //= a is equivalent


//= Floor Division
and assign value to the left operand to c = c // a
Program

a=10
b=5
print ("Augmented addition of int and int")
a+=b # equivalent to a=a+b
print ("a=",a, "type(a):", type(a))

a=10
b=5.5
print ("Augmented addition of int and float")
a+=b # equivalent to a=a+b
print ("a=",a, "type(a):", type(a))

a=10.50
b=5+6j
print ("Augmented addition of float and complex")
a+=b #equivalent to a=a+b
print ("a=",a, "type(a):", type(a))
a-=b #equivalent to a=a-b
print ("a=",a)
a*=b #equivalent to a=a*b
print ("a=",a)
a/=b #equivalent to a=a/b
print ("a=",a)
a%=b #equivalent to a=a%b
print ("a=",a)
a**=b #equivalent to a=a**b
print ("a=",a)
a//=b #equivalent to a=a//b
print ("a=",a)

Python Bitwise Operators


Bitwise operator works on bits and performs bit by bit operation. Assume if a = 60; and b =
13; Now in the binary format their values will be 0011 1100 and 0000 1101 respectively.
Following table lists out the bitwise operators supported by Python language with an example
each in those, we use the above two variables (a and b) as operands −
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
There are following Bitwise operators supported by Python language

Operator Description Example

Operator copies a bit to the


& Binary AND result if it exists in both (a & b) (means 0000 1100)
operands

It copies a bit if it exists in (a | b) = 61 (means 0011


| Binary OR
either operand. 1101)

It copies the bit if it is set in (a ^ b) = 49 (means 0011


^ Binary XOR
one operand but not both. 0001)

~ Binary Ones It is unary and has the effect (~a ) = -61 (means 1100 0011
Complement of 'flipping' bits. in 2's complement form due
to a signed binary number.

The left operands value is


moved left by the number of a << 2 = 240 (means 1111
<< Binary Left Shift
bits specified by the right 0000)
operand.

The left operands value is


moved right by the number a >> 2 = 15 (means 0000
>> Binary Right Shift
of bits specified by the right 1111)
operand.

Python Bitwise operators

Bitwise operators act on operands as if they were strings of binary digits. They operate bit by
bit, hence the name.

For example, 2 is 10 in binary, and 7 is 111.


In the table below: Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)

Operator Meaning Example

& Bitwise AND x & y = 0 (0000 0000)

| Bitwise OR x | y = 14 (0000 1110)

~ Bitwise NOT ~x = -11 (1111 0101)

^ Bitwise XOR x ^ y = 14 (0000 1110)

>> Bitwise right shift x >> 2 = 2 (0000 0010)

<< Bitwise left shift x 0010 1000)

Python Logical Operators

There are following logical operators supported by Python language. Assume variable a holds
10 and variable b holds 20 then
Operator Description Example

If both the operands are true then


and Logical AND (a and b) is true.
condition becomes true.

If any of the two operands are non-zero


or Logical OR (a or b) is true.
then condition becomes true.

Used to reverse the logical state of its


not Logical NOT Not(a and b) is false.
operand.
program
x=5
print(x > 3 or x < 4)
print(x > 3 and x < 4)
print(not(x > 3 and x < 10))

Output
True
False
False

Python Membership Operators

Python's membership operators test for membership in a sequence, such as strings, lists, or
tuples. There are two membership operators as explained below −

Operator Description Example

Evaluates to true if it finds a variable in the x in y, here in results in a 1 if x is a


in
specified sequence and false otherwise. member of sequence y.

Evaluates to true if it does not finds a


x not in y, here not in results in a 1
not in variable in the specified sequence and false
if x is not a member of sequence y.
otherwise.

program
var = "TutorialsPoint"
a = "P"
b = "tor"
c = "in"
d = "To"
print (a, "in", var, ":", a in var)
print (b, "not in", var, ":", b not in var)
print (c, "in", var, ":", c in var)
print (d, "not in", var, ":", d not in var)
It will produce the following output −
P in TutorialsPoint : True
tor not in TutorialsPoint : False
in in TutorialsPoint : True
To not in TutorialsPoint : True

Python Identity Operators

Identity operators compare the memory locations of two objects. There are two Identity
operators explained below −

Operator Description Example

Evaluates to true if the variables on either side


x is y, here is results in 1 if
is of the operator point to the same object and
id(x) equals id(y).
false otherwise.

Evaluates to false if the variables on either x is not y, here is not results


is not side of the operator point to the same object in 1 if id(x) is not equal to
and true otherwise. id(y).
Program

a="TutorialsPoint"
b=a
print ("id(a), id(b):", id(a), id(b))
print ("a is b:", a is b)
print ("b is not a:", b is not a)
It will produce the following output −
id(a), id(b): 2739311598832 2739311598832
a is b: True
b is not a: False

You might also like