In python, an expression is any legal combination of symbols (like a variable, constants, and operators) representing a value. An expression must have at least one operand (variable or constant) and have one or more operators. After evaluating an expression, we get a value.
For example, x+y*z-8 is an example of an expression, where +, * are operators; x, y, and z are variables, and 8 is a constant.
Actually, the operand is the value on which the operator is applied, and operators use constants and variables to form an expression.
Where an expression has more than one operator, then the expression is evaluated using the operator precedence rule.
Python Expression Example: 1
a=10 b=20 c=30 d=a+b*c print(d)
Output:
610
In this example, b*c is evaluated first, and then returning value+a is evaluated, and we get the result.
When a program is compiled, Python also checks the validity of all expressions. If an illegal expression (like x+_, -y or, <z++) is encountered, an error message is displayed.
Python Expression Example: 2
a=9 if(a++<11): print("Number is less than 11") else: print("Number is not less than 11")
Output:
Error
Leave a comment