Basic Operators in Python

In this section of the Python tutorial, we will have a look at operators & expression in Python. Operators are special symbols suggesting that some kind of computation should be performed. The values on which an operator operates are called operands.

Types of Operators in Python

Arithmetic Operators

The following table lists the Python-supported arithmetic operators:

OperatorNameExample
+Additionx + y
Subtractionx – y
*Multiplicationx * y
/Divisionx / y
%Modulusx % y
**Exponentiationx ** y
//Floor divisionx // y

Comparison Operators

The following table lists the Python-supported comparison operators:

OperatorExampleMeaningResult
==a == bEqual toTrue if the value of a is equal to the value of b
False otherwise
!=a != bNot equal toTrue if a is not equal to b
False otherwise
<a < bLess thanTrue if a is less than b
False otherwise
<=a <= bLess than or equal toTrue if a is less than or equal to b
False otherwise
>a > bGreater thanTrue if a is greater than b
False otherwise
>=a >= bGreater than or equal toTrue if a is greater than or equal to b
False otherwise

Logical Operators

As you’ve seen, some of Python’s objects and expressions are of Boolean form. That is, they are equivalent to either True or False, one of the Python variables.

The following table lists the Python-supported logical operators:

OperatorExampleMeaning
notnot xTrue if x is False
False if x is True
(Logically reverses the sense of x)
orx or yTrue if either x or y is True
False otherwise
andx and yTrue if both x and y are True
False otherwise

Bitwise Operators

Bitwise operators treat operands as binary digit sequences and operate bit by bit on them. The following table lists the Python-supported bitwise operators:

OperatorExampleMeaningResult
&a & bbitwise ANDEach bit location in the result is the logical AND of the bits in the operand’s corresponding position. (1 If both are 1, if not 0.)
|a | bbitwise OREach bit position in the result is the logical OR of the bits in the operands ‘ corresponding position. (1 If either one is 0, or 0.)
~~abitwise negationEvery bit position in the result is the logical rejection of the bit in the operand’s corresponding position. (1 Where 0, 0 If 1.)
^a ^ bbitwise XOR (exclusive OR)Each bit position in the result is the bits ‘ logical XOR in the operands ‘ corresponding position. (1 If the operand bits are unique, 0 if the operands are identical.)
>>a >> nShift right n placesEvery bit is moved to the right of n places.
<<a << nShift left n placesEvery bit is moved to the left of n places.

Identity Operators

Python provides two operators that decide whether the operands given have the same identity, i.e. refer to the same element, is and is not. This is not the same thing as equality, meaning that the two operands are objects that contain the same information but are not exactly the same entity.

Membership Operators

Membership operators are used to check whether an object displays a sequence:

OperatorDescriptionExample
inReturns True if a sequence with the specified value is present in the objectx in y
not inReturns True if a sequence with the specified value is not present in the object

Operator Precedence

A standard is provided to all operators that support language. Both operators of the highest priorities are performed first in an expression. Once the results are collected, the next highest precedence operators will be performed. So it goes on, until full evaluation of the expression. In left-to-right order, all operators with equal precedence are performed.

Here’s the order of the Python operators you’ve seen so far, from the lowest to the highest:

OperatorDescription
lowest precedenceorBoolean OR
andBoolean AND
notBoolean NOT
==, !=, <, <=, >, >=, is, is notcomparisons, identity
|bitwise OR
^bitwise XOR
&bitwise AND
<<, >>bit shifts
+, –addition, subtraction
*, /, //, %multiplication, division, floor division, modulo
+x, -x, ~xunary positive, unary negation, bitwise negation
highest precedence**exponentiation

Augmented Assignment Operators

You have seen that to assign a value to a variable, a single equal sign (=) is used. Of course, being an expression containing other variables is perfectly feasible for the value to the right of the assignment:

>>> alpha = 10
>>> beta = 20
>>> gama = alpha * 5 + beta
>>> gama
70

Leave a Comment

Your email address will not be published. Required fields are marked *