fbpx

Python: Operators and Expressions

In the previous blog, we have learned about python variable now we will learn the use of the operator on those variables.
Whenever we write a logical statement we need operator(s) and Operands. This logical statement called expression.

For Example 1 + 3, Operators are functionality that does something and can be represented by symbols such as + or by special keywords. Operators require some data to operate on and such data are called operands. In this case, 1 and 3 are the operands.

>>> 2 * 3
15
>>>

Arithmetics Operators:

 Arithmetic Operators and expression
Arithmetics Operators and expression
>>> 2 + 3
5
>>> 4 - 5
-1
>>> 5 - 4
1
>>> 4 * 5
20
>>> 5/4
1.25
>>> 5//4
1
>>> -5/4
-1.25
>>> -5//4
-2
>>> 4%3
1
>>> -4%3
2
>>> 4**2
16
>>>

How % (modulo) operator working on negative numbers:

The % operator in Python yields integers with the same sign as the divisor.

>>> 7 % -3
-2
>>> 7 % 3
1
>>> -7 % 3
2
>>>

To understand how % work with negative numbers, understand the below diagram. for more detail check this blog

modulo on negative number

Bitwise Operators:

Bit-wise Operators and expression
Bitwise Operators and expression
>>> 6 & 3
2
>>> 6 | 3
7
>>> 6 << 1
12
>>> 6 >> 1
3
>>> ~2
-3
>>> 6 ^ 3
5
>>>

Comparison Operators:

Comparison Operators and expression
Comparison Operators and expression

Equality Operator:

>>> "a" == "a"
True
>>> 1 != 1
False
>>> 1 <= 2
True
>>> 2 >= 1
True
>>> 1 < 3
True
>>> 3 > 4
False
>>> not 1
>>> False

A zero value is false. A non-zero value is true.

“or”, and “and” operator

When we apply “or” and “and” operator on non-boolean operand it will not return True or False

non-Boolean operand

x = False; y = True;
x and y returns False since x is False. In this case, Python will not evaluate y since it knows that the value of the expression will has to be false (since x is False). This is called short-circuit valuation.
x or y returns True. Short-circuit evaluation applies here as well.

short-circuit evaluation

If x and y are non-boolean operands:

if x isx or y x and y
truth valuexy
false valueyx
>>> x = 1
>>> y = 2
>>> x or y
1
>>> x = 0
>>> y = 1
>>> x or y
1
>>>

An empty string is false. A non-empty string is true.

>>> x = 1
>>> y = 2
>>> x and y
2
>>> x = 0
>>> y = 1
>>> x and y
0
>>>

Operator Precedence:

The following table gives the operator precedence table for Python, from the lowest precedence (least binding) to the highest precedence (most binding). This means that in a given expression, Python will first evaluate the operator’s higher precedence in the table before the operators lower precedence.

Precedence Operator Description
lowest precedence lambda Lambda Expression
orBoolean OR
andBoolean AND
not xBoolean NOT
in, not inMembership operator
<, <=, >, >=, !=, ==Comparisons
|Bitwise OR
^Bitwise XOR
&Bitwise AND
<<, >>Shift
+, –Addition, Substation
*, /,%Multiplication, Division, and Remainder
+x, -xPositive and Negative
~xBitwise NOT
**Exponentiation
x.attribute Attribute reference
x[index] Subscription
x[index:index] Slicing
f(arguments …) Function call
(expressions, …) Binding or tuple display
[expressions, …] List-display
{key: datum, …} Dictionary display
highest precedence `expressions, …` String conversion

By default, the operator precedence table decides which operators are evaluated before others. However, if you want to change the order in which they are evaluated, you can use parentheses.
For example, if you want the addition to being evaluated before multiplication in an expression, then you can write something like (2 + 3) * 4.

>>> 2 + 3 * 4
14
>>> (2 + 3) * 4
20
>>>

Operators are usually associated from left to right i.e. operators with same precedence are evaluated in a left to right manner. Some operators like assignment operators have right to left associativity

Associativity

For example, 2 + 3 + 4 is evaluated as (2 + 3) + 4. Some operators like assignment operators have right to left associativity i.e. a = b = c is treated as a = (b = c).

>>> 2 + 3 - 2
3
>>> (2 + 3) -2
3
>>>

Python supports a shorthand augmented assignment notation for these arithmetic and bitwise operators. i.e. x += y is equivalent to x = x + y.

 >>> x = 3
>>> y = 7
>>> x += y
>>> x
10
>>> x = 3
>>> y = 7
>>> x = x + y
>>> x
10
>>>

Summary:

We have learned how to use operators, operands and expressions – these are the basic building blocks of any program.