Page 280 - Ai_C10_Flipbook
P. 280
Symbol Purpose Example Output
/= Divides first and then assigns a Value=25
new value. Value/=5
print(Value) 5.0
//= Floor division first and then Value1=37
assigns a new value. print(Value1//5) 7
%= Remainder of a number first and N1=39
then assigns a new value. N2=5
N1%=N2
print(N1) 4
**= Exponential of a number first and N1=2
then assigns a new value. N2=4
N1**=N2
print(N1) 16
Operator Precedence
An expression is made up of values, variables,
Order of Precedence Operators
operators and functions. For example, 22/7*5.5
is an expression. 1 ()
2 **
To evaluate an expression with multiple operators
we follow an order of precedence in Python. This 3 *, /, %, //
order can be altered by writing an expression 4 +, -
within parenthesis.
5 <=, <, >, >=, ==, !=
This order of precedence in the descending order 6 =, %=, /=, //=, -=, +=, *=, **=
is listed below:
7 not
8 and
In the above table, as you have seen that there 9 or
are few operators with the same precedence
then order of evaluation is from left to right except for exponential (**) which is always from right to left.
For example:
• 2 + 5 * (15 - 10) // 2 will be evaluated as follows:
2+5*(5)//2 since * and // have same precedence, it will be evaluated from left to right.
2+12
14
• 25%3%2
To evaluate the above expression it works like (25%3)%2.
1%2= 1
• 2**2**3
This expression is evaluated from right to left. Here it will be equivalent to 2**(2**3). The result is 256.
278 Artificial Intelligence Play (Ver 1.0)-X

