Expressions: It is any valid combination of operators and operands (may be literals / variables).
It is composed of one or more operations. Example
1) 5 + 4 is valid
2) 5 + -4 is valid
3) 5 + * 6 is invalid because in this case, compiler will be confused which operation whether + or * is to be performed on 5 and 6.
4) a * b is valid
5) a * 5 is valid
6) a + b * 2 – 5 is valid
>>> 5 + 4 9 >>> 5 + -4 1 >>> 5 + * 6 SyntaxError: invalid syntax >>> a=5 >>> b=4 >>> a * b 20 >>> a * 5 25 >>> a + b * 2 - 5 8
Types of Expressions: Python supports following types of expressions:
1) Arithmetic Expressions
2) String Expressions
3) Relational Expressions
4) Logical Expressions
5) Compound Expressions
Arithmetic Expressions: It consists of numeric types (integer, floats, complex) and one or more arithmetic operators ( + ,- ,* , / , // , % , **). Example: 2 + 5**3
>>> 2 + 5**3 127
String Expressions: It consists of strings and/or integers along with string operators ( + , * ). Examples:
1) “Gargs” + “Academy”
2) “gargs” * 2
3) “gargs” + 2 is invalid because we cant add a string with a numeric type
4) “gargs” * “academy” is invalid because we cant multiply two strings
>>> "Gargs" + "Academy" 'GargsAcademy' >>> "gargs" * 2 'gargsgargs' >>> "gargs" + 2 Traceback (most recent call last): File "<pyshell#17>", line 1, in <module> "gargs" + 2 TypeError: can only concatenate str (not "int") to str >>> "gargs" * "academy" Traceback (most recent call last): File "<pyshell#18>", line 1, in <module> "gargs" * "academy" TypeError: can't multiply sequence by non-int of type 'str'
Relational Expressions: It consists of literals and/or variables of any valid types (Numeric / String / Sequences / Mapping) and one or more relational operators (< , <= , > , >= , == , !=). Example:
listname[index] >= value
>>> l=[1,2,3] >>> l[2] >= 1 True
Logical Expressions: It consists of literals and/or variables of any valid types (Numeric / String / Sequences / Mapping) and one or more logical operators (and / or / not ). Example:
1) True and True
2) 1 or 0
3) 1 and 0
4) not 5
5) not 0
6) not True
7) not False
8) not “TRUE”
9) not “False”
>>> True and True True >>> 1 or 0 1 >>> 1 and 0 0 >>> not 5 False >>> not 0 True >>> not True False >>> not False True >>> not "TRUE" False >>> not "False" False
Compound Expressions: It consists of literals and/or variables of any valid types (Numeric / String / Sequences / Mapping) and one or more operators . Example:
1) 5 > 4 and not 3 < 5
2) not 8 > 4 + 2
3) not 8 > 4 + 8
>>> 5>4 and not 3<5 False >>> not 8 > 4 + 2 False >>> not 8 > 4 + 8 True
Evaluation of Expression: Python evaluates the expressions according to the following rules:
1) Expressions are evaluated in the order of precedence of operators. If two or more operators of same precedence are encountered in an expression, then the operators are evaluated according to their associativity i.e from right to Left or Left to Right.
2) Performs any implicit conversions i.e. converts all operands upto the type of highest operand. Examples
a) If int and float/complex are to be evaluated, then int will be promoted to float/complex
b) If float and complex are to be evaluated, then float will be promoted to complex
3) Compute the result
4) Replace the sub expression with computed result and continue evaluating the expression.
5) Repeat steps 1 to 4 till the final result is obtained.
Examples:
1) As ** has more precedence than +, So 5**3 = 125 is evaluated first and then result is 2 + 125 = 127
2 + 5 ** 3
= 2 + 125
= 127
2) Since both / and * are of same precedence
hence they are evaluated according to associativity i.e. left to right
12 / 4 * 3
= 3 * 3
= 9
3) Since + has highest precedence, so 4 + 8 = 12 is calculated first
then > has more precedence than not, So 8 > 12 is evaluated as False, and then not False = True
not 8 > 4 + 8
not 8 > 12
not False
True
4) int and complex are to be added, so int will be promoted to complex before evaluating.
5 + (7+7j)
( 5 + 0j) + (7 + 7j)
(12+7j)
5) int and float are to be added, so int will be promoted to float before evaluating.
5 + 7.5
= 5.0 + 7.5
= 12.5
6) float and complex are to be added, so float will be promoted to complex before evaluating.
7.6 + (2+5j)
= (7.6 + 0j) + (2 + 5j)
= (9.6+5j)
>>> 2 + 5**3 127 >>> 12/4*3 9.0 >>> not 8 > 4 + 8 True >>> 5 + (7+7j) (12+7j) >>> 5 + 7.5 12.5 >>> 7.6 + (2+5j) (9.6+5j)
Evaluation of Relational Expressions: All relational or comparison expressions gives Boolean values only i.e. True / False
1) a<b<c is evaluated as a<b and b<c
2) a<b>c is evaluated as a<b and b>c
3) a>b>c is evaluated as a>b and b>c
4) a>b<c is evaluated as a>b and b<c
>>> 5<7<9 True >>> 5<7>9 False >>> 5>7>9 False >>> 5>7<9 False