Product SiteDocumentation Site

4.6. Expressions

Generally while writing expressions we put spaces before and after every operator so that the code becomes clearer to read, like
 
a = 234 * (45 - 56.0 / 34)

One example code used to show expressions
 
#!/usr/bin/env python
a = 9
b = 12
c = 3
x = a -b / 3 + c * 2 -1
y = a -b / (3 + c) * (2 -1)
z = a - (b / (3 +c) * 2) -1
print "X = ", x
print "Y = ", y
print "Z = ", z

The output
 
$ ./evaluationexp.py
X =  10
Y =  7
Z =  4

At first x is being calculated. The steps are like this
 
9 - 12 / 3 + 3 * 2 -1
9 - 4 + 3 * 2 - 1
9 - 4 + 6 - 1
5 + 6 -1
11 - 1
10

Now for y and z we have parentheses, so the expressions evaluated in different way. Do the calculation yourself to check them.