Day05-Python Operators # 2
Arithmetic, Assignment, Comparison, Logical, Identity, Membership, Bitwise
Table of contents
PART # 2
Logical Operators:
Logical operators are used to combine conditional statements. It tells whether the statement is True or False.
Operator | Description | Example |
and | Returns True if both statements are true | x < 5 and x < 10 |
or | Returns True if one of the statements is true | x < 5 or x < 4 |
not | Reverse the result, returns False if the result is true | not(x < 5 and x < 10) |
- and operator: Returns True if both statements are true
x = 5
print(x > 3 and x < 10) #output= True
# returns True => because 5 is greater than 3 and 5 also is less than 10
- or operator: Returns True if one of the statements is true
x = 5
print(x > 3 or x < 4) #output = True
# returns True => because one of the conditions are true --> 5 is greater than 3
# but 5 is not less than 4
- not operator: Reverse the result, returns False if the result is true and returns True if the result is false.
x = 5
print(not(x > 3 and x < 10))
# 5 is greater than 3 and 5 also is less than 10
# returns False => because both statements are true
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:
Operator | Description | Example |
is | Returns True if both variables are the same object | x is y |
is not | Returns True if both variables are not the same object | x is not y |
- is operator:
x = ["apple", "banana"]
y = ["apple", "banana"]
z = x
print(x is z) #returns True
# because z is the same object as x
print(x is y) #returns False
# because x is not the same object as y, even if they have the same content
print(x == y) #returns True
# to demonstrate the difference betweeen "is" and "==":
# this comparison returns True because x is equal to y
- is not operator:
x = ["apple", "banana"]
y = ["apple", "banana"]
z = x
print(x is not z) # returns False
# because z is the same object as x
print(x is not y) # returns True
# because x is not the same object as y, even if they have the same content
print(x != y) # returns False
# to demonstrate the difference betweeen "is not" and "!=":
# this comparison returns False because x is equal to y
Python Membership Operators:
Operator | Description | Example |
in | Returns True if a sequence with the specified value is present in the object | x in y |
not in | Returns True if a sequence with the specified value is not present in the object | x not in y |
- in operator:
x = ["apple", "banana"]
print("banana" in x) # returns True
# returns True => because a sequence with the value "banana" is in the list
- not in operator:
x = ["apple", "banana"]
print("pineapple" not in x) # returns True
# returns True => because a sequence with the value "pineapple" is not in the list
Bitwise Operators:
Bitwise operators are used to compare (binary) numbers:
- AND operator (&):
The bitwise AND operator & returns a binary number whose bits are set to 1 only if the corresponding bits of both its operands are 1
print(6 & 3) #output= 2
In this case, 6 and 3 are represented in binary as 110 and 011, respectively. Performing a bitwise AND operation on these two numbers results in 010, which is equivalent to the decimal number 2. Therefore, the output of the given Python code is 2.
- OR operator ( | ):
The bitwise OR operator | returns a binary number whose bits are set to 1 if either of the corresponding bits of its operands is 1.
print(6 | 3) #output= 7
In this case, 6 and 3 are represented in binary as 110 and 011, respectively. Performing a bitwise OR operation on these two numbers results in 111, which is equivalent to the decimal number 7. Therefore, the output of the given Python code is 7.
- XOR operator ( ^ ):
The bitwise XOR operator ^ returns a binary number whose bits are set to 1 only if the corresponding bits of its operands are different.
print(6 ^ 3) #output= 5
In this case, 6 and 3 are represented in binary as 110 and 011, respectively. Performing a bitwise XOR operation on these two numbers results in 101, which is equivalent to the decimal number 5. Therefore, the output of the given Python code is 5.
- NOT operator (~):
The tilde operator ~ in Python is a unary operator that performs bitwise inversion. It reverses all the bits in a given number, i.e., all ones become zeros and all zeros become ones
print(~3) #output= -4
In this case, the number 3 is represented in binary as 011. Inverting the bits of 3 results in 100, which is equivalent to the decimal number -4 1. Therefore, the output of the given Python code is -4.
- Zero fill left shift opera1tor (<<):
The bitwise left shift operator << shifts the bits of its first operand to the left by the number of positions specified in its second operand.
print(3 << 2) #output= 12
In this case, 3 is represented in binary as 011. Shifting the bits of 3 two positions to the left results in 1100, which is equivalent to the decimal number 12. Therefore, the output of the given Python code is 12.
- Signed right shift (>>):
The bitwise right shift operator >> shifts the bits of its first operand to the right by the number of positions specified in its second operand.
print(8 >> 2) #output= 2
In this case, 8 is represented in binary as 1000. Shifting the bits of 8 two positions to the right results in 0010, which is equivalent to the decimal number 2. Therefore, the output of the given Python code is 2.
Quick Quiz:
Example # 1
print((6 + 3) - (6 + 3)) #output =0
# Parenthesis have the highest precedence, and need to be evaluated first.
# The calculation above reads 9 - 9 = 0
Example # 2
print(100 + 5 * 3) #output = 115
# Multiplication has higher precedence than addition, and needs to be evaluated first.
# The calculation above reads 100 + 15 = 115
<< Previously -------------------------------------------------- Next Lesson >>