Operators in Python

In Python, operators are symbols or keywords that allow you to perform various operations on variables and values. There are several types of operators in Python, each with its own specific functionality. In this article, we’ll cover everything you need to know about Python operators.

Arithmetic Operators: Arithmetic operators are used to perform mathematical operations on numeric values. The following table lists the arithmetic operators in Python:

OperatorDescription
+Addition
Subtraction
*Multiplication
/Division (returns a float)
//Integer Division (returns an integer)
%Modulo (returns the remainder after division)
**Exponentiation

Here’s an example that uses arithmetic operators:

x = 10
y = 3

print(x + y)   # Output: 13
print(x - y)   # Output: 7
print(x * y)   # Output: 30
print(x / y)   # Output: 3.3333333333333335
print(x // y)  # Output: 3
print(x % y)   # Output: 1
print(x ** y)  # Output: 1000

Comparison Operators: Comparison operators are used to compare two values and return a Boolean value (True or False) based on the comparison. The following table lists the comparison operators in Python:

OperatorDescription
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Here’s an example that uses comparison operators:

x = 10
y = 5

print(x == y)   # Output: False
print(x != y)   # Output: True
print(x > y)    # Output: True
print(x < y)    # Output: False
print(x >= y)   # Output: True
print(x <= y)   # Output: False

Logical Operators: Logical operators are used to combine multiple Boolean expressions and return a Boolean value based on the result. The following table lists the logical operators in Python:

OperatorDescription
andReturns True if both expressions are True
orReturns True if either expression is True
notReturns True if the expression is False

Here’s an example that uses logical operators:

x = 5
y = 10
z = 15

print(x < y and y < z)   # Output: True
print(x < y or y > z)    # Output: True
print(not(x < y))        # Output: False

Assignment Operators: Assignment operators are used to assign a value to a variable. The following table lists the assignment operators in Python:

OperatorDescription
=Assigns a value to a variable
+=Adds a value to a variable and assigns the result
-=Subtracts a value from a variable and assigns the result
*=Multiplies a variable by a value and assigns the result
/=Divides a variable by a value and assigns the result
//=Performs integer division on a variable and assigns the result
%=Computes the modulus of a variable and assigns the result
**=Raises a variable to a power and assigns the result

Bitwise Operators: Bitwise operators are used to perform operations on the binary representation of numbers. The following table lists the bitwise operators in Python:

OperatorDescription
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise NOT
<<Bitwise Left Shift
>>Bitwise Right Shift

Here’s an example that uses bitwise operators:

x = 10    # Binary representation: 0b1010
y = 5     # Binary representation: 0b0101

print(x & y)    # Output: 0
print(x | y)    # Output: 15
print(x ^ y)    # Output: 15
print(~x)       # Output: -11
print(x << 1)   # Output: 20
print(x >> 1)   # Output: 5

Operator Precedence: Operator precedence refers to the order in which operators are evaluated in an expression. Python follows a specific order of precedence for its operators, which is shown in the following table (from highest to lowest precedence):

OperatorDescription
**Exponentiation
+x, -xPositive, Negative
*, /, //, %Multiplication, Division, Integer Division, Modulo
+, –Addition, Subtraction
<<, >>Bitwise Left Shift, Bitwise Right Shift
&Bitwise AND
^Bitwise XOR
|Bitwise OR
==, !=, >, <, >=, <=Comparison Operators
notLogical NOT
andLogical AND
orLogical OR
=, +=, -=, *=, /=, //=, %=, **=Assignment Operators

When an expression contains multiple operators with different precedence levels, Python evaluates them in the order specified by the table above. Parentheses can be used to group expressions and override the default precedence order.

Operators are an essential part of any programming language, and Python is no exception. By understanding the different types of operators available in Python and how they are used, you can write more complex and efficient code. Make sure to keep operator precedence in mind when writing expressions with multiple operators to ensure that your code executes as expected.

The codes mentioned in this post can be downloaded from github.com. Share the post with someone you think can benefit from the information.

Leave a Comment

Share this