How to use Python Operators – with example

  • Reading time:18 mins read

What are the Python operators?

A Python operator is used to perform a specific operation on a variable or value. The assignment operator is the most common type of operator, however, we have other types of operators such as:

  1. Arithmetic operators
  2. Comparison operators
  3. Logical operators
  4. Identity operators
  5. Membership operators
  6. Identity operators
  7. Bitwise operators

Python operators come in a few different guises. The main ones that you’ll be looking at are the mathematical ones and probably the comparison operators. But there are a few others that are quite useful and in this article, we will run through some of the more common ones and the basic ones.



Arithmetic Python operators

Arithmetic operators include operations like simple additions, subtractions, multiplication, exponential and division. If we initialize x to be equal to five and y to be equal to 10 we can illustrate the arithmetic operators, for instance, x plus y is a simple addition while a dash is used to denote subtraction. Multiplication is denoted with a star sign and double star signs represent exponentiation and finally, a forward slash represents divisions as shown in the code below.



Comparison operators

Comparison operators are used to comparing two values.


Python operators


Double Equal to

When comparing two values we use double equal signs since a single equal sign is an assignment operator. The equal to comparison operator is used to test if the value on the left-hand side is indeed equal to that on the right side, if they are equal then it returns the boolean True and False if otherwise.



In this case, False is returned since x is not equal to y. However, if we change the value of y to be equal to 5 then True will be returned since in that case, y will be equal to x.



This is particularly useful when working with code and you need to see if something is equal to something else.


Python operators are useful tools while coding.


Comparison operators can be used together with strings as well. If we equate x to be equal to the string ‘lion’ in this case if we compare it with the variable y then we will get the boolean value False.



In theory, we could have the same strings and in that case, it is going to return the value True instead as shown below.



Not Equal to

This is another useful comparison operator known as the not equal. This comparison operator is denoted by an exclamation mark that is then followed by an equal sign. To demonstrate this python operator we are going to turn back to the integer values that we had used initially. 



This returns true because x does not equal y. If we change these values again such that they are equal, this returns false because the statement that x is not equal to y Is false. Because the two values are the same.


Logical Python Operators

We can also use multiple statements to compare more than two values. To demonstrate this let’s add a couple of numbers. Let’s do ‘c’ is equal to eight and i is equal to 15 and will change y to 10 as well. The keyword ‘and’ and ‘or’ can be used to compare multiple values and return boolean statements accordingly. So we could say if x does not equal to y and c is equal to I.



This statement is returning false even though x is not the same as y, c is not the same as i, so the whole statement is false.


Learn how to use Python operators online!


We can also use or so this will be this one or this one, so this will return true because x is not equal to y.


Membership Python Operators

Membership operators are used to testing if a sequence is present in a particular object.

There are two types of membership operators: the ‘in’ operator returns True if a sequence with a specified value is present in an object while the ‘not in’ returns ‘True’ if the sequence with a specified value is not present in an object.

We can change the value of x to be equal to ‘i’ and y to be equal to ‘tiger’

So what we could do is we could say is x in y.



This is equal to true because  it has found the value of x within the y statement

Conversely, you can also have not in as shown below.



And we can see that that’s returning false there. So if we change the letter ‘i’ to ‘o’, we can see that it returns True.



If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!





Not sure if programming is for you? With CodeBerry you’ll like it.