An expression is defined as a combination of constants, variables, and operators . An expression always evaluates to a value . A value or a standalone variable is also considered as an expression but a standalone operator is not an expression.

Examples of expressions in python:

  • 125
  • n
  • n - 25
  • 25 + 67
  • “abc” + “def”

 

Precedence of Operators

  • Evaluation of the expression is based on precedence of operators. 
  • When an expression contains different kinds of operators, precedence determines which operator should be applied first. 
  • Higher precedence operator is evaluated before the lower precedence operator.
  • Unary operators need only one operand, and they have a higher precedence than the binary operators.
  • Parenthesis can be used to override the precedence of operators. The expression within () is evaluated first. 
  • For operators with equal precedence, the expression is evaluated from left to right.

Expressions: Combining Operators and Operands in Python - Teachoo - Concepts

 

Examples:

1. 30 + 40 * 2

Step 1: 30 + (40*2)

Step 2: 30 + 80

Answer: 110

 

2. 20 - 30  + 50

Step 1: (20-30) + 50

Step 2: -10 + 50

Answer: 40

Remove Ads
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.