DEV Community

Cover image for All Python Operators
Vadim Kolobanov
Vadim Kolobanov

Posted on • Updated on • Originally published at dev.to

All Python Operators

Photo by Pakata Goh on Unsplash

In this guide, we will talk about Python programming language operators. You will learn about arithmetic, logical and bitwise operators, as well as comparison, assignment, membership, identity operators, and their syntax. All this will be illustrated with examples.

An operator in Python is a symbol that performs an operation on one or more operands.
The operand is the variable or value on which the operation is performed.

Introduction to Python Operators

Python operators come in 7 types:

  • Arithmetic operators
  • Comparison operators
  • Assignment operators
  • Logical operators
  • Accessory Operators
  • Identity operators
  • Bitwise operators

Python arithmetic operators

(This type includes operators for performing basic arithmetic operations.)

Addition (+)

Adds the value on both sides of the operator.
Example:

3+4
7
Enter fullscreen mode Exit fullscreen mode

Subtraction (-)

Subtracts the value of the right side from the value in the left.
Example:

3-4
-1  
Enter fullscreen mode Exit fullscreen mode

Multiplication (*)

Multiplies the values on both sides of the operator.
Example:

3*4
12  
Enter fullscreen mode Exit fullscreen mode

Division (/)

Divides the value of the left side of the assignment of the right. The data type of the division result is a floating point number.
Example:

3/4
0.75 
Enter fullscreen mode Exit fullscreen mode

Exponentiation (**)

Raises the first number to the power of the second.
Example:

3**4
81
Enter fullscreen mode Exit fullscreen mode

