Operators
Operators are the symbols which perform the operation
on some values. These values are known as operands. For example if we write a +
b, the operator + acts on the two operand a and b, this is called binary
operator. If an operator acts on a single operand then it is called unary
operator.
Example:-
-a Unary
Minus operator
+a Unary
Plus Operator
a + b Binary Operator
Operators are divided into eight parts:
- Unary Operator
- Arithmetic Operator
- Assignment Operator
- Relational Operator
- Logical Operator
- Identity Operator
- Membership Operator
- Bitwise Operator
Unary Operator
When an operator acts on a single variable then it is
called Unary operator.
There are two types of unary operator:
Unary Plus Operator
Unary Minus Operator
Example:-
x = 10
a = x
b = -x
print(“The value of a is: “,a)
print(“The value of b is: “,b)
Output:-
The value of a is: 10
The value of b is: -10
Arithmetic Operator
These operators are used to perform arithmetic
operations like addition, subtraction, multiplication, division, etc.
Since this operator works on two operand it is called
binary operator. There are seven arithmetic operators in python:
Operator
|
Meaning
|
Example
|
Result
|
+
|
Addition
|
10 +
2
|
12
|
–
|
Subtraction
|
12 –
2
|
10
|
*
|
Multiplication
|
10 *
2
|
20
|
/
|
Division
|
10 /
2
|
5
|
%
|
Remainder
|
13 %
2
|
1
|
**
|
Exponential
|
10
**2
|
100
|
//
|
Quotient
|
13
// 2
|
6
|
Relational Operator
This operator is also known as comparison operator
because it compares two values and returns the Boolean value i.e. either ‘True’
or ‘False’.
The value of x is 10 and y is 9
Operator
|
Meaning
|
Example
|
Result
|
>
|
Greater
than
|
x
> y
|
True
|
<
|
Less
than
|
x
< y
|
False
|
>=
|
Greater
than equal to
|
x
>= y
|
True
|
<=
|
Less
than equal to
|
x
<= y
|
False
|
==
|
Equal
to
|
x ==
y
|
False
|
!=
|
Not
equal to
|
x !=
y
|
True
|
Assignment Operator
Assignment operator is used to assign values in the
variable or operand. Assignment operator is also used perform simple arithmetic
operations like addition, subtraction, multiplication, etc.
The value of x is 10 and y is 9
Operator
|
Meaning
|
Example
|
Result
|
=
|
Assignment
Operator
|
x=y
|
x=3
|
+=
|
Assignment
plus operator
|
x+=y
|
x =
x + y
x =
19
|
-=
|
Assignment
minus operator
|
x-=y
|
x =
1
|
*=
|
Multiplication
assignment operator
|
x*=y
|
x =
90
|
Logical Operator
Logical operator is used to check two or more than two
conditions. The logical operator also returns the Boolean result either true or
false.
The value of x is 10 and y is 9
Operator
|
Meaning
|
Example
|
Result
|
AND
|
It
returns true if two or more conditions are true
|
x=10
and y=9
|
True
|
OR
|
It
returns true if any two or more conditions are true
|
x=10
or y=19
|
True
|
NOT
|
Returns
true if operand is false
|
Not
x=10
|
False
|
Identity Operator
Identity operators are used to compare the objects
with the same memory location.
The value of x is 9 and y is 9.0
Operator
|
Meaning
|
Example
|
Result
|
is
|
Returns true if both variables are the
same object
|
x is
y
|
False
|
Is
not
|
Returns true if both variables are not the
same object
|
x is
not y
|
True
|
Membership Operator
Membership Operators are the operators, which are used
to check whether a value exists in the sequence like string, list, etc. or not.
The value of x is 9 and list1 = [5,7,9,11,13]
Operator
|
Meaning
|
Example
|
Result
|
In
|
Returns
true if value found in a sequence
|
x in
list1
|
True
|
not
in
|
Returns
true if value not found in a sequence
|
x
not in list1
|
False
|
Comments
Post a Comment
Students you can ask your doubts in comment section