Thursday, 28 September 2017

6)Python Operators

Python Operators
Operators are special symbols in Python that are useful to compute values of operands

Type of operators in Python:
  1. Arithmetic operators
  2. Comparison (Relational) operators
  3. Logical (Boolean) operators
  4. Bitwise operators
  5. Assignment operators
  6. Special operators

1.Arithmetic operators:
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication etc.

Arithmetic operators table:


Operator
Meaning
Example
+
Add two operands or unary plus
x + y
+2
-
Subtract right operand from the left or unary minus
x - y
-2
*
Multiply two operands
x * y
/
Divide left operand by the right one (always results into float)
x / y
%
Modulus - remainder of the division of left operand by the right
x % y (remainder of x/y)
//
Floor division - division that results into whole number adjusted to the left in the number line
x // y
**
Exponent - left operand raised to the power of right
x**y (x to the power y)


Examples:



2.Comparison operators:

Comparison operators are used to compare values. It either returns True or False

Comparison operators table:


Operator
Meaning
Example
> 
Greater that - True if left operand is greater than the right
x > y
< 
Less that - True if left operand is less than the right
x < y
==
Equal to - True if both operands are equal
x == y
!=
Not equal to - True if operands are not equal
x != y
>=
Greater than or equal to - True if left operand is greater than or equal to the right
x >= y
<=
Less than or equal to - True if left operand is less than or equal to the right
x <= y

Examples:



3.Logical operators:

Logical operators are the and, or, not operators.

Logical operators table:


Operator
Meaning
Example
and
True if both the operands are true
x and y
or
True if either of the operands is true
x or y
not
True if operand is false (complements the operand)
not x

Examples:



4.Bitwise operators:

Bitwise operators act on operands as if they were string of binary digits.

Bitwise operators table:


Operator
Description
Example
& Binary AND
Operator copies a bit to the result if it exists in both operands
(a & b) (means 0000 1100)
| Binary OR
It copies a bit if it exists in either operand.
(a | b) = 61 (means 0011 1101)
^ Binary XOR
It copies the bit if it is set in one operand but not both.
(a ^ b) = 49 (means 0011 0001)
~ Binary Ones Complement
It is unary and has the effect of 'flipping' bits.
(~a ) = -61 (means 1100 0011 in 2's complement form due to a signed binary number.
<< Binary Left Shift
The left operands value is moved left by the number of bits specified by the right operand.
a << 2 = 240 (means 1111 0000)
>> Binary Right Shift
The left operands value is moved right by the number of bits specified by the right operand.
a >> 2 = 15 (means 0000 1111)


Examples:





5.Assignment operators:
Assignment operators are used to assign values to variables.

Assignment operators table:


Operator
Example
Equivatent to
=
x = 5
x = 5
+=
x += 5
x = x + 5
-=
x -= 5
x = x - 5
*=
x *= 5
x = x * 5
/=
x /= 5
x = x / 5
%=
x %= 5
x = x % 5
//=
x //= 5
x = x // 5
**=
x **= 5
x = x ** 5
&=
x &= 5
x = x & 5
|=
x |= 5
x = x | 5
^=
x ^= 5
x = x ^ 5
>>=
x >>= 5
x = x >> 5
<<=
x <<= 5
x = x << 5



6.Special operators:
They are two types of special operators:

  1. Identity operators
  2. Membership operators

i)Identity operators:
Identity operators used to check if two values (or variables) are located on the same part of the memory.

Identity operators Table:


Operator
Meaning
Example
is
True if the operands are identical (refer to the same object)
x is True
is not
True if the operands are not identical (do not refer to the same object)
x is not True

ii)Membership operators:
in and not in are the membership operators in Python. They are used to test whether a value or variable is found in a sequence (string, list, tuple, set and dictionary).

Membership operators Table:


Operator
Meaning
Example
in
True if value/variable is found in the sequence
5 in x
not in
True if value/variable is not found in the sequence
5 not in x

Example:









3 comments:

Fundamentals of Python programming

Fundamentals of Python programming: Following below are the fundamental constructs of Python programming: Python Data types Python...