Division without remainder (//)

Performs division and returns the integer value of the quotient, removing the digits after the decimal point.
Example:

4//3
1
Enter fullscreen mode Exit fullscreen mode
10//3
3
Enter fullscreen mode Exit fullscreen mode

Division modulo (remainder of division) (%)

Performs division and returns the value of the remainder.
Example:

3%4
3
Enter fullscreen mode Exit fullscreen mode
4%3
1
Enter fullscreen mode Exit fullscreen mode
10%3
1
Enter fullscreen mode Exit fullscreen mode
10.5%3
1.5
Enter fullscreen mode Exit fullscreen mode

Comparison operators

Comparison operators in Python compare operands. They report whether one of them is greater than the other, less than, equal to, or both.

Less (<)

This operator checks whether the value on the left is less than the value on the right.
Example:

4<3
False
Enter fullscreen mode Exit fullscreen mode

More (>)

Checks whether the value on the left is greater than the one on the right.
Example:

4>3
True
Enter fullscreen mode Exit fullscreen mode

Less than or equal to (<=)

Checks whether the left part is less than or equal to the right.
Example:

7<=7
True
Enter fullscreen mode Exit fullscreen mode

Greater than or equal to (>=)

Checks whether the left part is greater than or equal to the right.
Example:

0>=0
True
Enter fullscreen mode Exit fullscreen mode

Equal to (==)

This operator checks whether the value from the left is equal to the right. 1 is equal to Boolean True, and 2 (two) is not. 0 is False.
Example:

3==3.0
True

1==True
True

7==True
False

0==False
True

0.5==True
False
Enter fullscreen mode Exit fullscreen mode

Not equal to (!=)

Checks whether the value on the left is equal to the value on the right. The <> operator performs the same task, but it was removed in Python 3.

When the condition is met, True is returned. Otherwise, False. This return value can be used in subsequent statements and expressions.
Example:

1!=1.0
False
Enter fullscreen mode Exit fullscreen mode
1==True  # This raise Syntax Error
Enter fullscreen mode Exit fullscreen mode

Assignment operators

The assignment operator assigns a value to a variable. It can manipulate the value before assignment. There are 8 assignment operators: 1 simple and 7 using arithmetic operators.

Assignment (=)

Assigns a value from the right to the left part. It is worth noting that == is used for comparison, and = for assignment.
Example:

a = 8
print(a)
8
Enter fullscreen mode Exit fullscreen mode

Addition and assignment (+=)

Sums up the value of both sides and assigns it to the expression on the left. a += 10 is the same as a = a + 10.

The same applies to all other assignment operators.
Example:

a=8
a += 2
print(a)
10
Enter fullscreen mode Exit fullscreen mode

Subtraction and assignment (-=)

Subtracts the value on the right from the left and assigns it to the expression on the left.
Example:

a=10
a -= 2
print(a)
8
Enter fullscreen mode Exit fullscreen mode

Division and assignment (/=)

Divides the value from the left by the right. Then assigns it to the expression on the left.
Example:

a /= 7
print(a)
1.0
Enter fullscreen mode Exit fullscreen mode

Multiplication and assignment (*=)

Multiplies the values of both sides. Then assigns the right to the left.
Example:

a *= 8
print(a)
8.0
Enter fullscreen mode Exit fullscreen mode

Modulo division and assignment (%=)

Performs modulo division for both parts. The result assigns to the left part.
Example:

a=7
a %= 3
print(a)
2.0
Enter fullscreen mode Exit fullscreen mode

Exponentiation and assignment (**=)

Performs exponentiation of the left part of the value of the right part. Then assigns a value to the left part.
Example:

a **= 5
print(a)
32.0
Enter fullscreen mode Exit fullscreen mode

Division with remainder and assignment (//=)

Performs division with the remainder and assigns the result to the left part.
Example:

a //= 3
print(a)
10.0
Enter fullscreen mode Exit fullscreen mode

This is one of the important Python operators

Python Logical operators

These are unions that allow you to combine several conditions. There are only three operators in Python: and (and), or (or) and not (not).

And

If the conditions on both sides of the and operator are true, then the entire expression is considered true.
Example:

a = 7 > 7 and 2 > -1
print(a)
False
Enter fullscreen mode Exit fullscreen mode

Or

The expression is false if both operands on both sides are false. If at least one of them is true, then the whole expression is true.
Example:

a = 7 > 7 or 2 > -1
print(a)
True
Enter fullscreen mode Exit fullscreen mode

Not

This operator inverts the Boolean values of the expression. True turns into False and vice versa. In the example below, the Boolean value 0 is False. Therefore, it turns into True.
Example:

a = not(0)
print(a)
True
Enter fullscreen mode Exit fullscreen mode

Accessory Operators

These operators check whether the value is part of a sequence. The sequence can be a list, a string, or a tuple. There are only two such operators: in and not in.

In

Checks whether the value is a member of the sequence. In this example, you can see that the fox line is not in the list of pets. But cat is, so it returns True. Also, the string me is a substring of disappointment. Therefore, it will return True.
Example:

pets=['dog','cat', 'ferret']
'fox' in pets
False

'cat' in pets
True
'me' in 'disappointment'
True
Enter fullscreen mode Exit fullscreen mode

Not in

This operator checks whether the value is not a member of the sequence.
Example:

'pot' not in 'disappointment'
True
Enter fullscreen mode Exit fullscreen mode

Identity operators

These operators check whether the operands are the same (whether they occupy the same position in memory).

Is

If the operands are identical, then True will be returned. Otherwise, False. Here 2 is not 20, so False will be returned. But '2' is the same as "2". Different quotes do not change the objects themselves, so True will be returned.
Example:

2 is 20
False
'2' is "2"
True
Enter fullscreen mode Exit fullscreen mode

Is not

2 is a number, and '2' is a string. Therefore, True will be returned.
Example:

2 is not '2'
True
Enter fullscreen mode Exit fullscreen mode

Python Bitwise operators**

These operators work on the operands bit by bit.

Binary And (&)

Performs a bitwise and operation on two values. Here binary 2 is 10 and 3 is 11. The result of bitwise and is 10 - binary 2. Bitwise and over 011(3) and 100(4) yields the result 000(0).
Example:

2&3
2
3&4
0
Enter fullscreen mode Exit fullscreen mode

Binary OR (|)

Performs a bitwise or operation on two values. Here or for 10(2) and 11(3) returns 11(3).
Example:

2|3
3
Enter fullscreen mode Exit fullscreen mode

Binary OR NOT (^)

Performs a bitwise xor (exclusive or) operation on two values. Here the result OR NOT for 10(2) and 11(3) will be 01(1).
Example:

2^3
1
Enter fullscreen mode Exit fullscreen mode

Inverting operator (~)

It returns inverted binary numbers. In other words, flips the bits. Bit 2 is 00000010. Its inverted version is 11111101. This is a binary -3. Therefore, the result is -3. Similarly, ~1 equals -2.
Example:

~-3
2
Enter fullscreen mode Exit fullscreen mode

Once again, the inverted -3 is 2.

Binary left shift (<<)

It shifts the value of the left operand to the position indicated on the right. So, binary 2 is 10. 2 << 2 will shift the value two positions to the left and 1000 will come out β€” this is binary 8.
Example:

2<<2
8
Enter fullscreen mode Exit fullscreen mode

Binary shift to the right (>>)

Shifts the value of the left operator to the position indicated on the right. So, binary 3 is 11. 3 >> 2 will shift the value two positions to the right. The result is 00, that is, 0. 3 >> 1 will shift one position to the right, and the result will be 01 - binary 1.
Example:

3>>2
3>>1
1
Enter fullscreen mode Exit fullscreen mode

Conclusions

In this article, all 7 types of Python operators were considered. An example was offered for each in the IDE. To understand the specifics of the operators, you need to continue working with them, use them in conditional constructions and combine them.

FAQ

I am a beginner, how should I learn Python?

Look into the following series:

Learning Python
Step by Step to Junior
Ideas

Would you mentor me?

Of course I am ready to participate in the training. The big difficulty is that English is not my native language and it will be possible to communicate through a translator

Would you like to collaborate on our organization?

If you have interesting ideas, we will be happy to invite your writing from our organization. Write in private messages or in social networks below

Connect to me on

Write me on Face....oh...Meta

My Twitter

Top comments (0)