Operators
in Python
Expressions
in any programming language are a combination of operators and operands.
Operators represent a computation and operands are the values or variables on
which the computation is performed. When expressions are evaluated the result
can be a numeric value or a Boolean value. The types of operators supported in
Python are
1.
Arithmetic operators
2.
Assignment operators
3.
Relational operators
4.
Logical operators
5.
Membership operators
6.
Identity operators
Arithmetic
operators:
Arithmetic
operators are used for performing arithmetic operations. Various arithmetic
operators are
Operator
|
Description
|
Example
|
Output
|
+
|
Addition
|
3 + 5
|
8
|
–
|
Subtraction
|
7 – 3
|
4
|
*
|
Multiplication
|
2 * 4
|
8
|
/
|
Division
|
9 / 3
|
3.0
|
%
|
Modulus
(reminder operator)
|
5 % 2
|
1
|
//
|
Truncation
(Floor) division
|
10 // 3
10 // 3.0
10.0 // 3
|
3
3.0
3.0
|
**
|
Exponentiation
|
2**3
|
8
|
Assignment
operator
Assignment
operator (=) is used to assign values to operands. The right side of the
assignment operator may be a value or an expression. The output of the
expression evaluation is assigned to the operand in the left side of the
assignment operator. Example
a = 5 (Direct assignment)
b = 3 + 4 (Evaluate expression containing
values)
c = a * b (Evaluate expression containing
operands)
d = c + 1 (Evaluate expression containing
values and operands)
Arithmetic
Assignment operators are also called as shortcut operators. When an operator is
used in the expression and the resultant value is assigned to the same operator
it is called as arithmetic assignment operator. Example, consider
a += b
is equal to a = a + b.
The
value of a and b are added and the result is assigned to a. The old value of a is
now replaced by the new value 8
Operator
|
Description
|
Example
|
Equivalent
|
+=
|
Addition
assignment
|
a += b
|
a = a + b
|
–=
|
Subtraction
assignment
|
a –= b
|
a = a – b
|
*=
|
Multiplication
assignment
|
a *= b
|
a = a * b
|
/=
|
Division
assignment
|
a /= b
|
a = a / b
|
%=
|
Modulus
assignment
|
a %= b
|
a = a % b
|
//=
|
Truncation
division assignment
|
a //= b
|
a = a // b
|
**=
|
Exponentiation
assignment
|
a **= b
|
a = a ** b
|
Relational
operators
Relational
operators are also known as comparison operators and they return a Boolean
value. They are used in conditional statements to compare values and take
action depending on the result. If a = 5, b = 7 and c = 5
Operator
|
Description
|
Example
|
Result
|
==
|
Equal
to
|
a == b
|
False
|
<
|
Less
than
|
a < b
|
True
|
<=
|
Less
than or Equal to
|
a <= b
|
True
|
>
|
Greater
than
|
a > c
|
False
|
>=
|
Greater
than or Equal to
|
a >= c
|
True
|
!=
|
Not
equal to
|
a != b
|
True
|
<>
|
Similar
to (!=)
|
a <> b
|
True
|
Logical
operators
Logical
operators are based on Boolean Algebra and they return a Boolean value. They
are used in situations involving more than one conditional statement.
Operator
|
Description
|
Evaluation
|
and
|
AND
|
Returns True if ALL conditional statements returns
True. Else returns False
|
or
|
OR
|
Returns True if atleast one of the conditional
statements return True. Else returns False
|
not
|
Unary
NOT
|
Returns True if the operand has zero value. Else
returns False
|
Example
if a = 5, b = 3 and c = 7
((a > b) and (a > c)) will
return False
((a
> b) or (a > c)) will return True
Membership
operators
Membership
operators checks whether an element is a member of a sequence or not. The
sequence can be List, String, Tuple or Dictionary. Example If List1 = [2,
4, 6, 8]
Operator
|
Example
|
Result
|
in
|
2
in List1
3
in List1
|
True
False
|
not in
|
2
not in List1
3
not in List1
|
False
True
|
Identity
operators
Identity
operators are used to compare the memory locations of two Python objects. Returns
true if the two variables on either side of is operator are referring to the
same object. Else returns False. Example if
List1
= [2, 4, 6, 8]
List2
= List1
Operator
|
Example
|
Result
|
is
|
List1
is List2
|
True
|
is not
|
List1
is not List2
|
False
|
Order of operations (Operator precedence)
When
more than one operator appears in the expression, the order of evaluation
depends on the precedence of operators. For mathematical operators Python
follows the PEMDAS rule
1.
Parenthesis has the highest precedence.
a.
Example (1+1)*5 = 10 and not 6
2.
Exponentiation has the next highest precedence
a.
Example 2**3+1 = 9 and not 16
3.
Multiplication and Division have higher
precedence than Addition and Subtraction.
a.
Note: Multiplication and Division have equal
precedence. Similarly Addition and Subtraction have the equal precedence.
b.
Example:
i. 2*6/4
= 3 (equal
precedence for * and /)
ii. 2+4-3
= 3 (equal
precedence for + and -)
iii. 2*6+4
= 16 and not 20 (higher
precedence for * than +)
4.
When operators have equal precedence the expression
is evaluated from left to right.
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu