0% found this document useful (0 votes)
3 views9 pages

Python Lecture 3

The document outlines various types of operators used in programming, including relational, logical, identity, membership, and bitwise operators, along with examples and their results. It explains how these operators function, such as comparing values, combining logical expressions, and performing operations on bits. Additionally, it briefly mentions operator precedence.

Uploaded by

Gayan Indunil
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)
3 views9 pages

Python Lecture 3

The document outlines various types of operators used in programming, including relational, logical, identity, membership, and bitwise operators, along with examples and their results. It explains how these operators function, such as comparing values, combining logical expressions, and performing operations on bits. Additionally, it briefly mentions operator precedence.

Uploaded by

Gayan Indunil
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
You are on page 1/ 9

Operators

1
Logic
◼ Many logical expressions use relational operators:
Operator Meaning Example Result
== equals 1 + 1 == 2 True
!= does not equal 3.2 != 2.5 True
< less than 10 < 5 False
> greater than 10 > 5 True
<= less than or equal to 126 <= 100 False
>= greater than or equal to 5.0 >= 5.0 True

◼ Logical expressions can be combined with logical operators:


Operator Example Result
and 9 != 6 and 2 < 3 True
or 2 == 3 or -1 < 5 True
not not 7 > 0 False

2
Arithmetic Operators

3
Assignment operators

4
Identity Operators
◼ Identity operators are used to compare the objects, not if they are equal,
but if they are actually the same object, with the same memory location

>>> x=y=6
>>> x is y
True
>>> x is not y
False
>>>

5
Membership Operators
◼ Membership operators are used to test if a sequence is presented in an
object
>>>x = ["apple", "banana"]
print("banana" in x)
# returns True because a sequence with the value "banana" is in the list

>>>x = ["apple", "banana"]


print("pineapple" not in x)
# returns True because a sequence with the value "pineapple" is not in
the list

6
Bitwise operators

7
Bitwise operators work on bits; that is, they perform a bit-by-bit
operation on operands. Assume a = 60 and b = 13. The following
show their binary representation and 4 bitwise operations on them.

a = 0011 1100
b = 0000 1101
--------------------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011

8
Operators Precedence

You might also like