[Python] Operator Meaning: Exploring Types and Functions

Operators in Python are essential for constructing meaningful programs. Similar to mathematical symbols, Python operators serve specific purposes. In this article will explore various types of operators in Python, explaining their functions and presenting sample code for clearer understanding.

Types of Operators in Python

Arithmetic Operators

Arithmetic operators are employed for basic mathematical operations. These operators facilitate primary calculations such as addition, subtraction, multiplication, and division between two operands.

python
a = 5
b = 3

print(a + b)  # Addition
print(a - b)  # Subtraction
print(a * b)  # Multiplication
print(a / b)  # Division
8
2
15
1.6666666666666667
OperatorOperationExampleResultDescription
+Addition5 + 813Adds values on either side of the operator
-Subtraction8 - 53Subtracts right operand from the left
*Multiplication5 * 840Multiplies values on either side of the operator
/Division8 / 24Divides left operand by the right one
%Modulus8 % 53Returns remainder of the division
//Floor Division8 // 51Division that results into whole number adjusted to the left in the number line
**Exponentiation2 ** 38Performs exponential (power) calculation on operators

Relational Operators

Relational operators are used to compare the values of two operands. Through these operators, evaluations are made to determine if two values are equal, if one value is greater than the other, or if one is less than the other. They return a Boolean value based on the result of the comparison.

python
print(a > b)  # Greater than
print(a < b)  # Less than
print(a == b) # Equal to
True
False
False
OperatorOperationExampleResultDescription
==Equal to5 == 8FalseTrue if values of two operands are equal
!=Not equal to5 != 8TrueTrue if values of two operands are not equal
>Greater than8 > 5TrueTrue if left operand is greater than the right
<Less than5 < 8TrueTrue if left operand is less than the right
>=Greater than or equal to8 >= 5TrueTrue if left operand is greater than or equal to the right
<=Less than or equal to5 <= 8TrueTrue if left operand is less than or equal to the right

Logical Operators

Logical operators evaluate the truth or falsity of multiple conditions. These operators are primarily used to control the flow of a program by combining various conditions to produce a final logical outcome (either true or false).

python
x = True
y = False

print(x and y) # Logical AND
print(x or y)  # Logical OR
print(not x)   # Logical NOT
False
True
False
OperatorOperationExampleResultDescription
andLogical ANDTrue and FalseFalseTrue if both operands are true
orLogical ORTrue or FalseTrueTrue if at least one of the operands is true
notLogical NOTnot TrueFalseTrue if operand is false (complements the operand)

Bitwise Operators

Bitwise operators act on the binary representation of numbers. They perform operations on individual bits of the operands, facilitating logic operations, shifts, and negations at the bit level.

python
print(a & b)   # Bitwise AND
print(a | b)   # Bitwise OR
print(a ^ b)   # Bitwise XOR
1
7
6
OperatorOperationExampleResultDescription
&Bitwise AND5 & 31Bits that are set in both operands are set
|Bitwise OR5 | 37Bits that are set in either operand are set
^Bitwise XOR5 ^ 36Bits that are set in one operand but not both
~Bitwise NOT~5-6Bits that are set are not set, and vice versa
<<Left Shift5 << 110Shifts the bits of the number to the left
>>Right Shift5 >> 12Shifts the bits of the number to the right

Assignment Operators

Assignment operators are used to assign values to variables. Beyond basic assignments, these operators also offer functionalities that combine operations with assignments. For instance, they can add a specific number to a value and then assign the result to a variable.

python
a += 2  # Increment value of a by 2
b -= 1  # Decrement value of b by 1
a = 7
b = 2
OperatorOperationExampleResultDescription
=Assignx = 77Assigns value from the right operand to the left operand
+=Add and assignx += 3x + 3Adds right operand to the left operand and then assigns the result to the left operand
-=Subtract and assignx -= 3x - 3Subtracts right operand from the left operand and then assigns the result to the left operand
*=Multiply and assignx *= 3x * 3Multiplies left operand by the right operand and then assigns the result to the left operand
/=Divide and assignx /= 3x / 3Divides left operand by the right operand and then assigns the result to the left operand
%=Modulus and assignx %= 3x % 3Takes the modulus of the left operand with the right operand and then assigns the result to the left operand
//=Floor divide and assignx //= 3x // 3Floor divides left operand by the right operand and then assigns the result to the left operand
**=Exponentiate and assignx **= 3x ** 3Raises the left operand to the power of the right operand and then assigns the result to the left operand

Membership Operators

Membership operators test for the presence of a value within a sequence, such as a list, string, or tuple. They are mainly employed to check if a particular element exists within a given sequence.

python
list = [1, 2, 3, 4, 5]
print(3 in list)    # Check if 3 is in list
print(6 not in list) # Check if 6 is not in list
True
True
OperatorOperationExampleResultDescription
inEvaluates to true if it finds a variable in the specified sequence and false otherwise5 in [1, 2, 3, 4, 5]TrueTrue if value/variable is found in the sequence
not inEvaluates to true if it doesn’t finds a variable in the specified sequence and false otherwise6 not in [1, 2, 3, 4, 5]TrueTrue if value/variable is not found in the sequence

Identity Operators

Identity operators are used to compare the memory locations of two objects. Their primary use is to determine if two variables refer to the same object in memory.

python
x = [1, 2, 3]
y = x

print(y is x)       # True since y and x refer to the same object
print(y is not x)   # False for the same reason
True
False
OperatorOperationExampleResultDescription
isEvaluates to true if the variables on either side of the operator point to the same object and false otherwisex is yDepends on the values of x and yTrue if both variables are the same object
is notEvaluates to false if the variables on either side of the operator point to the same object and true otherwisex is not yDepends on the values of x and yTrue if both variables are not the same object

Understanding Python operators is pivotal for efficient programming. Whether comparing variables, adjusting values, or performing calculations, Python's versatile operators are at the core of these operations.


FAQs

  1. What's the difference between == and = in Python?
    • == compares the values of two operands to determine equality, returning a boolean (True or False). For example, 7 == 7 results in True. On the other hand, = is used to assign the value of the right operand to the variable on the left.
  2. How does the floor division // operator work differently from the regular division / operator?
    • The / operator divides and returns a floating-point result. For instance, 5 / 2 yields 2.5. In contrast, // divides and rounds down to the nearest whole number, so 5 // 2 gives 2.
  3. When should I use bitwise operators like & or |?
    • Bitwise operators perform operations directly on the binary representations of numbers. They're useful in specific scenarios like manipulating individual bits in data processing or low-level programming.
  4. Why use the is operator instead of ==?
    • The is operator checks if two variables reference the exact same object in memory, not just if they have equal values. For instance, two lists with identical values might be True for ==, but False for is if they're different objects in memory.
  5. How do in and not in work with different Python data types?
    • Membership operators test for presence. In strings, in verifies if a substring exists. In lists or tuples, it checks if an item is present. For example, 'apple' in 'pineapple' is True, and 3 in [1, 2, 3] is also True.
© Copyright 2023 CLONE CODING