Python Operators
Python has a wide range of operators that can be used to perform various operations on values and variables. The arithmetic and assignment operators are handled in separate chapters.
Comparison Operators
Comparison operators are used to compare the values of two objects. They return a Boolean value indicating whether the comparison is true or false.
The following comparison operators are available in Python:
- ==: equal to
- !=: not equal to
: greater than
- <: less than
=: greater than or equal to
- <=: less than or equal to
Here are some examples of using comparison operators in Python:
# Example 1: equal to x = 5 y = 7 print(x == y) # Output: False # Example 2: not equal to x = 5 y = 7 print(x != y) # Output: True # Example 3: greater than x = 5 y = 7 print(x > y) # Output: False # Example 4: less than x = 5 y = 7 print(x < y) # Output: True # Example 5: greater than or equal to x = 5 y = 7 print(x >= y) # Output: False # Example 6: less than or equal to x = 5 y = 7 print(x <= y) # Output: True
Logical Operators
Logical operators are used to perform logical operations on Boolean values. They return a Boolean value indicating the result of the operation.
The following logical operators are available in Python:
- and: returns True if both operands are True, otherwise returns False
- or: returns True if either operand is True, otherwise returns False
- not: returns the negation of the operand (i.e. if the operand is True, returns False, and vice versa)
- Here are some examples of using logical operators in Python:
# Example 1: and x = True y = False print(x and y) # Output: False # Example 2: or x = True y = False print(x or y) # Output: True # Example 3: not x = True print(not x) # Output: False
Identity Operators
Identity operators are used to compare the identity of two objects. They return a Boolean value indicating whether the objects are the same object in memory.
The following identity operators are available in Python:
is: returns True if both operands are the same object in memory, otherwise returns False is not: returns True if the operands are not the same object in memory, otherwise returns False Here are some examples of using identity operators in Python:
# Example 1: is x = [1, 2, 3] y = x print(x is y) # Output: True # Example 2: is not x = [1, 2, 3] y = [1, 2, 3] print(x is not y) # Output: True
Membership Operators
Membership operators are used to test whether a value is a member of a sequence (such as a list, tuple, or string). They return a Boolean value indicating whether the value is a member of the sequence.
The following membership operators are available in Python:
in: returns True if the value is a member of the sequence, otherwise returns False not in: returns True if the value is not a member of the sequence, otherwise returns False Here are some examples of using membership operators in Python:
# Example 1: in x = 'hello' print('h' in x) # Output: True # Example 2: not in x = 'hello' print('a' not in x) # Output: True
Bitwise Operators
Bitwise operators are used to perform operations on the individual bits of an integer value. They return an integer value representing the result of the operation.
The following bitwise operators are available in Python:
- &: bitwise and
- |: bitwise or
- ^: bitwise xor
- ~: bitwise not
- <<: left shift
: right shift
Here are some examples of using bitwise operators in Python:
# Example 1: bitwise and x = 0b10101 y = 0b11011 print(x & y) # Output: 0b10001 # Example 2: bitwise or x = 0b10101 y = 0b11011 print(x | y) # Output: 0b11111 # Example 3: bitwise xor x = 0b10101 y = 0b11011 print(x ^ y) # Output: 0b01110 # Example 4: bitwise not x = 0b10101 print(~x) # Output: -0b10110 # Example 5: left shift x = 0b10101 print(x << 2) # Output: 0b1010100 # Example 6: right shift x = 0b10101 print(x >> 2) # Output: 0b101