Python Lecture 3
Python Lecture 3
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
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
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