The Wayback Machine - https://web.archive.org/web/20240905235949/https://www.geeksforgeeks.org/expressions-in-python/
Open In App

Expressions in Python

Last Updated : 03 Sep, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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
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
Class 9 RD Sharma Solutions - Chapter 5 Factorisation of Algebraic Expressions- Exercise 5.1
Question 1: Factorize x3 + x – 3x2 – 3 Solution: x3 + x – 3x2 – 3 Here x is common factor in x3 + x and – 3 is common factor in – 3x2 – 3 x3 – 3x2 + x – 3 x2 (x – 3) + 1(x – 3) Taking (x – 3) common (x – 3) (x2 + 1) Therefore, x3 + x – 3x2 – 3 = (x – 3) (x2 + 1) Question 2: Factorize a(a + b)3 – 3a2b(a + b) Solution: a(a + b)3 – 3a2b(a + b) Taking
7 min read
Class 8 RD Sharma Solutions - Chapter 6 Algebraic Expressions And Identities - Exercise 6.5 | Set 1
Multiply the following:Question 1. (5x + 3) by (7x + 2) Solution: To find the product of (5x + 3) (7x+2) [use distributive property] ⇒ (5x + 3) (7x + 2) ⇒ 5x(7x+2) + 3(7x+2) ⇒ (5x × 7x + 5x × 2) + (3 × 7x + 3 × 2) ⇒ 35x2 + 10x + 21x + 6 ⇒ 35x2 + 31x + 6 The required answer is 35x2 + 31x + 6 Question 2. (2x + 8) by (x - 3) Solution: To find the prod
5 min read
Class 8 RD Sharma Solutions - Chapter 8 Division Of Algebraic Expressions - Exercise 8.2
Question 1. Divide 6x3y2z2 by 3x2yz Solution: We can write 6x3y2z2 as 2 * 3 * x * x * x * y * y * z * z We can write 3x2yz as 3 * x * x * y * z Now ans = (6x3y2z2)/(3x2yz) = (2 * 3 * x * x * x * y * y * z * z)/(3 * x * x * y * z) = 2 * x * y * z So the answer is 2xyz Question 2. Divide 15m2n3 by 5m2n2 Solution: We can write 15m2n3 as 5 * 3 * m * m
4 min read
Class 8 RD Sharma Solutions - Chapter 8 Division Of Algebraic Expressions - Exercise 8.3
Question 1. Divide x + 2x2 + 3x4 – x5 by 2x Solution: Here, (x + 2x2 + 3x4 – x5) / 2x x/2x + 2x2/2x + 3x4/2x – x5/2x By using this formula ⇒ an/am = an-m 1/2 x1-1 + x2-1 + 3/2 x4-1 – 1/2 x5-1 1/2 + x + 3/2 x3 – 1/2 x4 Question 2. Divide y4 – 3y3 + 1/2y2 by 3y Solution: Here, (y4 – 3y3 + 1/2y2)/ 3y y4/3y – 3y3/3y + (½)y2/3y By using this formula ⇒ a
2 min read
Class 9 RD Sharma Solutions - Chapter 5 Factorisation of Algebraic Expressions- Exercise 5.2 | Set 1
Factorize each of the following expressions:Question 1. p3+27 Solution: ⇒ p3+33 ⇒(p+3)(p2-3p-9) [ a3+b3=(a+b)(a2-ab+b2) ] Therefore, p3+27 = (p+3)(p2-3p-9) Question 2. y3+125 Solution: ⇒ y3+53 ⇒ (y+5)(y2-3y+25) [ a3+b3=(a+b)(a2-ab+b2) ] Therefore, y3+125 = (y+5)(y2-3y+25) Question 3. 1 - 27a3 Solution: ⇒ (1)3 - (3a)3 ⇒ (1-3a)(12+3a+(3a)2) [ a3-b3=(
2 min read
Class 8 RD Sharma Solutions - Chapter 8 Division Of Algebraic Expressions - Exercise 8.1
Question 1: Write the degree of each of the following polynomials: (i) 2x3 + 5x2 – 7 (ii) 5x2 – 3x + 2 (iii) 2x + x2 – 8 (iv) 1/2y7 – 12y6 + 48y5 – 10 (v) 3x3 + 1 (vi) 5 (vii) 20x3 + 12x2y2 – 10y2 + 20 Solution: In a polynomial, degree is the highest power of the variable. (i) 2x3 + 5x2 – 7 Given: 2x3 + 5x2 – 7 Therefore, the degree of the polynomi
5 min read
Class 8 RD Sharma Solutions- Chapter 6 Algebraic Expressions And Identities - Exercise 6.2
Question 1. Add the following algebraic expressions:(i) 3a2b, -4a2b, 9a2b Solution: 3a2b, -4a2b, 9a2bNow we have add the given expression= 3a2b + (-4a2b) + 9a2b= 3a2b – 4a2b + 9a2b= 8a2b (ii) 2/3a, 3/5a, -6/5a Solution: We have to add the given expression2/3a + 3/5a + (-6/5a)2/3a + 3/5a – 6/5aNow take LCM for 3 and 5 which will be 15= (2×5)/(3×5)a
9 min read
Class 8 RD Sharma Solutions- Chapter 6 Algebraic Expressions And Identities - Exercise 6.6 | Set 1
Question 1. Write the following squares of binomials as trinomials:(i) (x + 2)2 Solution: x2 + 2 (x) (2) + 22 x2 + 4x + 4 (ii) (8a + 3b)2 Solution: (8a)2 + 2 (8a) (3b) + (3b) 64a2 + 48ab + 9b2 (iii) (2m + 1)2 Solution: (2m)2 + 2 (2m) (1) + 12 4m2 + 4m + 1 (iv) (9a + 1/6)2 Solution: (9a)2 + 2 (9a) (1/6) + (1/6)2 81a2 + 3a + 1/36 (v) (x + x2/2)2 Solu
9 min read
Class 8 RD Sharma Solutions - Chapter 8 Division Of Algebraic Expressions - Exercise 8.5
Question 1: Divide the first polynomial by the second polynomial in each of the following. Also, write the quotient and remainder:(i) 3x2 + 4x + 5, x – 2 Solution: 3x2 + 4x + 5, x – 2 By using factorization method, [Tex]⇒\:\frac{\left(3x^2+4x+5\right)}{x-2} = \:\frac{3x\left(x-2)+10(x-2\right)+25}{x-2} [/Tex] [Tex]=\:\frac{\left(x-2)(3x+10\right)+2
3 min read
Class 9 RD Sharma Solutions - Chapter 5 Factorisation of Algebraic Expressions- Exercise 5.2 | Set 2
Factorize each of the following expressions:Question 13. 8x2y3-x5 Solution: ⇒ x2((2y)3 - x3) ⇒ x2(2y-x)((2y)2+2xy+x2) [x3-y3=(x-y)(x2+xy+y2)] ⇒ x2(2y-x)(4y2+2xy+x2) Therefore, 8x3y3-x5 = x2(2y-x)(4y2+2xy+x2) Question 14. 1029 - 3x3 Solution: ⇒ 3 (343 - x3) ⇒ 3(73 - x3) [x3-y3=(x-y)(x2+xy+y2)] ⇒ 3 (7-x)(72+7x+x2) Therefore, 1029 - 3x3 = 3 (7-x)(72+7
2 min read
Class 8 RD Sharma Solutions - Chapter 6 Algebraic Expressions And Identities - Exercise 6.7
Question 1. Find the following products:(i) (x + 4) (x + 7) Solution: By simplifying the given expression, we get x (x + 7) + 4 (x + 7) x2 + 7x + 4x + 28 x2+ 11x + 28 (ii) (x – 11) (x + 4) Solution: By simplifying the given expression, we get x (x + 4) – 11 (x + 4) x2 + 4x – 11x – 44 x2 – 7x – 44 (iii) (x + 7) (x – 5) Solution: By simplifying the g
5 min read
Class 9 RD Sharma Solutions - Chapter 5 Factorisation of Algebraic Expressions- Exercise 5.4
Factorize each of the following:Question 1. a3 + 8b3 + 64c3 – 24abc Solution: We know that a3 + b3 + c3 – 3abc = (a + b + c) (a2 + b2 + c2 – ab – bc – ca) = a3 + 8b3 + 64c3 – 24abc = (a)3 + (2b)3 + (4c)3 – (3 × a × 2b × 4c) = (a + 2b + 4c) [(a)2 + (2b)2 + (4c)2 -(a × 2b)– (2b × 4c)– (4c × a)]2 = (a + 2b + 4c) (a2 + 4b2 + 16c2 – 2ab – 8bc – 4ca) Que
7 min read
Class 8 RD Sharma Solutions - Chapter 8 Division Of Algebraic Expressions - Exercise 8.4 | Set 1
Question 1. Divide 5x3 – 15x2 + 25x by 5x Solution: We have to divide 5x3 – 15x2 + 25x by 5x So, (5x3 – 15x2 + 25x)/5x 5x3/5x – 15x2/5x + 25x/5x x2 - 3x + 5 Question 2. Divide 4z3 + 6z2 - z by −1/2 z Solution: We have to divide 4z3 + 6z2-z by −1/2 z So, (4z3 + 6z2 - z) / (-1/2)z (4z3/(-1/2)z) + (6z2/(-1/2)z) + (-z/(-1/2)z) (-4 × 2/1)z2 + (6 × (-2)/
5 min read
Why are Algebraic Expressions Useful?
Algebraic expression started in the 9th century. In the beginning, it was more in statement form and not mathematical at all. For instance, algebraic equations used to be written as "5 times the thing added with 3 gives 18" which is basically 5x + 3 = 18. This type of equation which was not mathematical was Babylonian algebra. Algebra evolved with
5 min read
What are the types of algebraic expressions?
An algebraic expression is an expression composed of various components, such as the variables, constants, coefficients, and arithmetic operations. These components form various parts of the algebraic expressions. An algebraic expression is a linear equation composed of any number of variables. The highest power of the variable is known as the degr
6 min read
Clarity and Simplicity of Expressions
The concept of programming lies between solving complex expressions, to ease human efforts. The computer is super fast and can do a lot of calculations, but if the expression you provided is not clear enough, then it is going to be confusing, or the human who will later be willing to understand or improve the code won't understand the clarity of th
6 min read
How to Add and Subtract Rational Expressions with Unlike Denominators and Variables?
Numbers represented in the form of m/n are called Fractions. Here, 'm' or the upper part of the fraction is the numerator, and 'n' or the lower part of the fraction is called the denominator. Fractions with numerator lesser than Denominator, are Proper Fraction. Fractions with numerator greater than Denominator, fall in the category of Improper Fra
4 min read
Give some examples of equivalent expressions of 10/13
Just like, letters are the basic unit of any language similarly numbers are the basic unit of mathematics. The origin of the number system was done in the ancient age by our ancestors. In an early age, they used the barter system. They exchange things. But later they might start using their fingers for calculation purposes and that might lead to th
4 min read
Simplifying Expressions and Combining Like Terms
Algebra is the branch of mathematics that deals with mathematical expressions. It comprises variables, coefficients, operators, and constants. Example: Let 3x + 4 be an algebraic expression. Then 3 is the coefficient, x is the variable, + is the operator, and 4 is the constant. Algebra helps to derive unknown quantities. Let us go through the compo
4 min read
Are algebraic expressions polynomials?
Algebra is a branch of mathematics that deals with equations consisting of coefficients and variables, where variables are unknown quantities. These equations are solved with certain formulas to gain a solution. These formulas are the combination of numbers and letters in which numbers represent known and letters represent unknown values. Algebraic
3 min read
NCERT Solutions Class 8 – Chapter 8 Algebraic Expressions and Identities – Exercise 8.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 + qr
5 min read
NCERT Solutions Class 8 - Chapter 8 Algebraic Expressions and Identities – Exercise 8.4
Problem 1. Multiply the binomials.Solution: When we multiply two binomials, four multiplications must take place. These multiplications can be in any order, although we need to take care of that each of the first two terms is multiplied by each of the second terms. For example: (2x + 3)(3x – 1), if we have to multiply these two binomial. Step 1: Mu
7 min read
NCERT Solutions Class 8 – Chapter 8 Algebraic Expressions and Identities – Exercise 8.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
Addition and Subtraction of Algebraic Expressions
Addition and Subtraction of Algebraic Expressions are fundamental operations performed on algebraic expressions. These operations are used to simplify algebraic expressions. Let's learn in detail about Algebraic expressions, addition of algebraic expressions, subtraction of algebraic expressions and others in detail. Table of Content What are Algeb
8 min read
How are Algebraic Expressions Formed?
Basic concept of algebra taught us how to express an unknown value using letters such as x, y, z, etc.  These letters are termed here as variables. this expression can be a combination of both variables and constants. Any value that is placed before and multiplied by a variable is termed a coefficient. An idea of expressing numbers using letters or
5 min read
Addition of Algebraic Expressions
Algebraic expressions are mathematical phrases that can include numbers, variables, and operation symbols. The addition of algebraic expressions involves combining like terms to simplify the expression. Addition of Algebraic Expression involves combining like terms of the given expression and then adding their numeral coefficients. We can add two o
7 min read