In Python, operators are symbols or special characters that perform certain operations on one or more values or variables.
For example, the plus (+) operator performs addition between two numbers, while the equal-to (==) operator compares whether two values are equal. Python has several types of operators as given beolow:
Example:
a = 10
b = 5
print(a + b) # Output: 15
print(a - b) # Output: 5
print(a * b) # Output: 50
print(a / b) # Output: 2.0 (floating-point division)
print(a // b) # Output: 2 (integer division)
print(a % b) # Output: 0 (remainder after division)
print(a ** b) # Output: 100000 (a raised to the power of b)
a = 10
b = 5
print(a > b) # Output: True
print(a < b) # Output: False
print(a >= b) # Output: True
print(a <= b) # Output: False
print(a == b) # Output: False
print(a != b) # Output: True
a = 10
b = 5
c = 7
print((a > b) and (b < c)) # Output: True
print((a < b) or (b < c)) # Output: True
print(not(a > b)) # Output: False
a = 10
b = 5
a += b # Equivalent to a = a + b
print(a) # Output: 15
a -= b # Equivalent to a = a - b
print(a) # Output: 10
a *= b # Equivalent to a = a * b
print(a) # Output: 50
a /= b # Equivalent to a = a / b
print(a) # Output: 10.0
a %= b # Equivalent to a = a % b
print(a) # Output: 0.0
a **= b # Equivalent to a = a ** b
print(a) # Output: 0.0
a = 60 # binary value 111100
b = 13 # binary value 1101
print(a & b) # Output: 12 (binary value 1100)
print(a | b) # Output: 61 (binary value 111101)
print(a ^ b) # Output: 49 (binary value 110001)
print(~a) # Output: -61 (complement of binary value 111100)
print(a << 2) # Output: 240 (binary value 11110000)
print(a >> 2) # Output: 15 (binary value 1111)
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # Output: True
print(a is c) # Output: False
print(a is not c) # Output: True
In the above example, a and b refer to the same list object in memory, so a is b is True. However, c is a new list object with the same values as a, so a is c is False.
a = [1, 2, 3]
b = 2
c = 4
print(b in a) # Output: True
print(c in a) # Output: False
print(b not in a) # Output: False
print(c not in a) # Output: True
In the above example, b is a member of the list a, so b in a is True. However, c is not a member of a, so c in a is False.
a = 10
b = 5
c = "a is greater than b" if a > b else "a is less than or equal to b"
print(c) # Output: "a is greater than b"
In the above example, if a is greater than b, the value of c will be "a is greater than b", otherwise it will be "a is less than or equal to b".
a = None
b = 10
c = a or b
print(c) # Output: 10
In the above example, a is None, so c takes the value of b, which is 10.
a = "Hello, World!"
b = [1, 2, 3, 4, 5]
print(a[2:7]) # Output: "llo, "
print(b[1:4]) # Output: [2, 3, 4]
print(b[:2]) # Output: [1, 2]
print(b[2:]) # Output: [3, 4, 5]
print(b[:-1]) # Output: [1, 2, 3, 4]
In the above example, a[2:7] extracts a slice of a from the third character up to (but not including) the eighth character. Similarly, b[1:4] extracts a slice of b from the second element up to (but not including) the fifth element. b[:2] extracts a slice of b from the beginning up to (but not including) the third element, and b[2:] extracts a slice of b from the third element to the end. b[:-1] extracts a slice of b from the beginning up to (but not including) the last element.
Advertisement
Advertisement