0% found this document useful (0 votes)
5 views40 pages

Understanding Operators in Programming

This document provides an overview of various types of operators in programming, including arithmetic, comparison, logical, identity, membership, and bitwise operators. It explains operator precedence, associativity, and includes examples for each operator type. Additionally, it covers shortcut arithmetic operators and the concept of increment and decrement operators.

Uploaded by

romeothird15
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views40 pages

Understanding Operators in Programming

This document provides an overview of various types of operators in programming, including arithmetic, comparison, logical, identity, membership, and bitwise operators. It explains operator precedence, associativity, and includes examples for each operator type. Additionally, it covers shortcut arithmetic operators and the concept of increment and decrement operators.

Uploaded by

romeothird15
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Operators

MODULE 3
CCS 1400
❑ Explain the different types of
operators

❑ Perform operations using the


different types of operators

Objectives
❑ 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
Increment and
assignment
decrement operators
operators

7
Operator Name Example
= Assignment Operator a=7
+= Addition Assignment a += 1 #a=a+1
-= Subtraction Assignment a -= 3 #a=a–3

Multiplication
*= a *= 4 #a=a*4
Assignment
Float Division
/= a /= 3 #a=a/3
Assignment
%= Remainder Assignment a %= 10 # a = a % 10
**= Exponent Assignment a **= 10 # a = a ** 10
Floor Division
//= a //= 8 # a = a // 8
Assignment

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
unary positive, unary
+x, -x, ~x
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
Returns True if both
and x < 9 and x < 15
statements are true
Returns True if one of the
or x < 7 or x < 5
statements is true
Reverse the result, returns
not 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 x is y
the same object
is not Returns True if both variables are x is not y
not 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
& AND Sets each bit to 1 if both bits are 1 x&y
Sets each bit to 1 if one of two bits
| OR x|y
is 1
Sets each bit to 1 if only one of two
^ XOR x^y
bits is 1
~ NOT Inverts all the bits ~x
Shift left by pushing zeros in from
<< Zero fill left shift the right and let the leftmost bits x << 2
fall off
Shift right by pushing copies of the
>> Signed right shift leftmost bit in from the left, and let x >> 2
the rightmost bits fall off

34
Decimal Binary Hexadecimal
0 0000 0
1 0001 1
2 0010 2
3 0011 3 1 0 1 1 0 1 0 1
4 0100 4
5 0101 5
6 0110 6 128 64 32 16 8 4 2 1
7 0111 7
8 1000 8
101101012 = 128 + 32 + 16 + 4 + 1
9 1001 9
101101012 = 18110
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F
35
1&2 3&5
1 = 01 3 = 0011
2 = 10 5 = 0101
00 0001

36
1|2 3|5
1 = 01 3 = 0011
2 = 10 5 = 0101
11 0111

37
1^2 3^5
1 = 01 3 = 0011
2 = 10 5 = 0101
11 0110

38
1 << 2 3 >> 2
1 =0001 3 = 0011
0100 0000

39
40

You might also like