0% found this document useful (0 votes)
2 views

python operator

The document provides an overview of Python operators, which are constructs that manipulate the values of operands. It categorizes operators into types such as Arithmetic, Comparison, Assignment, Logical, Bitwise, Membership, and Identity operators, detailing their functions and providing examples. The document also explains operator precedence and includes sample code snippets to illustrate the use of various operators.

Uploaded by

karanranvir061
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

python operator

The document provides an overview of Python operators, which are constructs that manipulate the values of operands. It categorizes operators into types such as Arithmetic, Comparison, Assignment, Logical, Bitwise, Membership, and Identity operators, detailing their functions and providing examples. The document also explains operator precedence and includes sample code snippets to illustrate the use of various operators.

Uploaded by

karanranvir061
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Python operators are the constructs which can

manipulate the value of operands. These are


symbols used for the purpose of logical,
arithmetic and various other operations.
Consider the expression 4+5=9. Here, 4 and 5
are called operands and + is called operator. In
this tutorial, we will study different types of
Python operators.

Types of Python Operators


Python language supports the following types of
operators.

" Arithmetic Operators


" Comparison (Relational) Operators
Assignment Operators
"Logical Operators
" Bitwise Operators
" Membership Operators
"ldentity Operators
Let us have a quick look on all these operators
one by one.

Python Arithmetic Operators


Python arithmetic operators are used to perform
mathematical operations on numerical values.
These operations are Addition, Subtraction,
Miltinlication Diicion Mad1ue Evnnante and
Operator Name Exam

+ Addition 10 + 20

Subtraction 20- 10

Multiplication 10 * 20 =200

Division 20/ 10 =

% Modulus 22% 10

**
Exponent 4**2 = 16

Floor Division 9//2 = 4

Example
Following is an example which shows all the
above operations:

a = 21 Edit &Run
b = 10

# Addit ion
print ("a + b : + b

# Subtraction
print ("a - b b
# Floor Division
print ("a // b: a // b

This produce the following result -

a + b : 31
a - b: 11
a * b: 210
a / b: 2.1
a %b : 1
a ** b : 16679880978201
a // b : 2

ADVERTISEMENT

60+ sports 6000+


products. Visit Store
Today.
DECATHLON
499 60+ SPORTS
SPORTs
NTAIN 399
6000+ PRODUCTS
NOW OPEN
AT PATIALA

ADVERTISEMENT

Python Comparison Operators


Python comparison operators compare the
values on either sides of them and decide
a = 21
Edit &Run
b = 10

# Addit ion
print ("a + b : ,a + b

# Subtraction
print ("a - b a b

# Mult iplication
print ("a * b * b)

# Division
print ("a / b: " , a / b)

# Modulus
print ("a % b : ", a % b)

# Exponent
print ("a ** b : . a** b)

# Floor Division
print ("a // b: a // b)
a = 4 Edit & Run
b= 5

# Equal
print ("a == b : " .. a == b)

# Not Equal
print ("a != b : a !: b

# Greater Than
print ("a > b: , a > b)

# Less Than
print ("a < b:" a < b)

# Greater Than or Equal to


print ('a >: b: a > b)

# Less Than or Equal to


print ('a <= b: a <= b)

This produce the following result -

a == b : False
a != b : True
a > b: False
a <b: True
a >= b : False
a <= b : True
9:57 AMO

X Python -Operators | T.
tutorialspoint.com

Python Comparison Operators


Python comparison operators compare the
values on either sides of them and decide the
relation among them. They are also called
relational operators. These operators are equal,
not equal, greater than, less than, greater than or
equal to and less than or equal to.

Operator Name Example

Equal 4 ==5 is not


true.

Not Equal 4 = 5 is true.

Greater Than 4>5 is not


true.

Less Than 4<5 is true.

Greater than or 4 >=5 is not


Equal to true.

<= Less than or Equal to 4 <=5is true.

Example
Following is an example which shows all the
above comparison operations:

a =4 Edit &Run :
b = 5
# Greater Than or Equal to

print ("a >= b: a >= b

# Less Than or Equal to


print ("a <= b: ", a <= b

This produce the following result

a == b :
False
a != b : True
a > b : False
a <b : True
a >= b : False
a <= b : True

ADVERTISEMENT

ADVERTISEMENT

Python Assignment Operators


Python assignment operators are used to assign
values to variables. These operators include
simple assignment operator, addition assi
# Assignment Operator Edit & Run O:
a = 10

# Addition Assignment
a += 5
print ("a + = 5:

# Subtraction Assignment
a -= 5
print ("a -= 5 a

# Mult iplication Assignment


a #- 5

print ("a *= 5 : a

# Division Assignment
a /- 5

print ('a /= 5 :".a)

# Remainder Assignment
a %= 3
print ('a %= 3 : a

# Exponent Assignment
a #*= 2

print ("a **= 2:

# Floor Division
Assignment
a //= 3

print ("a //= 3


=6=/=I - = =*=*

Assignment operators
11
is is not

ldentity operators
12
in not in

Membership operators
13
not or and

Logical operators

O Previous Page Next Page

Advertisements

Euit pepe
in Evaluates to true if it
x in y, here in
finds a variable in
results in a 1 if
the specified X is a member
sequence and false
of sequence y.
otherwise.

not in Evaluates to true if it X not in y, here


does not finds a not in results
variable in the in a 1 if x is
not amember
specified sequence
and false otherwise. of sequence y.

ADVERTISEMENT
9:58 AMO

3
*/%/

Multiply, divide, modulo and floor


division

Addition and subtraction

Right and left bitwise shift

6
&

Bitwise 'AND'

Bitwise exclusive 'OR' and regular


'OR'

Comparison operators

9
<>= =

Equality operators

10
=%= |== + = *= **=

Assignment operators
Operator Description Exam

and If both the operands (a and b)


Logical are true then true.
AND condition becomes
true.

or If any of the two (a or b) i


Logical operands are non
OR zero then condition
becomes true.

not Used to reverse the Notla and b) is


Logical logical state of its false.
NOT operand.

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 -
1

Exponentiation (raise to the power)

Complement, unary plus and minus


(method names for the last two are
+@ and-@)

3
*/% I/

Multiply, divide, modulo and floor


division

Addition and subtraction

>><<

Right and left bitwise shift


Operator Description Example
is Evaluates to true if
the variables on X is y, here is
either side of the results in 1 if
operator point to the id(x) equals
same object and id(y).
false otherwise.

is not Evaluates to false if


X is not y, here
the variables on
is not results
either side of the
operator point to the
in 1 if id(x) is
not equal to
same object and true
id(y).
otherwise.

Python perators Precedence


The following tatble lists all operators from
highest precedence to lowest.

[Show xample ]
Sr.No. Operator &Description

Exponentiation (raise to the power)


9:57 AMO

Python Assignment Operators


Python assignment operators are used to assign
values to variables. These operators include
simple assignment operator, addition assign,
subtraction assign, multiplication assign, division
and assign operators etc.

Operator Name Example


Assignment a = 10
Operator

+= Addition a += 5 (Same
Assignment as a =a + 5)

Subtraction a -=5 (Same


Assignment as a = a - 5)

*=
Multiplication a*=5 (Same
Assignment as a = a *5)

Division Assignment a/=5 (Same


as a = a/5)

%= Remainder a %= 5 (Same
Assignment as a = a % 5)

Exponent a t**=2(Same
Assignment as a = a ** 2)

Floor Division all=3 (Same


Assignment as a = al/ 3)

Example
Following is an example which shows all

You might also like