Unit-2
Unit-2
Unit 2
CC S 1 5 0 0
Objectives
❑ Explain the different types of operators
❑ Perform operations using the different types of operators
❑ Define operator precedence and associativity
❑ Create a program using the different types of operators
2
Arithmetic Operators
3
Operator Name Example
+ Addition x + y
- Subtraction x – y
* Multiplication x * y
/ Float Division x / y
% Modulus x % y
** Exponentiation x ** y
// Integer Division x // y
4
5
Shortcut Arithmetic
Operators
6
Two Categories
❑ Compound assignment operators
❑ Increment and decrement operators
7
Operator Name Example
= Assignment Operator a = 7
+= Addition Assignment a += 1 # a = a + 1
-= Subtraction Assignment a -= 3 # a = a – 3
*= Multiplication Assignment a *= 4 # a = a * 4
/= Division Assignment a /= 3 # a = a / 3
%= Remainder Assignment a %= 10 # a = a % 10
**= Exponent Assignment a **= 10 # a = a ** 10
8
Increment and Decrement Operators
9
Unary Operators
10
Operator Name Example
+ Positive +x
- Negative –y
11
Comparison Operators
12
Comparison operators
− <, <=, >, >=, ==, and !=
• work with numbers and strings
− return True or False
13
Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Lesser than or equal to x <= y
14
Comparing Strings
− using ASCII
ordering, the pairs
of characters at
each position in
the two strings are
compared
• 'a' is less than 'b’,
but 'A' is less than
'a'
15
Chained Comparison
Operators
16
− operators can be arranged arbitrarily, used as a chain
− example: x < y < z is similar to x < y and y < z
17
Precedence and
Associativity of Arithmetic
Operations
18
Operator Description
lowest or Boolean OR
precedence
and Boolean AND
not Boolean NOT
==, !=, <, <=, >, >=, is
, is not comparisons, identity
| bitwise OR
^ bitwise XOR
& bitwise AND
<<, >> bit shifts
+, - addition, subtraction
*, /, //, % multiplication, division,
floor division, modulo
+x, -x, ~x unary positive, unary
negation, bitwise negation
highest ** exponentiation
precedence
19
20
Associativity of Operators
❑ Associativity
− order of evaluating multiple operators with the same
precedence
21
❑ Almost all operators have left-to-right associativity
❑ Example:
− * and // have the same precedence, if both are present in the
expression, the left one should be evaluated first
22
❑ ** has right-to-left associativity
23
❑ Assignment and comparison operators do not have
associativity in Python.
24
Logical Operators
25
Operator Description Example
and Returns True if both x < 9 and x < 15
statements are true
or Returns True if one of the x < 7 or x < 5
statements is true
not Reverse the result, returns not(x < 12 and x < 8)
False if the result is true
26
Identity Operators
27
Operator Description Example
is Returns True if both variables are the x is y
same object
is not Returns True if both variables are not x is not y
the same object
28
29
Membership Operators
30
Operator Description Example
in Returns True if a sequence with the x in y
specified value is present in the
object
not in Returns True if a sequence with the x not in y
specified value is not present in the
object
31
32
Bitwise Operators
33
Operator Name Description Example
& Sets each bit to 1 if both bits
AND x & y
are 1
| Sets each bit to 1 if one of two
OR x | y
bits is 1
^ Sets each bit to 1 if only one of x ^ y
XOR
two bits is 1
~ NOT Inverts all the bits ~x
Shift left by pushing zeros in
<< Zero fill left shift from the right and let the x << 2
leftmost bits fall off
Shift right by pushing copies of
>> Signed right the leftmost bit in from the left, x >> 2
shift and let the rightmost bits fall
off
34
1 AND 2 3 AND 5
1 = 01 3 = 0011
2 = 10 5 = 0101
00 0001
35
1 OR 2 3 OR 5
1 = 01 3 = 0011
2 = 10 5 = 0101
11 0111
36
1 XOR 2 3 XOR 5
1 = 01 3 = 0011
2 = 10 5 = 0101
11 0110
37
1 << 2 3 >> 2
1 =0001 3 = 0011
0100 0000
38
39