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:
- Arithmetic operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Identity operators
- 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 are used to comparing two values.
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.
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.
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 Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
- How to create perfect HTML tables?
- HTML color codes
- CSS background images
- Best Programming Books You Must Read in 2021
- Python for loop
- Creating a table using HTML and CSS
- Best way of using Java Arrays and ArrayLists
- Best way of using Python Sets
- Best ways of using a Python Dictionary
- Best way of using Python Classes
- Best way of using Python Range
- Best way of using Python if-else
- Best way of using Python RegEx
- Best way of using Python Lists
- Best way of using Python Enumerate
- Best way of using Python Functions
- Best way of using Python Split
- Best way of using Python Try-Except
- Best way of using Python Tuple
- Best way of using Python Arrays
- Best way of using Python Sort
- Best way of using Python DateTime
- How to download Python?
- Best way of using Python FileWrite
- Best way of using Python Lambda
- Best way of using Python ListAppend
- Best way of using Python ListComprehension
- Best way of using Python Map
- Best way of using Python Operators
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.