OPERATORS
PYTHON
Mrs.N.Kavitha
Head,Department of Computer Science,
E.M.G.Yadava Women’s College
INTRODUCTION
An operator is a symbol that performs an operation. An operator acts on
some variables called operands.
For example : if we write a+b, the operator ‘+’ is acting on two
operands ‘a’ and ‘b’. If an operator acts on a single variable, it is called
unary operator. If an operator acts on two variables, it is called binary
operator.
Arithmetic operators
Assignment operators
Unary minus operator
Relational operators
Logical operators
Boolean operators
Bitwise operators
Membership operators
Identity operators
ARITHMETIC OPERATORS
These operators are used to perform basic arithmetic operations like
addition, subtraction, division, etc.
These are seven arithmetic operators available in python.Let assume the
values a=13 and b=5
Operator Meaning Example Result
+ Addition operator. Adds two values. a+b 18
- Subtraction operator. Subtracts one value from
another.
a-b 8
* Multiplication operator. Multiplies value on
either side of the operator.
a*b 65
/ Division operator. Divides left operand by the
right operand.
a/b
2.6
% Modulus operator.Gives remainder of division. a**b 3
** Exponent operator. Calculates exponential
power value. a**b gives the value of a to the
power of b.
a//b 371293
// Integer division. This is also called floor
division.
a//b 2
ASSIGNMENT OPERATORS
These operators are useful to store the right side value in to a left side
variable.
They can also be used to perform simple arithmetic operations like
addition, subtraction, etc., and then store the result in to a variable.
Let’s assume the values x=20, y=10 and z=5.
Operator Example Meaning Result
=- z=x+y Assignment operator. Stores right side value in to left side variables,
i.e. x+y stores in to z.
z=30
+= z+=x Addition assignment operator. Adds right operand to the left operand
and stores the result in to left operand, i.e.z=z+x.
z=25
-= z-=x Subtraction assignment operator. Subtracts right operand from left
operand and stores the result in to left operand.
z=15
*= z*=x Multiplication assignment operator. Multiplies right operand with left
operand and stores the result in toleft operand, i.e.z=z*x.
z=100
/= z/=x Division assignment operator. Divides left operand with right
operand and stores the result in to left operand, i.e.z=z/x.
z=0.25
%= z%=x Modulus assignment operator. Dividies left operand with right
operand and stores the remainder in to left operand, ie.z=z%x.
z=5
**= z**=y Exponentiation assignment operator. Performs power value and then
stores the result in to left operand, i.e.z=z**y.
z=97.65625
It is possible to assign the same value to two variables in the same statement
as:
a=b=1
print(a,b) # will display 1 1
Another example is where we can assign different values to two variables as:
a=1;b=1
print(a,b) # will display 1 2
UNARY MINUS OPERATOR
The unary minus operator is denoted by the symbol minus (-). When this
operator is used before a variable, its value is negated. That means if the variable
value is positive, it will be converted in to negative and vice versa.
For example:
n=10
print(-n) # display -10
num=-10
num=-num
print(num) # display -10
RELATIONAL OPERATORS
Relational operators are used to compare two qualities. These operators
will result in true or false depending on the values compared, we are assuming
a=1 and b=2.
Operator Example Meaning Result
> a>b Greater than operator. If the value of right operand, it gives True
else it gives False.
False
>= a>=b Greater than or equal operator. If the value of left operand is greater
or equal than of right operand , it gives True else False.
False
< a<b Less than operator. If the value of left operand is less than the value
of right operand, it gives True else it gives False.
True
<= a<=b Less than or equal operator. If the value left operand is lesser or
equal than that of right operand , it gives True else False.
True
== a==b Equals operator. If the value of left operand is equal to the value of
right operand, it gives True else False.
False
!= a!=b Not equals operator. If the value of the left operator is not equal to
the value of right operand, it returns True else it returns False.
True
> a>b Greater than operator. If the value of right operand, it gives True
else it gives False.
False
For example:
CODING
a=1;b=2
if(a>b):
print(“Yes”)
else:
print(“No”)
OUTPUT
No
LOGICAL OPERATORS
Logical operator are useful to construct compound conditions. A compound
condition is a combination of more than one simple condition. Let’s take x=1 and
y=2
Operator Example Meaning Result
And x and y And operator. If x is False, it returns
x, otherwise it returns y.
2
Or x or y Or operator. If x is False, it returns
y, otherwise it returns x.
1
Not not x Not operator. If x is False, it returns
True otherwise True.
False
For example:
x=100
y=200
print(x and y) # will display 200
print(x or y) # will display 100
print(not x) # will display False
CODING
x=1; y=2; z=3
if(x<y and y<z):
print(‘Yes’)
else:
print(‘No’)
OUTPUT: Yes
BOOLEAN OPERATORS
Boolean operators act upon ‘bool’ type literals and they provide ‘bool’ type
output. It means the result provided by Boolean operators will be again either
True or False. Let’s take x=True and y=False.
Operator Example Meaning Result
And x and y Boolean operator. If both x and y are True, then
it returns True otherwise False.
False
Or x or y Boolean or operator. If either x or y is True,
then it returns True, else False.
True
Not not x Boolean operator. If x is True, it returns Flase
else True.
False
For example:
a=True
b=False
a and a
True
a and b
False
a or a
True
a or b
True
b or b
False
not a
False
not b
True
BITWISE OPERATORS
These operators act on individual bits (0 and 1) of the operands. We can use bitwise
operators directly on binary numbers or on integers also.
There are 6 types of bitwise operators:
 Bitwise Complement operator (~)
 Bitwise AND operator (&)
 Bitwise OR operator (|)
 Bitwise XOR operator (^)
 Bitwise Left shift operator (<<)
 Bitwise Right shift operator (>>)
