0% found this document useful (0 votes)
11 views10 pages

5 Operators

The document provides an overview of various types of operators in Python, including arithmetic, relational, assignment, bitwise, shift, logical, identity, and membership operators. It explains the functionality of each operator type with examples, detailing how they operate on variables and the precedence of operations. Additionally, it covers concepts such as chained comparisons and the truth value associated with different data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

5 Operators

The document provides an overview of various types of operators in Python, including arithmetic, relational, assignment, bitwise, shift, logical, identity, and membership operators. It explains the functionality of each operator type with examples, detailing how they operate on variables and the precedence of operations. Additionally, it covers concepts such as chained comparisons and the truth value associated with different data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Operators

Operators are symbols that perform specific operations when applied on variables.
c=a+b # c,a,b, operands and “=”,”+” are operators
Above statement is an expression (combination of operands and operator.)
Types of operator
 Arithmetic Operators : +,-,*, / , %, **, // =
 Relational(Comparison) operators: < , > , <= , >= , = =(equal to),
!= (not equal to)
 Assignment Operator:=,+ =, -=, *=, /=, //=, %=
 Bitwise Operators: &, ^, |
 Shift Operators: << , >>
 Logical Operators: and, or, not
 Identity Operators: is, is not
 Membership Operators: in, not in
Arithmetic Operators
Assume variable a holds the value 10 and variable b holds the value 21, then −

Operator Action Example

+ Addition Adds values on either side of the operator. a + b = 31

- Subtraction Subtracts right hand operand from left hand operand. a – b = -11

* Multiplication Multiplies values on either side of the operator a * b = 210

/ Division Divides left hand operand by right hand operand b / a = 2.1

% Modulus Divides left hand operand by right hand operand and b%a=1
returns remainder

// Floor Division - The division of operands where the result is 9//2 = 4 and
the quotient in which the digits after the decimal point are 9.0//2.0 = 4.0,
removed. But if one of the operands is negative, the result
is floored, i.e., rounded away from zero (towards negative -11//-3 = -4,
infinity): 11.0//3 = -4.0
** Exponent Performs exponential (power) calculation on operators a**b =10 to the
power 20

Assignment Operator
Assume variable a holds the value 10 and variable b holds the value 20, then −
Show Example

Operator Description Example

= Assigns values from right side operands to left c = a + b assigns


side operand value of a + b into c

+= Add AND It adds right operand to the left operand and c += a is equivalent
assign the result to left operand to c = c + a

-= Subtract AND It subtracts right operand from the left operand c -= a is equivalent
and assign the result to left operand to c = c – a

*= Multiply AND It multiplies right operand with the left operand c *= a is equivalent
and assign the result to left operand to c = c * a

/= Divide AND It divides left operand with the right operand c /= a is equivalent
and assign the result to left operand to c = c / a

%= Modulus AND It takes modulus using two operands and c %= a is equivalent


assign the result to left operand to c = c % a

**= Exponent AND Performs exponential (power) calculation on c **= a is equivalent


operators and assign value to the left operand to c = c ** a

//= Floor Division It performs floor division on operators and c //= a is equivalent
assign value to the left operand to c = c // a

Relational (Comparison) operators


These operators compare two values. It returns the result as True or False depending upon the
result of comparison
Assume variable a holds the value 10 and variable b holds the value 20, then −
Show Example

Operator Action Example

== If the values of two operands are equal, then the condition (a == b)


becomes true.
Result: False

!= If values of two operands are not equal, then condition (a!= b)


becomes true.
Result: True.

> If the value of left operand is greater than the value of right (a > b)
operand, then condition becomes true.
Result: False

< If the value of left operand is less than the value of right (a < b)
operand, then condition becomes true.
Result: True

>= If the value of left operand is greater than or equal to the value (a >= b)
of right operand, then condition becomes true.
Result: False

<= If the value of left operand is less than or equal to the value of (a <= b)
right operand, then condition becomes true.
Result: True

Chained comparison
Python can chain multiple comparison which are like shortened version of Boolean expression.
Rather than writing 1<2 and 2<3 we can write 1<2<3. This is a chained version of the earlier
Boolean expression.
Example
A=10
B=20
C=5
# C <A<B is same as C <A and A<B
print(C<A) #True
print(A<B) #True
print(C<A<B) #True
As per the and property C<A will be first evaluated and if only it is True, then the next
chained expression A<B will be evaluated.

Comparison – few points to remember.


 For numeric types, the values are compared after removing trialing zeros after decimal
