Understanding Operators in Python
This is the 3rd post in a series of learning the Python programming language.
Operators in Python are special symbols that are used to perform certain operations on one or more operands. The operands can be variables, values, or expressions.
There are several types of operators in Python, including:
Arithmetic operators
Arithmetic operators are used for performing mathematical operations on one or more operands, which can be variables, values, or expressions.
Addition (+)
The addition operator is used to add two or more numbers. You can also use the addition operator with variables and expressions.
x = 5
y = 3
z = x + y
print(z) # Output: 8
Subtraction (-)
The subtraction operator is used to subtract one number from another.
x = 5
y = 3
z = x - y
print(z) # Output: 2
Multiplication (*)
The multiplication operator is used to multiply two or more numbers.
x = 5
y = 3
z = x * y
print(z) # Output: 15
Division (/)
The division operator is used to divide one number by another.
x = 5
y = 3
z = x / y
print(z) # Output: 1.6666666666666667
Floor Division (//)
The floor division operator is used to divide one number by another but rounds down to the nearest whole number.
x = 5
y = 3
z = x // y
print(z) # Output: 1
Modulus (%)
The modulus operator is used to get the remainder when one number is divided by another.
x = 5
y = 3
z = x % y
print(z) # Output: 2
Exponent (**)
The exponent operator is used to raise a number to a power.
x = 5
y = 3
z = x ** y
print(z) # Output: 125
Comparison operators
These operators are used to compare values and determine if they are equal, not equal, greater than, less than, greater than or equal to, or less than or equal to.
Equal (==)
The equal operator is used to determine if two values are equal.
x = 5
y = 3
z = x == y
print(z) # Output: False
Not Equal (!=)
The not equal operator is used to determine if two values are not equal.
x = 5
y = 3
z = x != y
print(z) # Output: True
Greater than (>)
The greater than operator is used to determine if one value is greater than another.
x = 5
y = 3
z = x > y
print(z) # Output: True
Less than (<)
The less than operator is used to determine if one value is less than another.
x = 5
y = 3
z = x < y
print(z) # Output: False
Greater than or equal to (>=)
The ‘greater than or equal to’ operator is used to determine if one value is greater than or equal to another.
x = 5
y = 3
z = x >= y
print(z) # Output: True
Less than or equal to (<=)
The ‘less than or equal to’ operator is used to determine if one value is less than or equal to another.
x = 5
y = 3
z = x <= y
print(z) # Output: False
Logical operators
These operators are used to perform logical operations such as and, or, and not.
AND (and
)
The and
operator returns True
if both the operands are true and False
if any of the operands is false.
x = 5
y = 3
z = x > 2 and y < 4
print(z) # Output: True
OR (or
)
The or
operator returns True
if any of the operands is true and False
if both operands are false.
x = 5
y = 3
z = x > 2 or y < 2
print(z) # Output: True
NOT (not
)
The not
operator is used to reverse the logical state of its operand. If a condition is true then not
operator will make false.
x = 5
y = 3
z = not x > y
print(z) # Output: False
Assignment operators
These operators are used to assign values to variables.
Basic assignment (=
)
The basic assignment operator is used to assign a value to a variable.
x = 5
print(x) # Output: 5
Addition assignment (+=
)
The addition assignment operator is used to add a value to a variable and assign the result to the variable.
x = 5
x += 3 # It's equivalent to x = x + 3
print(x) # Output: 8
Subtraction assignment (-=
)
The subtraction assignment operator is used to subtract a value from a variable and assign the result to the variable.
x = 5
x -= 3 # It's equivalent to x = x - 3
print(x) # Output: 2
Multiplication assignment (*=
)
The multiplication assignment operator is used to multiply a variable by a value and assign the result to the variable.
x = 5
x *= 3 # It's equivalent to x = x * 3
print(x) # Output: 15
Division assignment (/=
)
The division assignment operator is used to divide a variable by a value and assign the result to the variable.
x = 5
x /= 3 # It's equivalent to x = x / 3
print(x) # Output: 1.6666666666666667
Floor division assignment (//=
)
The floor division assignment operator is used to divide a variable by a value and assign the floor division result to the variable.
x = 5
x //= 3 # It's equivalent to x = x // 3
print(x) # Output: 1
Modulus assignment (%=
)
The modulus assignment operator is used to divide a variable by a value and assign the remainder to the variable.
x = 5
x %= 3 # It's equivalent to x = x % 3
print(x) # Output: 2
Exponent assignment (**=
)
The exponent assignment operator is used to raise a variable to a power and assign the result to the variable.
x = 5
x **= 3 # It's equivalent to x = x ** 3
print(x) # Output: 125
Bitwise operators
These operators are used to perform bit-level operations on integers.
Bitwise AND (&)
The bitwise AND operator compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
x = 5 # binary: 0101
y = 3 # binary: 0011
z = x & y
print(z) # Output: 1
Bitwise OR (|)
The bitwise OR operator compares each bit of the first operand to the corresponding bit of the second operand. If either bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
x = 5 # binary: 0101
y = 3 # binary: 0011
z = x | y
print(z) # Output: 7
Bitwise XOR (^)
The bitwise XOR operator compares each bit of the first operand to the corresponding bit of the second operand. If the bits are different, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
x = 5 # binary: 0101
y = 3 # binary: 0011
z = x ^ y
print(z) # Output: 6
Bitwise NOT (~)
The bitwise NOT operator inverts all the bits of the operand.
x = 5 # binary: 0101
z = ~x
print(z) # Output: -6
Left Shift (<<)
The left shift operator shifts the bits of the operand to the left by the number of positions specified by the second operand.
x = 5 # binary: 0101
y = 2
z = x << y
print(z) # Output: 20
Right Shift (>>)
The right shift operator shifts the bits of the operand to the right by the number of positions specified by the second operand.
x = 20 # binary: 010100
y = 2
z = x >> y
print(z) # Output: 5
Membership operators
Membership operators are used to test whether a value is a member of a sequence (such as a string, list, or tuple) or a set.
in
The in
operator is used to test if a value is present in a sequence.
x = [1, 2, 3, 4, 5]
y = 3
z = y in x
print(z) # Output: True
not in
The not in
operator is used to testing if a value is not present in a sequence.
x = [1, 2, 3, 4, 5]
y = 6
z = y not in x
print(z) # Output: True
Membership operators can also be used with strings, in which case they check if the given substring is present in the string or not.
x = "Hello World"
y = "World"
z = y in x
print(z) # Output: True
The membership operator can also be used with sets. A set is a collection of unique elements, so it can be used to check if an element is present or not in a set.
x = {1, 2, 3, 4, 5}
y = 3
z = y in x
print(z) # Output: True
Identity operators
Identity operators are used to comparing the memory addresses of two objects.
is
The is
operator compares the memory addresses of two objects. It returns True
if both objects are the same object, and False
if they are different objects.
x = [1, 2, 3]
y = x
z = (x is y)
print(z) # Output: True
is not
The is not
operator compares the memory addresses of two objects. It returns True
if both objects are different objects, and False
if they are the same object.
x = [1, 2, 3]
y = [1, 2, 3]
z = (x is not y)
print(z) # Output: True
It’s important to notice that the is
operator checks for the identity of the object, while the ==
operator checks for the equality of the values of the objects.
x = [1, 2, 3]
y = [1, 2, 3]
print(x == y) # True
print(x is y) # False
Identity operators are useful when you want to check if two variables are referencing the same object in memory. It’s also important to note that in Python when you create a new variable and assign it to an existing object, it creates a new reference to the existing object, not a new copy of the object.
If you like the post, don’t forget to clap. If you’d like to connect, you can find me on LinkedIn.