Expressions
Expressions
>>> 2 + 2
>>> 4
0.6 0.6
x = 3.9 * x * ( 1 - x )
0.4
x = 3.9 * x * ( 1 - x )
x = 1 + 2 * 3 - 4 / 5 ** 6
• Operators with the same precedence are evaluated from left to right.
1+2*5
Parenthesis
Power 1 + 10
Multiplication
Addition 11
Left to Right
Datec Learning Centers
Operator Precedence Example
1 + 2 ** 3 / 4 * 5
>>> x = 1 + 2 ** 3 / 4 * 5
>>> print x
11 1+8/4 *5
Note:
8/4 goes before 4*5 because
of the left-right rule.
1+2*5
Parenthesis
Power
Multiplication
1 + 10
Addition
Left to Right
Datec Learning Centers 11
EXPRESSION Example
>>> x=(a+b)
>>> a=5
>>> print(x)
>>> b=10
15
>>> c=3
>>> d=2
>>> x=(a+b)*(c*d)/e-f
>>> e=1
>>> print(x)
>>> f=9
81.0
Datec Learning Centers
Operator Precedence
❖ Remember the rules top to bottom.
❖ You can also use int() and float() to convert between strings and
integers.
❖ You will get an error if the string does not contain numeric
characters.
Enter a number: 3
>>> number
An integer 3
(no single quotes)!
>>>
>>> number
Float
(no single quotes)! 3.7
>>>