MEMBERSHIPOPERATORS
The membership operators are useful to test for membership in a sequence
such as strings, lists, tuples or dictionaries.
There are two types of membership operators:
 in
 not in
The in operator
This operator returns True if an element is found in the specified
sequence. If the element is not found in the sequence, then it returns False.
The not in operator
This words in reverse manner for ‘in’ operator. This operator returns
True if an element is not found in the sequence, then it returns False.
For example:
CODING
Names=[“dharshini”, “ indhuja”, “jepitha” ]
For name in names:
Print(name)
OUTUT
dharshini
indhuja
jepitha
IDENTITYOPERATORS
These operators compare the memory locations of two objects. Hence, it is
possible to know whether the two objects are same or not.
The memory location of an object can be seen using the id()
function.
For example:
a=25 b=25
id(a)
1670954952
Id(b)
1670954952
There are two identity operators:
Is
Is not
For example:
a=25 b=25
id(a)
1670954952
Id(b)
1670954952
There are two identity operators:
Is
Is not
The is operator
The ‘is’ operator is useful to compare whether two objects are same or
not. It will internally compare the identity number of the objects. If the identity
numbers of the objects are same, it will return True; otherwise, it returns False.
The is not operator
This is not operator returns True, if the identity numbers of two
objects being compared are not same. If they are same, then it will return False.
For example:
CODING
one=[1,2,3,4]
two=[1,2,3,4]
if(one is two):
print(“one and two are same”)
else:
print(“one and two are not same”)
OUTPUT: one and two are not same
Thank you…!

