Operators in Python
Operators in Python
• Identifiers
•
• Variables
• Simple Input/Output
Contents
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
Operators and Operands
The operators are special symbols that represent computations
Types of Operators
Arithmetic Operators
Relational Operators
Assignment & Compound Operators
Logical Operators
Bitwise Operators
Membership Operators
Identity Operators
Note: The bool data type can be used in arithmetic, relational,
assignment and logical operators. (Ture is 1 and False is 0)
4
Arithmetic Operator
Arithmetic operators are the symbols that represent arithmetic math
operations.
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
** Exponentiation x ** y
== Equality
!= Not equal
> Greater than
< Less than
>= Greater than and Equal to
<= Less than and Equal to
6
Assignment & Compound Operators
The assignment (& compound ) operators are binary. These operators
perform operation on two operands and assign the result to left side
operand.
For all operators (except =), the variable must be initialized before
the use in compound operators.
= Assignment
+= Add & Assignment
It adds right operand to the left operand and assign the result
to left operand.
/= Divide & Assignment
It divides left operand with the right operand and assign the
result to left operand
etc.
7
Logical Operators
The logical operators are binary except not.
The logical operators considers operators either True or False
These operators compare the values on either sides of them and returns
the value either “True” or “False”.
The basic rule is…
False is “zero” or “empty” string
True is anything else (“non-empty” or “not zero”)
Operators
and (Logical AND)
If both the operands are True, then condition becomes true.
or (Logical OR)
If any of the two operands are non-zero then condition
becomes true.
not(Logical NOT)
It is used to reverse the logical state of the operand.
8
Bitwise Operators
The bitwise operators are binary ( except complement ~) and are
applied on corresponding bits in integers.
| (Binary OR)
If both bits in both operands are zero, then it copies bit zero to
the result otherwise it copies one.
^ (Binary XOR)
If one bit is one and other is zero, then it copies bit one to the
result otherwise it copies zero bit.
9
Bitwise Operators
<< (Binary Left Shift)
The left operand's value is moved left by the number of bits
specified by the right operand.
~ (2’s complement)
It is a unary operator that complements the operand.
10
Membership Operators
Python’s membership operators test for membership in a
sequence, such as strings, lists, or tuples.
in
Evaluates to True if it finds a value in the specified
sequence and False otherwise
not in
Evaluates to True if it does not finds a value in the
specified sequence and False otherwise.
11
Operator’s Precedence
() Parenthesis
** Exponentiation (raise to the power)
~, +, - Complement, unary plus and minus
*, /, %. // 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
12
Operator’s Associatively
Different operators have different priorities.
When two operators have the same precedence, associativity helps
to determine the order of operations.
Almost all the operators have left-to-right associativity.
Exponent operator ** has right-to-left associatively.
Assignment operator = has right-to-left associatively.
• Relational Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators