0% found this document useful (0 votes)
35 views11 pages

Topic 4

The document describes different types of operators in Python including arithmetic, comparison, assignment, logical, bitwise, membership and identity operators. It provides examples of operators like +, -, *, /, ==, >, <, =, +=, ~, &, |, and, or, not, in, is and is not and explains what each one does.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views11 pages

Topic 4

The document describes different types of operators in Python including arithmetic, comparison, assignment, logical, bitwise, membership and identity operators. It provides examples of operators like +, -, *, /, ==, >, <, =, +=, ~, &, |, and, or, not, in, is and is not and explains what each one does.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

OPERATORS

Operator
• An operator is a symbol. The python interpreter identifies operator and
performs a specific mathematical or logical operation. The python
programming language provides the following operators.
• Arithmetic operator
• Comparison operator
• Assignment operator
• Logical operators
• Bitwise operators
• membership operators
• Identity operators
Arithmetic Operators
Operator Description
+ Addition Adds values on either side of the operator

- Subtraction Subtracts right-hand operand from left-hand operand

*Multiplication Multiplies values on either side of the operator

/ Division Divides left-hand operand by right-hand operand


Divides left-hand operand by right-hand operand and
% Modulus
returns the remainder
**Exponent Performs exponential(power) calculations on operators

// Floor Division
Comparison Operators
Operator Description

== If the values of two operands are equal, then the condition becomes true.

!= If the values of two operands are not equal, then the condition becomes true
If the value of the left operand is greater than the value of the right operand
> then the condition becomes true
If the value of the left operand is less than the value of the right operand then
< the condition becomes true
If the value of the left operand is greater than or equal to the value of the
>= right operand then the condition becomes true
If the value of the left operand is less than or equal to the value of the right
<= operand then the condition becomes true
Assignment Operators
Operator Description

= Assigns values from right side operand to left side operand

+= It adds right operand to the left operand and assigns the result to left operand

-= It subtracts right operand to the left operand and assigns the result to left operand

*= It multiplies right operand to the left operand and assigns the result to left operand

/= It divides right operand to the left operand and assigns the result to left operand

%= It takes modulus using two operand and assigns the result to left operand

**= It performs exponential calculation on operators and assign the value to the left operand

//= It performs floor division on operators and assign the value to the left operand
Bitwise Operators
Operator Description
Binary AND Operator copies a bit to the result if it exists in both
& operands
| Binary OR It copies a bit if it exists in either operand

^ It copies the bit if it is set in one operand but not both

~ It is unary and has the effect of ‘Flipping bits’


The left operands values is moved left by the number of bits
<< specified by the right operand
The left operands values is moved right by the number of bits
>> specified by the right operand
Bitwise Operators
Operator Description
Binary AND Operator copies a bit to the result if it exists in both
& operands
| Binary OR It copies a bit if it exists in either operand

^ It copies the bit if it is set in one operand but not both

~ It is unary and has the effect of ‘Flipping bits’


The left operands values is moved left by the number of bits
<< specified by the right operand
The left operands values is moved right by the number of bits
>> specified by the right operand
Logical Operators

Operator Description
Logical AND If both operands are true then condition becomes
and true
Logical OR If any two operands are non-zero then condition
or become true
not Logical NOT used to reverse the logical state of its operand
Membership Operators

Operator Description
Evaluates to true if it finds a variable in the specified sequence and
In false otherwise
Evaluates to true if it does not find a variable in the specified
not in sequence and false otherwise
Identity Operators

Operator Description
Evaluates to true if the variables on either side of the operator
is point to the same object and false otherwise.
Evaluates to false if the variables on either side of the operator
is not point to the same object and true otherwise

You might also like