Open In App

Expressions in Python

Last Updated : 03 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

An expression is a combination of operators and operands that is interpreted to produce some other value. In any programming language, an expression is evaluated as per the precedence of its operators. So that if there is more than one operator in an expression, their precedence decides which operation will be performed first. We have many different types of expressions in Python. Let’s discuss all types along with some exemplar codes :

1. Constant Expressions: These are the expressions that have constant values only.

Example:

Python3




# Constant Expressions
x = 15 + 1.3
  
print(x)


Output

16.3

2. Arithmetic Expressions: An arithmetic expression is a combination of numeric values, operators, and sometimes parenthesis. The result of this type of expression is also a numeric value. The operators used in these expressions are arithmetic operators like addition, subtraction, etc. Here are some arithmetic operators in Python:

Operators Syntax Functioning
+ x + y Addition
x – y Subtraction
* x * y Multiplication
/ x / y Division
// x // y Quotient
% x % y Remainder
** x ** y Exponentiation

Example:

Let’s see an exemplar code of arithmetic expressions in Python :

Python3




# Arithmetic Expressions
x = 40
y = 12
  
add = x + y
sub = x - y
pro = x * y
div = x / y
  
print(add)
print(sub)
print(pro)
print(div)


Output

52
28
480
3.3333333333333335

3. Integral Expressions: These are the kind of expressions that produce only integer results after all computations and type conversions.

Example:

Python3




# Integral Expressions
a = 13
b = 12.0
  
c = a + int(b)
print(c)


Output

25

4. Floating Expressions: These are the kind of expressions which produce floating point numbers as result after all computations and type conversions.

Example:

Python3




# Floating Expressions
a = 13
b = 5
  
c = a / b
print(c)


Output

2.6

5. Relational Expressions: In these types of expressions, arithmetic expressions are written on both sides of relational operator (> , < , >= , <=). Those arithmetic expressions are evaluated first, and then compared as per relational operator and produce a boolean output in the end. These expressions are also called Boolean expressions.

Example:

Python3




# Relational Expressions
a = 21
b = 13
c = 40
d = 37
  
p = (a + b) >= (c - d)
print(p)


Output

True

6. Logical Expressions: These are kinds of expressions that result in either True or False. It basically specifies one or more conditions. For example, (10 == 9) is a condition if 10 is equal to 9. As we know it is not correct, so it will return False. Studying logical expressions, we also come across some logical operators which can be seen in logical expressions most often. Here are some logical operators in Python:

Operator Syntax Functioning
and P and Q It returns true if both P and Q are true otherwise returns false
or P or Q It returns true if at least one of P and Q is true
not not P It returns true if condition P is false

Example:

Let’s have a look at an exemplar code :

Python3




P = (10 == 9)
Q = (7 > 5)
  
# Logical Expressions
R = P and Q
S = P or Q
T = not P
  
print(R)
print(S)
print(T)


Output

False
True
True

7. Bitwise Expressions: These are the kind of expressions in which computations are performed at bit level.

Example:

Python3




# Bitwise Expressions
a = 12
  
x = a >> 2
y = a << 1
  
print(x, y)


Output

3 24

8. Combinational Expressions: We can also use different types of expressions in a single expression, and that will be termed as combinational expressions.

Example:

Python3




# Combinational Expressions
a = 16
b = 12
  
c = a + (b >> 1)
print(c)


Output

22

But when we combine different types of expressions or use multiple operators in a single expression, operator precedence comes into play.

Multiple operators in expression (Operator Precedence)

It’s a quite simple process to get the result of an expression if there is only one operator in an expression. But if there is more than one operator in an expression, it may give different results on basis of the order of operators executed. To sort out these confusions, the operator precedence is defined. Operator Precedence simply defines the priority of operators that which operator is to be executed first. Here we see the operator precedence in Python, where the operator higher in the list has more precedence or priority:

Precedence Name Operator
1 Parenthesis    ( ) [ ] { }
2 Exponentiation  **
3 Unary plus or minus, complement  -a , +a , ~a
4 Multiply, Divide, Modulo   /  *  //  %
5 Addition & Subtraction  +  –
6 Shift Operators >>  <<
7 Bitwise AND &
8 Bitwise XOR ^
9 Bitwise OR |
10 Comparison Operators >=  <=  >  <
11 Equality Operators ==  !=
12 Assignment Operators  =  +=  -=  /=  *=
13 Identity and membership operators is, is not, in, not in
14 Logical Operators    and, or, not

So, if we have more than one operator in an expression, it is evaluated as per operator precedence. For example, if we have the expression “10 + 3 * 4”. Going without precedence it could have given two different outputs 22 or 52. But now looking at operator precedence, it must yield 22. Let’s discuss this with the help of a Python program:

Python3




# Multi-operator expression
  
a = 10 + 3 * 4
print(a)
  
b = (10 + 3) * 4
print(b)
  
c = 10 + (3 * 4)
print(c)


Output

22
52
22

Hence, operator precedence plays an important role in the evaluation of a Python expression.



Previous Article
Next Article

Similar Reads

