You are on page 1of 6

APPLICATION LABORATORY II

Sherina Sally

Python Operators Department of ICT


Faculty of Technology,
University of Colombo

OUTLINE
• Arithmetic Operators

• Assignment Operators

• Comparison Operators
All Rights Reserved

• Logical Operators

• Identity Operators

• Membership Operators

• Bitwise Operators

• Precedence and Associativity

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 2

1
ARITHMETIC OPERATORS
• Performs mathematical operations on numerical values

+ Addition a+b
- Subtraction a-b

All Rights Reserved


* Multiplication a*b
/ Division a/b
% Modulus a%b
** Exponentiation a ** b
// Floor division a // b

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 3

ASSIGNMENT OPERATORS
• Assigns a literal to a variable

= a = “String” Assigns “String” to a


+= a += 10 a = a + 10
All Rights Reserved

-= a -= 10 a = a - 10
*= a *= 10 a = a * 10
/= a /= 10 a = a / 10
%= a %= 10 a = a % 10
** = a**= 2 a = a ** 2
//= a //= 10 a = a // 10

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 4

2
COMPARISON OPERATORS
• Compares the values of two operands

• The result is a Boolean value: True or False

== a==b Returns True if a and b has the same value

All Rights Reserved


!= a != b Returns True if a is not the same as b

> a>b Returns True if a is greater than b

< a<b Returns True if a is less than b

>= a >= b Returns True if a is greater than or equal to b

<= a <= b Returns True if a is less than or equal to b

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 5

LOGICAL OPERATORS
• Combines conditional statements

• The result is a Boolean value: True or False


All Rights Reserved

not not ( a < 10) Returns True if a is greater than 10

and (a <10) and (b<15) Returns True if both the conditions are True

or (a <10) or (b<15) Returns True if either condition is True

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 6

3
IDENTITY OPERATORS
• Compares two objects by memory location

• The result is a Boolean value: True or False

All Rights Reserved


is a is b Returns True if both a and b are the same
object having the same memory location

is not a is not b Returns True if a and b are not the same


object

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 7

MEMBERSHIP OPERATORS
• Test for a value in a sequence: string, list, tuples

• The result is a Boolean value: True or False


All Rights Reserved

in x in y Returns True if x is a member of the sequence y

not in x not in y Returns True if x is not a member of the


sequence y

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 8

4
BITWISE OPERATORS
• Compares bits (1 and 0)

• The result is a bit: 1 or 0

All Rights Reserved


& a&b Returns 1 only if a and b are both 1

| a|b Returns 0 if a and b are both 0

~ ~a Returns the 1’s compliment of a

^ a^b Returns 1 only if either a or b is 1

<< a << 2 Shifts the leftmost bits by two places

>> a >> 2 Shifts the rightmost bits by two places

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 9

PRECEDENCE AND ASSOCIATIVITY


• Useful to evaluate python expressions which contains multiple
operators and operands
Precedence lowers

• The highest precedence of operators is the ()


All Rights Reserved

• Operators at the same level of precedence, will be evaluated


according to the associativity

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 10

5
EVALUATING EXPRESSIONS

(4+1) ** 2 --+3 5 & 6 << 1

All Rights Reserved


5 ** 2 -(-(+3))
5 & 12

-(-3)
25
4

Department of ICT, Faculty of Technology, University of Colombo All Rights Reserved 11

Department of ICT, Faculty of Technology, University of Colombo

You might also like