point.
>> 6 >6.0 # will give result False
 Capital letters(ASCII code 65-90) are considered less than small letters(ASCII code 97-
102)
>> "Hello" <"hEllo" # will give result True
 In string comparison, special characters are also assigned some ASCII value.(ASCII of
space is 32,Enter is 13
>> "Hello" <" hello" # will give result False. Since there is a space before ‘h’ in the second
string.
 Floating point comparison gives unexpected results. So it is suggested not to use equality
testing with floating point.
>> 0.1+0.1+0.1= = 0.3 # will give result False
>> print(0.1+0.1+0.1)
Output:0.30000000000000004
In python floating point numbers are presented in memory in binary from up to the allowed 15
digit precision.
Relation Operator with arithmetic operators
Relational operator have lower priority than arithmetic operators. First arithmetic operation
will be solved then comparison operation
Example
a,b,c=10,20,30
print(a+10 > b-10)
ouput
True # here the comparison will be 20>10
What is the difference?

If the value of ‘a’ is 100 what is the difference between the below statements.
Statement 1 >>> a == 60 # equality operator
Statement 2 >>> a = 60 # assignment operator
Logical value ------ Associated with other values
In Python every value is associated with Boolean value true or false. Any object can be tested
for truth value

false (truth value) true(truth value)

None All other values are


considered as True
False
Zero (0)
Empty sequences "", [ ],(),{}

Logical Operators

There are three logical operators supported by Python. These operators (and, or, not) are to be
written in lower case only. The logical operator evaluates to either True or False based on the
logical operands on its either side.

Relational expression as operands:

Operator Operation Description Example


and Logical AND If both >>> num1 = 10
operands are >>> num2 = 20
True, then >>> (num1 > 5) and (num2 < 25)
condition True
becomes True >>> num1 = = 10 and num2 == 10
False
or Logical OR If any of the >>> num1 = 10
two >>> num2 = 2
operands are >>> num1 >= 10 or num2 >= 10
True, then True
condition >>> num1 <= 5 or num2 <= 10
becomes True True
not Logical NOT Used to >>> num1 = 10
reverse the >>> not (num1 = = 20)
logical state True
of its operand >>> not (num1 = = 10)
False

The and operator evaluates to True if both of its (relational) operand evaluates to True; False
if both or either of both operands evaluates to False

Operand1 Operand2 Result


x Y x and y

True True True

True False False

False True False

False False False

The or operator evaluates to True if either of its (relational) operand evaluates to True; False if
both operands evaluates to False

Operand1 Operand2 Result


x Y x or y

True True True

True False True

False True True

False False False

Number / string are used as operand


The output will be based on the internal Boolean value of a number / string. The result (output)
will not be true or false but it will be the value used with and, or operator. Internal value of
the result will be True or False.

and -- in an expression x and y if first operand (i.e., the expression x) has false tval, then return
first operand x as result otherwise return y

Operand1 Operand2 Result


x y x and y

truetval truetval y

truetval falsetval y

falsetval truetval x

falsetval falsetval x

Output
>>> (0) and (0) #0
>>>(0) and (10) #0
>>>(4) and (0) #0
>>> "hello" and " " #''
>>> (9) and (7) #7
>>>"xyz" and "abc" # 'abc'
or – in an expression x or y if first operand (i.e., the expression x) has false tval, then return
second operand y as result otherwise return x

Operand1 Operand2 Result


X y x or y

truetval truetval x

truetval falsetval x

falsetval truetval y

falsetval falsetval y

Output
>>> (0) or (0) #0
>>>(0) or (10) # 10
>>>(4) or (0) #4
>>> "hello" or " " # 'hello '
>>> (9) or (7) #9
>>>"xyz" or "abc" # 'xyz'

Not Operator
>>> not 5 # False because 5 (truetval)
>>>not 0 #True because 0( falsetval)
>>> -4 #True because -4(truetval)
>>> not(5>2) #False because expression is True
>>>not(5 > 9) #True because expression is False

Identity Operators
There are two identity operators is and is not. The identity operators compares the memory
location of two objects and returns True or False accordingly.

Operator Description Example

is returns True if both the operands are a is b


pointing to the same object(i.e., both are
referring to same memory location);
otherwise returns False

is not returns True if both the operands are a is not b


pointing to different object(i.e., both are
referring to different memory location);
otherwise returns False

Example
A=10
B=20
C=10
A is B # False
A is C # True
A is not B #True
A is not C #False

Membership Operators
There are two identity operators in and not in. Python’s membership operators test for
membership in a sequence, such as strings, lists, or tuples.

Operato Description Example


r

in Evaluates to True if it finds a variable in the


a in list
specified sequence and False otherwise.

not in Evaluates to True if it does not finds a variable in a not in list


the specified sequence and False otherwise.

Example
>>> a=10
>>> b=50
>>> list=[10,20,30,40]
>>> a in list
True
>>> 50 in list
False
Operator precedence
The chart of Operator precedence from highest to lowest for the operators high
low
Operator Associativity
If an expression have multiple operators same precedence (*, /, //). All most all operators have
left to right associativity except exponentiation (**) which has right to left associativity.

You might also like