Mathematical Operations on Algebraic Expressions - Algebraic Expressions and Identities | Class 8 Maths
The basic operations that are being used in mathematics (especially in real number systems) are addition, subtraction, multiplication and so on. These operations can also be done on the algebraic expressions. Let us see them in detail. Algebraic expressions (also known as algebraic equations) are defined as "the type of expressions", in which it ma
5 min read
Python | Set 2 (Variables, Expressions, Conditions and Functions)
Introduction to Python has been dealt with in this article. Now, let us begin with learning python. Running your First Code in Python Python programs are not compiled, rather they are interpreted. Now, let us move to writing python code and running it. Please make sure that python is installed on the system you are working on. If it is not installe
3 min read
What is an Expression and What are the types of Expressions?
Expression: An expression is a combination of operators, constants and variables. An expression may consist of one or more operands, and zero or more operators to produce a value. Example: a+b c s-1/7*f . . etc Types of Expressions: Expressions may be of the following types: Constant expressions: Constant Expressions consists of only constant value
2 min read
Linear Equations in One Variable - Solving Equations which have Linear Expressions on one Side and Numbers on the other Side | Class 8 Maths
Linear equation is an algebraic equation that is a representation of the straight line. Linear equations are composed of variables and constants. These equations are of first-order, that is, the highest power of any of the involved variables i.e. 1. It can also be considered as a polynomial of degree 1. Linear equations containing only one variable
3 min read
Class 8 NCERT Solutions - Chapter 9 Algebraic Expressions and Identities - Exercise 9.5 | Set 1
Question 1. Use a suitable identity to get each of the following products. (i) (x + 3) (x + 3) Solution: (x + 3) (x + 3) Putting formula (a + b)2 = a2 + b2+ 2ab Put a = x &amp; b = 3 (x + 3) (x +3 ) = (x + 3)2 = x2 + 6x + 9 (ii) (2y + 5) (2y + 5) Solution: (2y + 5) (2y + 5) Putting formula (a + b)2 = a2 + b2 + 2ab Put a = 2y &amp; b = 5 (2y + 5) (2
8 min read
Class 8 NCERT Solutions - Chapter 9 Algebraic Expressions and Identities - Exercise 9.2
Question 1. Find the product of the following pairs of monomials. Monomial: Expression containing only one term (i) 4, 7p Ans: (4) * (7p) = 28p (ii) -4p, 7p Ans: (-4p) * (7p) = -28p2 Explanation: When a negative number is multiplied to a positive number the product becomes negative. (iii) -4p, 7pq Ans: (-4p) * (7pq) = -28p2q (iv) 4p3, -3p Ans: (4p3
2 min read
Class 8 NCERT Solutions - Chapter 9 Algebraic Expressions and Identities - Exercise 9.5 | Set 2
Chapter 9 Algebraic Expressions and Identities - Exercise 9.5 | Set 1 Question 5. Show that: (i) (3x + 7)2 - 84x = (3x - 7)2 Solution: L.H.S. = (3x + 7)2 - 84x = 9x2 + 42x + 49 - 84x = 9x2 - 42x + 49 = (3x - 7)2 = R.H.S. L.H.S. = R.H.S. (ii) (9p - 5q)2 + 180pq = (9p + 5q)2 Solution: LHS = (9p - 5q)2 + 180pq = 81p2 - 90pq + 25q2 + 180pq = 81p2 + 90p
5 min read
Class 8 NCERT Solutions - Chapter 9 Algebraic Expressions and Identities - Exercise 9.3
Question 1. Carry out the multiplication of the expressions in each of the following pairs. (i) 4p, q + r Solution: (4p) * (q + r) = 4pq + 4pr (ii) ab, a - b Solution: (ab) * (a - b) = a2 b - ab2 (iii) a + b, 7a2b2 Solution: (a + b) * (7a2b2) = 7a3b2 + 7a2b3 (iv) a2 - 9, 4a Solution: (a2 - 9) * (4a) = 4a3 - 36a (v) pq + qr + rp, 0 Solution: (pq + q
5 min read
Class 8 RD Sharma Solutions - Chapter 6 Algebraic Expressions And Identities - Exercise 6.1
Question 1. Identify the terms, their coefficients for each of the following expressions: (i) 7x2yz – 5xy (ii) x2 + x + 1 (iii) 3x2y2 – 5x2y2z2 + z2 (iv) 9 – ab + bc – ca (v) a/2 + b/2 – ab (vi) 0.2x – 0.3xy + 0.5y Solution: (i) 7x2yz – 5xy In this expression there are two terms: 7x2yz and – 5xy The coefficient of 7x2yz is 7 The coefficient of –5xy
3 min read
Class 8 RD Sharma Solutions - Chapter 6 Algebraic Expressions And Identities - Exercise 6.3 | Set 1
Find each of the following products:Question 1: 5x2 * 4x3 Solution: First, separate the numbers and variables. = (5 * 4) * (x2 * x3) Add the powers of the same variable and multiply the numbers. = 20 * (x2+3) = 20x5 Question 2: -3a2 * 4b4 Solution: First, separate the numbers and variables. = (-3 * 4) * (a2) * (b4) = -12a2b4 Question 3: (-5xy) * (-
5 min read