English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Online Tools

Python Basic Tutorial

Python Functions

Python Data Types

Python Flow Control

O

Python Objects and Classes

Python Date and Time

Advanced Knowledge of Python

Python Operators

Python Reference Manual

In this article, you will learn all about different types of operators in Python, their syntax, and how to use them in examples.

What are operators in Python?

Operators are special symbols in Python used to perform arithmetic or logical calculations. The values operated on by the operators are called operands.

For example: 2+3
5

>>>+Here,2And3is the operator that performs addition.5is the operand,

is the output of the operation.

Arithmetic operators

OperatorMeaningExample
+Arithmetic operators are used to perform mathematical operations, such as addition, subtraction, multiplication, etc. - Additionx + y + 2
-Adds two operands or unary plus - Subtractionx-y- 2
*Subtracts the right operand from the left operand (unary minus) -Multiplicationx * y
/Multiplies two operands - Divisionx / y
Divides the left operand by the right operand (the result is always a float)% -Modulusx % y (the remainder when the left operand is divided by the right operand) / the remainder of y)
//Integer division - Returns the integer part of the quotient (rounded down)x // y
**Power - Returns the y-th power of xx ** y (x to the power of y)

Example1Arithmetic operators in Python

x = 15
y = 4
# Output: x + y = 19
print('x + y =, x+y)
# Output: x - y = 11
print('x - y =, x-y)
# Output: x * y = 60
print('x * y =, x*y)
# Output: x / y = 3.75
print('x / y =, x/y)
# Output: x // y = 3
print('x // y =, x//y)
# Output: x ** y = 50625
print('x ** y =, x**y)

When the program is run, the output is:

x + y = 19
x - y = 11
x * y = 60
x / y = 3.75
x // y = 3
x ** y = 50625

Comparison operators

Comparison operators are used to compare values. They return True or False based on the condition.

OperatorMeaningExample
>Greater-Returns True if the left operand is greater than the right operandx > y
<Less than-Returns True if the left operand is less than the right operandx < y
==Equal-Returns True if both operands are equalx == y
!=Not equal-Returns True if the operands are not equalx != y
>=Greater than or equal to-Returns True if the left operand is greater than or equal to the rightx >= y
<=Less than or equal to-Returns True if the left operand is less than or equal to the rightx <= y

Example2Comparison operators in Python

x = 10
y = 12
# Output: x is greater than y: False
print('x is greater than y: ', x > y)
# Output: x is less than y: True
print('x is less than y: ', x < y)
# Output: x is equal to y: False
print('x is equal to y: ', x == y)
# Output: x is not equal to y: True
print('x is not equal to y: ', x != y)
# Output: x >= y is False
print('x >= y is ', x>=y)
# Output: x <= y is True
print('x <= y is ', x<=y)

Output result

x > y is False
x < y is True
x == y is False
x != y is True
x >= y is False
x <= y is True

Logical operators

Logical operators are and, or, not operators.

OperatorMeaningExample
andIf both operands are true, it is truex and y
orIf any operand is true, it is truex or y
notIf the operand is false, it is True (complement the operand)not x

Example3:Logical operators in Python

x = True
y = False
print('x and y is ', x and y)
print('x or y is ', x or y)
print('not x is ', not x)

Output result

x and y is False
x or y is True
not x is False

This is the truth table for these operators:truth table.

Bitwise operators

Bitwise operators act on operands as if they are strings of binary digits. They run one by one, hence the name.

For example,2is10binary,7is111.

be listed in the table below:Letx= 10(0000 1010binary) andy= 4(0000 0100binary)

OperatorMeaningExample
&bitwise ANDx&y = 0(0000 0000)
|bitwise ORx | y = 14(0000 1110)
~bitwise NOT~x = -11(1111 0101)
^bitwise XORx ^ y = 14(0000 1110)
>>bitwise right shiftx >> 2 = 2(0000 0010)
<<bitwise left shiftx << 2 = 40(0010 10(00)

Assignment operator

In Python, the assignment operator is used to assign a value to a variable.

a = 5is a simple assignment operator, which assigns the value on the right5assigned to the left variablea.

There are many similar compound operators in Python, a += 5They are added to the variable and then assigned to them. Equivalent to a = a + 5.

OperatorExampleequivalent
=x = 5x = 5
+=x + = 5x = x + 5
-=x-= 5x = x-5
*=x * = 5x = x * 5
/=x / = 5x = x / 5
%=x%= 5x = x%5
//=x // = 5x = x // 5
**=x ** = 5x = x ** 5
&=x&= 5x = x&5
|=x |= 5x = x | 5
^=x ^= 5x = x ^ 5
>>=x >>= 5x = x >> 5
<<=x <<= 5x = x << 5

Special operators

Python provides some special types of operators, such as identity operators or membership operators. Below, they are described through examples.

Identity operator

is and is not are identity operators in Python. They are used to check whether two values (or variables) are located in the same part of memory. Two equal variables do not necessarily mean they are the same.

OperatorMeaningExample
isIf the operands are the same, then true (referring to the same object)x is true
is notIf the operands are not the same, then true (not referring to the same object)x is not true

Example4:Identity operator in Python

x1 = 5
y1 = 5
x2 = 'Hello'
y2 = 'Hello'
x3 = [1,2,3]
y3 = [1,2,3]
# Output: False
print(x1 is not y1)
# Output: True
print(x2 is y2)
# Output: False
print(x3 is y3)

Output result

False
True
False

Here, we seex1Andy1They are integers with the same value, so they are both equal and the same.x2Andy2(string)is the same.

Butx3Andy3They are equal but not the same. This is because although they are equal, the interpreter still locates them separately in memory.

Membership operator

in and not in are membership operators in Python. They are used to test whether a value (or variable) exists in a sequence (String,List,Tuple,SetAndDictionary)to find the value or variable.

In the dictionary, we can only test the existence of the key, not the value.

OperatorMeaningExample
inIf the value is found in the sequence/Variable, then true5 in x
not inIf the value is not found in the sequence/Variable, then true5 not in x

Example #5:Membership operator in Python

x = 'Hello world'
y = {1: 'a',2: 'b'}
# Output: True
print('H' in x)
# Output: True
print('hello' not in x)
# Output: True
print(1 in y)
# Output: False
print('a' in y)

Output result

True
True
True
False

Here, 'H' is in x, but 'hello' does not exist in x (remember that Python is case-sensitive). Similarly,1Is the key, and a is the value of the dictionary y, so y returns False for a.