Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
34 views

Arithmetic Operators: Operator Description Example

The document describes various operators in Python including arithmetic, comparison, assignment, bitwise, logical, membership, and identity operators. Arithmetic operators perform mathematical operations like addition, subtraction, etc. Comparison operators compare values and return True or False. Assignment operators assign values to variables. Bitwise operators work on bits and perform operations like AND, OR, etc. Logical operators perform logical operations and return True or False. Membership operators test for membership in a sequence. Identity operators compare the memory locations of objects and return True if two variables point to the same object.

Uploaded by

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

Arithmetic Operators: Operator Description Example

The document describes various operators in Python including arithmetic, comparison, assignment, bitwise, logical, membership, and identity operators. Arithmetic operators perform mathematical operations like addition, subtraction, etc. Comparison operators compare values and return True or False. Assignment operators assign values to variables. Bitwise operators work on bits and perform operations like AND, OR, etc. Logical operators perform logical operations and return True or False. Membership operators test for membership in a sequence. Identity operators compare the memory locations of objects and return True if two variables point to the same object.

Uploaded by

DrSuresh Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Operator Description Example

+ Addition Adds values on either side of the a + b = 30


operator.

- Subtraction Subtracts right hand operand from left a – b = -10


hand operand.

* Multiplication Multiplies values on either side of the a * b = 200


operator

/ Division Divides left hand operand by right hand b/a=2


operand

% Modulus Divides left hand operand by right hand b%a=0


operand and returns remainder

** Exponent Performs exponential (power) a**b =10 to the power 20


calculation on operators

// Floor Division - The division of operands 9//2 = 4 and 9.0//2.0 = 4.0,


where the result is the quotient in which -11//3 = -4, -11.0//3 = -4.0
the digits after the decimal point are
removed. But if one of the operands is
negative, the result is floored, i.e.,
rounded away from zero (towards
negative infinity) −

Arithmetic Operators

Comparison Operators
Operator Description Example

== If the values of two operands are equal, then the (a == b) is not true.
condition becomes true.

!= If values of two operands are not equal, then (a != b) is true.


condition becomes true.

<> If values of two operands are not equal, then (a <> b) is true. This is
condition becomes true. similar to != operator.

> If the value of left operand is greater than the value (a > b) is not true.
of right operand, then condition becomes true.

< If the value of left operand is less than the value of (a < b) is true.
right operand, then condition becomes true.

>= If the value of left operand is greater than or equal to (a >= b) is not true.
the value of right operand, then condition becomes
true.

<= If the value of left operand is less than or equal to the (a <= b) is true.
value of right operand, then condition becomes true.
Assignment Operators
Operator Description Example

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


operands to left side operand value of a + b into c

+= Add AND It adds right operand to the left


c += a is equivalent
operand and assign the result to left
to c = c + a
operand

-= Subtract AND It subtracts right operand from the


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

*= Multiply AND It multiplies right operand with the


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

/= Divide AND It divides left operand with the right c /= a is equivalent


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

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


and assign the result to left operand to c = c % a

**= Exponent Performs exponential (power)


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

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


and assign value to the left operand to c = c // a
Bitwise Operators
Bitwise operator works on bits and performs bit by bit operation. Assume if
a = 60; and b = 13; Now in binary format they will be as follows −

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

[ Show Example ]

Operator Description Example

& Binary AND Operator copies a bit to the result if it (a & b) (means
exists in both operands 0000 1100)

| Binary OR It copies a bit if it exists in either (a | b) = 61 (means


operand. 0011 1101)

^ Binary XOR It copies the bit if it is set in one operand (a ^ b) = 49


but not both. (means 0011 0001)

~ Binary Ones (~a ) = -61 (means


Complement 1100 0011 in 2's
It is unary and has the effect of 'flipping'
complement form
bits.
due to a signed
binary number.

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

>> Binary Right The left operands value is moved right by


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

Python Logical Operators


There are following logical operators supported by Python language.
Assume variable a holds 10 and variable b holds 20 then
Used to reverse the logical state of its operand.
There are following logical operators supported by Python language.
Assume variable a holds 10 and variable b holds 20 then −

Operator Description Example

and Logical If both the operands are true then condition becomes (a and b)
AND true. is true.

or Logical OR If any of the two operands are non-zero then (a or b)


condition becomes true. is true.

not Logical Used to reverse the logical state of its operand. Not(a
NOT and b) is
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

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


specified sequence and false otherwise. in a 1 if x is a
member of sequence
y.

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

Python Identity Operators


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

Operator Description Example

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


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

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


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

You might also like