Python operators are one of the most used character or character set in Python Coding. These operators are used mainly in math operations in python. You can add two values, divide one to another or you can compare two variables with these python operators.
As an operator, you can sue one character or one more character. Each of these character sets has a different meaning as a python operator.
There are different python operators. We can divide them into seven categories like below:
Now, let’s learn what are these python operators with different examples for each of them.
You can also check Python If Else
Python Arithmetic Operators are used with math operations like addition, multiplication, subtraction, division etc. You can find the list of these operations that are done by Python Arithmetic Operators.
Now, let’s give some examples to these operators.
You can also Download Python Cheat Sheet!
For example, to add one value to another, you can use plus (+) operator like below:
The output will be:
We can also use divide operator of python for division like below:
The result will be:
In another example, we can calculate exponentiation. Let ‘s do it for 2 and 6.
The output will be the result of 2 x 2 x 2 x 2 x 2 x 2. And this is:
The are basic math operations that we can udo by using these basic python operators.
Python assignment operators are used to assign value to variables. The basic operator that I used for this purpose is equal (=) operator. Beside this operator, there are other assignment operators that has one more character. You can find all of these Python assignment operators below:
Now, let’s do some examples with these assignment operators of python programming.
Below, we will use *= operator to calculate
Or in another example, let’s use Bitwise AND. This Bitwise AND operator (&=) will compare the bits of each variable and will return 1 if each bits are 1.
7 -> 0000 0111 (binary)
3 -> 0000 0011 (binary)
So, the bitwise and will return with the value of the equal ones and this is:
Lastly, let’s do an example with Bitwise left shift. Here, we will sue Bitwise Left Shift (<<=) operator like below:
Here the binary value of 5 is
a = 5 = 0000 0101 (Binary)
So, when we use Bitwise Left Shift (<<=) operator, for the new value, the binary numbers will be shifted one left like below:
a << 1 = 0000 1010 = 10
And the output of this python code will be:
Another operator type in python programming is python comparison operators. With these operators, we can compare any variable with another variable. Below, you can find these python comparison operators.
These operators are used widely in python programming.
For example, we can use equal operator (==) to compare two variable and return a result according to it.
The output will be:
Now, let’s use also for loop with range to print a series of numbers. Here, we will control our variable with >= operator. If it is equal or greater than the mentioned value, it will break printing.
It will start to print from 0 to 5. But it will control the value of x every time if it is equal or greater than 4. So, in the output, there will be only the values lower than 4.
To combine conditional statements, we use python logical operators. There are three logical operators in python. These are:
and operator returns True, if both statements are true.
or operator returns True, if one of the statements is true.
not operator reverse the result, returns False if the result is true.
For example, below, we will use and operator of python and check the two statements. If two of them are true, then the program will return True. If one or two of them are false, it will return false.
The output of this python code will be:
Because, in the first line, both statements are True. In the second line one statement is True and the other is False. For True result, all the statements of and operator must be true.
Now, let’s do another example with or operator. Only one true statement is enough for or operator to return True.
The result of the first line will be True, because one statement is true about b.
The result of the second line will be False, because both of the statements are false about c.
Python Identity Operators are the operators that are used to compare the objects. This is not only a comparison about their value, but also their location in the memory. These operators check that if they are the same object or not. Below, you can find these Python Identity Operators.
is operator checks that if both objects are the same object. If yes, it returns True.
is not operator checks that if both objects are different objects. If yes, it returns True.
For example, in the below python code, we will compare two string list.
The result of first print will be False. Because, a is not same as b. But the result of the second print will be True. Because, c is the same object as a.
Python Membership Operators are used to check the members of a sequence. To do this, it uses two operators:
in operator returns True, if it finds the value as a member in the sequence.
not in operator return True, if it do not find the value as a members in the sequence.
In the below example, we will check “cc” string in the string list a.
The first result will be True, because “cc” is the member of a.
The second result will be False, because there is no “dd” in a.
The same example with not in operator is below. This time the results will be reverse.
Lastly, we will talk about Bitwise operators of python. These operators are used to compare binary numbers in python programming.
In this lesson, we have learned different python operators. You will use these important python operators too much in your codes. So, if you practice more with different examples, you can improve your knowledge about this important lesson.
Leave a Reply