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:

Operator Name Example
+ Addition x + y
Subtraction x – y
* Multiplication x * y
/ Division x / y
% Modulus x % y
** Exponentiation x ** y
// Floor division x // y

Comparison Operators

The following table lists the Python-supported comparison operators:

Operator Example Meaning Result
== a == b Equal to True if the value of a is equal to the value of b
False otherwise
!= a != b Not equal to True if a is not equal to b
False otherwise
< a < b Less than True if a is less than b
False otherwise
<= a <= b Less than or equal to True if a is less than or equal to b
False otherwise
> a > b Greater than True if a is greater than b
False otherwise
>= a >= b Greater than or equal to True 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:

Operator Example Meaning
not not x True if x is False
False if x is True
(Logically reverses the sense of x)
or x or y True if either x or y is True
False otherwise
and x and y True 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:

Operator Example Meaning Result
& a & b bitwise AND Each 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 | b bitwise OR Each 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.)
~ ~a bitwise negation Every 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 ^ b bitwise 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 >> n Shift right n places Every bit is moved to the right of n places.
<< a << n Shift left n places Every 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:

Operator Description Example
in Returns True if a sequence with the specified value is present in the object x in y
not in Returns 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:

Operator Description
lowest precedence or Boolean OR
and Boolean AND
not Boolean NOT
==, !=, <, <=, >, >=, is, is not comparisons, identity
| bitwise OR
^ bitwise XOR
& bitwise AND
<<, >> bit shifts
+, – addition, subtraction
*, /, //, % multiplication, division, floor division, modulo
+x, -x, ~x unary 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 Reply

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

Shopping cart