Operators Concept in Python-N.Kavitha.pptx

  • 1.
    OPERATORS PYTHON Mrs.N.Kavitha Head,Department of ComputerScience, E.M.G.Yadava Women’s College
  • 2.
    INTRODUCTION An operator isa symbol that performs an operation. An operator acts on some variables called operands. For example : if we write a+b, the operator ‘+’ is acting on two operands ‘a’ and ‘b’. If an operator acts on a single variable, it is called unary operator. If an operator acts on two variables, it is called binary operator.
  • 3.
    Arithmetic operators Assignment operators Unaryminus operator Relational operators Logical operators Boolean operators Bitwise operators Membership operators Identity operators
  • 4.
    ARITHMETIC OPERATORS These operatorsare used to perform basic arithmetic operations like addition, subtraction, division, etc. These are seven arithmetic operators available in python.Let assume the values a=13 and b=5
  • 5.
    Operator Meaning ExampleResult + Addition operator. Adds two values. a+b 18 - Subtraction operator. Subtracts one value from another. a-b 8 * Multiplication operator. Multiplies value on either side of the operator. a*b 65 / Division operator. Divides left operand by the right operand. a/b 2.6 % Modulus operator.Gives remainder of division. a**b 3 ** Exponent operator. Calculates exponential power value. a**b gives the value of a to the power of b. a//b 371293 // Integer division. This is also called floor division. a//b 2
  • 6.
    ASSIGNMENT OPERATORS These operatorsare useful to store the right side value in to a left side variable. They can also be used to perform simple arithmetic operations like addition, subtraction, etc., and then store the result in to a variable. Let’s assume the values x=20, y=10 and z=5.
  • 7.
    Operator Example MeaningResult =- z=x+y Assignment operator. Stores right side value in to left side variables, i.e. x+y stores in to z. z=30 += z+=x Addition assignment operator. Adds right operand to the left operand and stores the result in to left operand, i.e.z=z+x. z=25 -= z-=x Subtraction assignment operator. Subtracts right operand from left operand and stores the result in to left operand. z=15 *= z*=x Multiplication assignment operator. Multiplies right operand with left operand and stores the result in toleft operand, i.e.z=z*x. z=100 /= z/=x Division assignment operator. Divides left operand with right operand and stores the result in to left operand, i.e.z=z/x. z=0.25 %= z%=x Modulus assignment operator. Dividies left operand with right operand and stores the remainder in to left operand, ie.z=z%x. z=5 **= z**=y Exponentiation assignment operator. Performs power value and then stores the result in to left operand, i.e.z=z**y. z=97.65625
  • 8.
    It is possibleto assign the same value to two variables in the same statement as: a=b=1 print(a,b) # will display 1 1 Another example is where we can assign different values to two variables as: a=1;b=1 print(a,b) # will display 1 2
  • 9.
    UNARY MINUS OPERATOR Theunary minus operator is denoted by the symbol minus (-). When this operator is used before a variable, its value is negated. That means if the variable value is positive, it will be converted in to negative and vice versa. For example: n=10 print(-n) # display -10 num=-10 num=-num print(num) # display -10
  • 10.
    RELATIONAL OPERATORS Relational operatorsare used to compare two qualities. These operators will result in true or false depending on the values compared, we are assuming a=1 and b=2.
  • 11.
    Operator Example MeaningResult > a>b Greater than operator. If the value of right operand, it gives True else it gives False. False >= a>=b Greater than or equal operator. If the value of left operand is greater or equal than of right operand , it gives True else False. False < a<b Less than operator. If the value of left operand is less than the value of right operand, it gives True else it gives False. True <= a<=b Less than or equal operator. If the value left operand is lesser or equal than that of right operand , it gives True else False. True == a==b Equals operator. If the value of left operand is equal to the value of right operand, it gives True else False. False != a!=b Not equals operator. If the value of the left operator is not equal to the value of right operand, it returns True else it returns False. True > a>b Greater than operator. If the value of right operand, it gives True else it gives False. False
  • 12.
  • 13.
    LOGICAL OPERATORS Logical operatorare useful to construct compound conditions. A compound condition is a combination of more than one simple condition. Let’s take x=1 and y=2 Operator Example Meaning Result And x and y And operator. If x is False, it returns x, otherwise it returns y. 2 Or x or y Or operator. If x is False, it returns y, otherwise it returns x. 1 Not not x Not operator. If x is False, it returns True otherwise True. False
  • 14.
    For example: x=100 y=200 print(x andy) # will display 200 print(x or y) # will display 100 print(not x) # will display False CODING x=1; y=2; z=3 if(x<y and y<z): print(‘Yes’) else: print(‘No’) OUTPUT: Yes
  • 15.
    BOOLEAN OPERATORS Boolean operatorsact upon ‘bool’ type literals and they provide ‘bool’ type output. It means the result provided by Boolean operators will be again either True or False. Let’s take x=True and y=False. Operator Example Meaning Result And x and y Boolean operator. If both x and y are True, then it returns True otherwise False. False Or x or y Boolean or operator. If either x or y is True, then it returns True, else False. True Not not x Boolean operator. If x is True, it returns Flase else True. False
  • 16.
    For example: a=True b=False a anda True a and b False a or a True a or b True b or b False not a False not b True
  • 17.
    BITWISE OPERATORS These operatorsact on individual bits (0 and 1) of the operands. We can use bitwise operators directly on binary numbers or on integers also.
  • 18.
    There are 6types of bitwise operators:  Bitwise Complement operator (~)  Bitwise AND operator (&)  Bitwise OR operator (|)  Bitwise XOR operator (^)  Bitwise Left shift operator (<<)  Bitwise Right shift operator (>>)
  • 19.
    MEMBERSHIPOPERATORS The membership operatorsare useful to test for membership in a sequence such as strings, lists, tuples or dictionaries. There are two types of membership operators:  in  not in
  • 20.
    The in operator Thisoperator returns True if an element is found in the specified sequence. If the element is not found in the sequence, then it returns False. The not in operator This words in reverse manner for ‘in’ operator. This operator returns True if an element is not found in the sequence, then it returns False.
  • 21.
    For example: CODING Names=[“dharshini”, “indhuja”, “jepitha” ] For name in names: Print(name) OUTUT dharshini indhuja jepitha
  • 22.
    IDENTITYOPERATORS These operators comparethe memory locations of two objects. Hence, it is possible to know whether the two objects are same or not. The memory location of an object can be seen using the id() function.
  • 23.
  • 24.
  • 25.
    The is operator The‘is’ operator is useful to compare whether two objects are same or not. It will internally compare the identity number of the objects. If the identity numbers of the objects are same, it will return True; otherwise, it returns False. The is not operator This is not operator returns True, if the identity numbers of two objects being compared are not same. If they are same, then it will return False.
  • 26.
    For example: CODING one=[1,2,3,4] two=[1,2,3,4] if(one istwo): print(“one and two are same”) else: print(“one and two are not same”) OUTPUT: one and two are not same
  • 27.