Operators:
- Operators are symbols and used to perform some mathematics operations.
- There are 6 kinds of operators,
- This is normal mathematic operations such as,
S.No |
Operator |
Expression |
Example |
Result |
1. |
+ |
Addition |
If
a=10 and b=5 then a+b=10+5 |
15 |
2. |
- |
Subtraction |
If
a=10 and b=5 then a-b=10-5 |
5 |
3. |
* |
Multiplication |
If
a=10 and b=5 then a*b=10*5 |
50 |
4. |
/ |
Division |
If
a=10 and b=5 then a/b=10/5 |
2 |
5. |
% |
Modulo |
If
a=10 and b=5 then a%b=10%5 |
0.05 |
6. |
** |
Power |
If
a=5 then a**2 |
25 |
7. |
// |
Floor
division |
If
a=10 and b=5 then a//b=7//5 |
1 |
- It is used to compare two or more data.
- These logical operators mostly used in conditions.
- It is used to combine simple relational statements into more complex expressions.
S.No |
Operator |
Expression |
Example |
Result |
1. |
and |
Both expressions should be true |
If a=11,b=10
and c=12 then a>b and a>c |
False |
2. |
or |
One expression should be true |
If a=11,b=10
and c=12 then a>b or a>c |
True |
3. |
not |
Both expression false then return true |
If a=5,b=10
and c=12 then a>b not a>c |
True |
- These operators are used to getting the result in the same variable(input/output).
- It is used to manipulate the data at a bit level and it operates on integer only.
- The truth table for ‘ & ‘is shown below(It means True=1 and false=0.)
- The truth table for ‘ | ‘is shown below
- The truth table for ‘ ^ ‘is shown below
- The bitwise shift left operator is used to move the bits from right to left in storage.
- Each box called bits and a=6 and a<<1 then a=12.
- The shit right is similar to the left shit.
- This operator is used to forward the bits from left to right in storage.
- For example, If a=6 and a>>1 then its value is 3.
Explanation:
Result: a of right shit value is 3.
Example program for the bitwise shift right operator:
- The truth table for ‘ ~ ‘is shown below
Using a negative signature for one's complement(~a=-7).
6.
Special operators:
Python language provides some special type of operators like identity operator and membership
operator.
(i)
Identity operator:
- An identity operator is used to checking both are equal or not equal.
Operator
|
Description
|
Example
|
Result
|
is
|
If
both values are the same then return true. |
If
a=4 and b=5 then a is b |
False
|
is
not |
If
both values are not the same then return true. |
If
a=4 and b=5 then a is not b |
True
|
(ii)
Membership operator:
- An identity operator is used to checking a given value is present in a list or words.
Operator
|
Description
|
Example
|
Result
|
in
|
If
the values are present in the given list or anything then return true. |
If
a=” Hello” then
‘l’ in a |
True |
not
in |
If
the values are not present not in the given list or anything then return true. |
If
a=” Hello” then
‘i’ in a |
True
|