Skip to main content
Python Operators With Examples

Python Operators With Examples

Operators are symbols that perform arithmetic and other operations. Operators are fundamental concepts that all programmers must understand. Python has several types of operators. So, what exactly are the Python operators?

This time, we’ll talk about the Python language’s operators.

Arithmetic Operators

Arithmetic operators are operators that are used to perform mathematical operations. Addition, subtraction, multiplication, and so on are examples of mathematical operations. The arithmetic operators’ symbols and descriptions are as follows:

OperatorDescription
*Addition
Subtraction
*Multiplication
/Division (float)
%Modulus
**Power
//Division (floor)
arithmetic operator symbols

Example of arithmetic operators:

print(2 + 3) # addition
print(2 - 3) # subtraction
print(2 * 3) # multiplication
print(3 / 2) # division (float)
print(3 % 2) # modulus
print(3 ** 2) # power
print(3 // 2) # division (floor)

Output:

5
-1
6
1.5
1
9
1

Comparison Operators

A value is compared using the comparison operator. This operator will return True or False based on the comparison results. This operator is typically used in the condition of a branch. The following are the comparison operator symbols:

OperatorDescription
>Greater than
<Less than
==Equal to
>=Greater than or equal to
<=Less than or equal to
!=Not equal to
isx is the same as y
is notx is not the same as y
comparison operator symbols

Example of comparison operators:

a = 2
b = 3

print(a > b) # greater than
print(a < b) # less than
print(a == b) # equal to
print(a >= b) # greater than or equal to
print(a <= b) # less than or equal to
print(a != b) # not equal to
print(a is b) # a is the same as b
print(a is not b) # a is not the same as b

Output:

False
True
False
False
True
True
False
True

Related post: Check Prime Number in Python With Flowchart


Logical Operators

Logical operators are operators that are used to combine multiple conditional statements. These operators include logical AND, logical OR, and logical NOT. The symbols used are as follows:

OperatorDescription
andLogical AND
orLogical OR
notLogical NOT
logical operator symbols

Example of logical operators:

a = True
b = False

print(a and b) # logical AND
print(a or b) # logical OR
print(not a) # Logical NOT

Output:

False
True
False

Bitwise Operators

The bitwise operator is a type of operator that is used to perform binary number operations. This operation is carried out bit by bit. As for some symbols used in bitwise operations, they are as follows:

OperatorDescription
&Bitwise AND
|Bitwise OR
~Bitwise NOT
^Bitwise XOR
>>Bitwise right shift
<<Bitwise left shift
bitwise operator symbols

Example of bitwise operators:

a = 10
b = 6

print(a & b) # bitwise AND
print(a | b) # bitwise OR
print(~a) # bitwise NOT
print(a ^ b) # bitwise XOR
print(a >> 2) # bitwise right shift
print(a << 2) # bitwise left shift

Output:

2
14
-11
12
2
40

Assignment Operators

A type of operator that is used to assign a value to a variable is the assignment operator. This assignment operator employs the following symbols:

OperatorDescription
=Assign value of right side of expression to left side operand
+=Add AND: Add right-side operand with left side operand and then assign to left operand
-=Subtract AND: Subtract right operand from left operand and then assign to left operand
*=Multiply AND: Multiply right operand with left operand and then assign to left operand
/=Divide (float) AND: Divide left operand with right operand and then assign to left operand
%=Modulus AND: Takes modulus using left and right operands and assign the result to left operand
**=Exponent AND: Calculate exponent (raise power) value using operands and assign value to left operand
//=Divide (floor) AND: Divide left operand with right operand and then assign the value (floor) to left operand
&=Performs bitwise AND on operands and assign value to left operand
|=Performs bitwise OR on operands and assign value to left operand
^=Performs bitwise XOR on operands and assign value to left operand
>>=Performs bitwise right shift on operands and assign value to left operand
<<=Performs bitwise left shift on operands and assign value to left operand
assignment operator symbols

Example of assignment operators:

a = 2

# assign value
b = a
print(b)

# add AND
b += a
print(b)

# subtract AND
b -= a
print(b)

# multiply AND
b *= a
print(b)

# bitwise left shift
b <<= a
print(b)

Output:

2
4
2
4
16

Related post: Python Data Types With Examples


Membership Operators

The membership operation is used to determine whether or not a value exists in a variable. There are two membership operators in Python in and not in.

OperatorDescription
inReturn True if value is found in the sequence
not inReturn True if value is not found in the sequence
membership operator symbols

Example of membership operators:

ls = [1, 5, 3, 7]
a = 2

print(a in ls)
print(a not in ls)

Output:

False
True

Conclusion

These are some of the operators available in the Python programming language. You can see other python operators on W3Schools. If you have any questions, please leave them in the comments section.

Thank you. Hopefully, the discussion this time will add to our understanding.

Share this

Shafy Gunawan

I’m a web developer. I spend my whole day, practically every day, experimenting with HTML, CSS, JavaScript, and PHP; dabbling with Python programming language; and share the knowledge that I learned.

Leave a Reply

Your email address will not be published. Required fields are marked *